<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申报信息名称" prop="handbookName" label-width="100px" >
        <el-input
          v-model="queryParams.handbookName"
          placeholder="请输入申报信息名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目指南级别" prop="handbookLevel" label-width="100px">
        <el-input
          v-model="queryParams.handbookLevel"
          placeholder="请输入项目指南级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报开始日期" prop="projectAppStTime" label-width="100px">
        <el-date-picker clearable
          v-model="queryParams.projectAppStTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申报开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申报结束日期" prop="projectAppEndTime" label-width="100px">
        <el-date-picker clearable
          v-model="queryParams.projectAppEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申报结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="指南负责人" prop="handbookLeader" label-width="100px">
        <el-input
          v-model="queryParams.handbookLeader"
          placeholder="请输入指南负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['scientific:handbook:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scientific:handbook:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scientific:handbook:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['scientific:handbook:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="handbookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <!-- <el-table-column label="项目ID" align="center" prop="handbookId" v-if="true"/> -->
      <el-table-column label="申报信息名称" align="center" prop="handbookName" :min-width="180"/>
      <!-- <el-table-column label="项目审核状态" align="center" prop="handbookStatus" /> -->
      <el-table-column label="项目指南类型" align="center" prop="handbookType" />

      <el-table-column label="项目指南级别" align="center" width="100px" :min-width="100">
        <template slot-scope="scope">
          <el-tag size="medium" :type="getTagLevelType(scope.row.handbookLevel)">
            {{ formatHandbookLevel(scope.row.handbookLevel) }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="申报状态" align="center" width="100px" :min-width="100">
        <template slot-scope="scope">
          <el-tag size="medium" :type="getTagType(scope.row.declareStatus)">
            {{ formatHandbookStatus(scope.row.declareStatus) }}
          </el-tag>
        </template>
      </el-table-column>



      <el-table-column label="申报开始日期" align="center" prop="projectAppStTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.projectAppStTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申报结束日期" align="center" prop="projectAppEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.projectAppEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="指南负责人" align="center" prop="handbookLeader" />
      <el-table-column label="指南负责人电话" align="center" prop="projectLeaderPhone" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['scientific:handbook:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scientific:handbook:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目申报v2对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申报信息名称" prop="handbookName">
          <el-input v-model="form.handbookName" placeholder="请输入申报信息名称" />
        </el-form-item>
        <el-form-item label="项目立项流程实例Id" prop="handbookProcId">
          <el-input v-model="form.handbookProcId" placeholder="请输入项目立项流程实例Id" />
        </el-form-item>
        <el-form-item label="项目指南级别(0市级 1区级 3省级 4国家级)" prop="handbookLevel">
          <el-input v-model="form.handbookLevel" placeholder="请输入项目指南级别(0市级 1区级 3省级 4国家级)" />
        </el-form-item>
        <el-form-item label="申报开始日期" prop="projectAppStTime">
          <el-date-picker clearable
            v-model="form.projectAppStTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择申报开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申报结束日期" prop="projectAppEndTime">
          <el-date-picker clearable
            v-model="form.projectAppEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择申报结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="指南负责人" prop="handbookLeader">
          <el-input v-model="form.handbookLeader" placeholder="请输入指南负责人" />
        </el-form-item>
        <el-form-item label="指南负责人电话" prop="projectLeaderPhone">
          <el-input v-model="form.projectLeaderPhone" placeholder="请输入指南负责人电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHandbook, getHandbook, delHandbook, addHandbook, updateHandbook } from "@/api/scientific/handbook";

export default {
  name: "Handbook",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目申报v2表格数据
      handbookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        handbookName: undefined,
        handbookProcId: undefined,
        handbookStatus: undefined,
        handbookType: undefined,
        handbookLevel: undefined,
        declareStatus: undefined,
        projectAppStTime: undefined,
        projectAppEndTime: undefined,
        handbookLeader: undefined,
        projectLeaderPhone: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        handbookId: [
          { required: true, message: "项目ID不能为空", trigger: "blur" }
        ],
        handbookName: [
          { required: true, message: "申报信息名称不能为空", trigger: "blur" }
        ],
        handbookProcId: [
          { required: true, message: "项目立项流程实例Id不能为空", trigger: "blur" }
        ],
        handbookStatus: [
          { required: true, message: "项目审核状态(0未审核 1审核中 3审核通过)不能为空", trigger: "change" }
        ],
        handbookType: [
          { required: true, message: "项目指南类型(0 科技计划 1 研发计划)不能为空", trigger: "change" }
        ],
        handbookLevel: [
          { required: true, message: "项目指南级别(0市级 1区级 3省级 4国家级)不能为空", trigger: "blur" }
        ],
        declareStatus: [
          { required: true, message: "申报状态(0 停止申报 1 申报中)不能为空", trigger: "change" }
        ],
        projectAppStTime: [
          { required: true, message: "申报开始日期不能为空", trigger: "blur" }
        ],
        projectAppEndTime: [
          { required: true, message: "申报结束日期不能为空", trigger: "blur" }
        ],
        handbookLeader: [
          { required: true, message: "指南负责人不能为空", trigger: "blur" }
        ],
        projectLeaderPhone: [
          { required: true, message: "指南负责人电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目申报v2列表 */
    getList() {
      this.loading = true;
      listHandbook(this.queryParams).then(response => {
        this.handbookList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        handbookId: undefined,
        handbookName: undefined,
        handbookProcId: undefined,
        handbookStatus: undefined,
        handbookType: undefined,
        handbookLevel: undefined,
        declareStatus: undefined,
        projectAppStTime: undefined,
        projectAppEndTime: undefined,
        handbookLeader: undefined,
        projectLeaderPhone: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.handbookId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目申报v2";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const handbookId = row.handbookId || this.ids
      getHandbook(handbookId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改项目申报v2";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.handbookId != null) {
            updateHandbook(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addHandbook(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const handbookIds = row.handbookId || this.ids;
      this.$modal.confirm('是否确认删除项目申报v2编号为"' + handbookIds + '"的数据项？').then(() => {
        this.loading = true;
        return delHandbook(handbookIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('scientific/handbook/export', {
        ...this.queryParams
      }, `handbook_${new Date().getTime()}.xlsx`)
    },
        /**
     * 把项目状态的申报code转换为文字: 0 : 未申报  1: 审核中  2: 已申报  
     * @param   {number|string} cellValue - 原始数值
     * @returns {string} 转换后的字符串
     * @author  zhanli
     */
     formatHandbookStatus(cellValue) {
      switch (cellValue) {
        case '0':
          return "暂未开始";
        case '1':
          return "申报中";
        case '2':
          return "停止申报";
        default:
          return "未知状态";
      }
    },
    formatHandbookLevel(cellValue) {
      switch (cellValue) {
        case '0':
          return "市级";
        case '1':
          return "区级";
        case '2':
          return "省级";
        case '3':
          return "国家级";
        default:
          return "未知";
      }
    },
    getTagType(status) {
      switch (status) {
        case '2':
          // 显示红色 停止申报
          return 'danger';
        case '1':
          // 审核中 显示绿色
          return 'success';
        case '0':
          // 未申报, 灰色
          return 'info';
        case '4':
          // 其他状态 灰色
          return 'primary';
        default:
          return '';
      }
    },
    getTagLevelType(status) {
      switch (status) {
        case '2':
          // 省级 蓝色
          return 'primary';
        case '1':
          // 市级 显示绿色
          return 'success';
        case '0':
          // 区级, 灰色
          return 'info';
        case '3':
          // 国家级 红色
          return 'danger';
        default:
          return '';
      }
    }
  }
};
</script>
