<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">
        <el-input
          v-model="queryParams.handbookName"
          placeholder="请输入指南名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指南分类" prop="handbookCategory">
        <el-input
          v-model="queryParams.handbookCategory"
          placeholder="请输入指南分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指南级别" prop="handbookLevel">
        <el-input
          v-model="queryParams.handbookLevel"
          placeholder="请输入指南级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="handbookDate">
        <el-date-picker clearable
          v-model="queryParams.handbookDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发布时间">
        </el-date-picker>
      </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:project_application_plan: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:project_application_plan: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:project_application_plan: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:project_application_plan: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="指南名称" align="center"  :min-width="150">
        <template slot-scope="scope">
          <el-tag size="medium" :type="info">
            {{ scope.row.handbookName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发布人" prop="directorName" width="80"></el-table-column>
      <el-table-column label="指南分类" align="center" :min-width="150">
        <template slot-scope="scope">
            {{ formatHandbookType(scope.row.handbookLevel) }}
        </template>
      </el-table-column>

      <el-table-column label="指南级别" align="center" :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" prop="handbookDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handbookDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

<!--      <el-table-column label="指南文件" align="center" prop="handbookFile" />-->
<!--      <el-table-column label="指南录入流程状态" align="center" prop="handbookStatus" />-->
      <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:project_application_plan:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['scientific:project_application_plan:remove']"-->
<!--          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleHandbookDetail(scope.row)"
            v-hasPermi="['scientific:project_application_plan:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-top"
            @click="handlePlanApply(scope.row)"
            v-hasPermi="['scientific:project_application_plan: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"
    />

    <!-- 添加或修改项目申报指南对话框 -->
    <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="指南分类" prop="handbookCategory">
          <el-input v-model="form.handbookCategory" placeholder="请输入指南分类" />
        </el-form-item>
        <el-form-item label="指南级别(4院级 3市级 2省级 1国家级)" prop="handbookLevel">
          <el-input v-model="form.handbookLevel" placeholder="请输入指南级别(4院级 3市级 2省级 1国家级)" />
        </el-form-item>
        <el-form-item label="发布时间" prop="handbookDate">
          <el-date-picker clearable
            v-model="form.handbookDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="handbookNote">
          <el-input v-model="form.handbookNote" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="指南文件" prop="handbookFile">
          <file-upload v-model="form.handbookFile"/>
        </el-form-item>
        <el-form-item label="指南录入流程实例Id" prop="handbookProcId">
          <el-input v-model="form.handbookProcId" placeholder="请输入指南录入流程实例Id" />
        </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";
import planApply from '@/views/scientific/project_application_plan/planApply.vue'
import { listProcess } from '@/api/workflow/process'

export default {
  name: "Handbook",
  computed: {
    planApply() {
      return planApply
    }
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目申报指南表格数据
      handbookList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        handbookId: undefined,
        handbookName: undefined,
        handbookCategory: undefined,
        handbookLevel: undefined,
        handbookDate: undefined,
        handbookNote: undefined,
        handbookFile: undefined,
        handbookProcId: undefined,
        handbookStatus: 3,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        handbookId: [
          { required: true, message: "项目ID不能为空", trigger: "blur" }
        ],
        handbookName: [
          { required: true, message: "指南名称不能为空", trigger: "blur" }
        ],
        handbookCategory: [
          { required: true, message: "指南分类不能为空", trigger: "blur" }
        ],
        handbookLevel: [
          { required: true, message: "指南级别(4院级 3市级 2省级 1国家级)不能为空", trigger: "blur" }
        ],
        handbookDate: [
          { required: true, message: "发布时间不能为空", trigger: "blur" }
        ],
        handbookNote: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        handbookFile: [
          { required: true, message: "指南文件不能为空", trigger: "blur" }
        ],
        handbookProcId: [
          { required: true, message: "指南录入流程实例Id不能为空", trigger: "blur" }
        ],
        handbookStatus: [
          { required: true, message: "指南录入流程状态(0未发起审核 1审核中 2审核失败 3审核通过)不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目申报指南列表 */
    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,
        handbookCategory: undefined,
        handbookLevel: undefined,
        handbookDate: undefined,
        handbookNote: undefined,
        handbookFile: undefined,
        handbookProcId: undefined,
        handbookStatus: 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 = "添加项目申报指南";
    },
    /** 修改按钮操作 */
    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 = "修改项目申报指南";
      });
    },
    /** 提交按钮 */
    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('是否确认删除项目申报指南编号为"' + 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/project_application_plan/export', {
        ...this.queryParams
      }, `handbook_${new Date().getTime()}.xlsx`)
    },
    handleHandbookDetail(row) {
      this.$router.push({
        path: '/scientific/handbook/detail/' + row.handbookProcId,
        query: {
          processed: false,
        }
      })
    },
    handlePlanApply(row) {
      // 跳转到申报计划录入
      this.$router.push({
        name: 'planApply',
        params: {
          handbook: row
        }
      })
    },
    formatHandbookLevel(cellValue) {
      switch (cellValue) {
        case '1':
          return "国家级";
        case '2':
          return "省级";
        case '3':
          return "市级";
        case '4':
          return "院级";
        default:
          return "未知";
      }
    },
    getTagLevelType(status) {
      switch (status) {
        case '2':
          // 省级 蓝色
          return 'primary';
        case '3':
          // 市级 显示绿色
          return 'success';
        case '4':
          // 院级, 灰色
          return 'info';
        case '1':
          // 国家级 红色
          return 'danger';
        default:
          return '';
      }
    },
    formatHandbookType(cellValue) {
      switch (cellValue) {
        case '1':
          return "科技计划";
        case '2':
          return "研发计划";
        default:
          return "未知";
      }
    },
  }
};
</script>
