<!--
 * @Author: ysh
 * @Date: 2025-08-15 16:13:32
 * @LastEditors: wrh
 * @LastEditTime: 2026-01-23 16:57:18
-->
<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="type">
        <el-select v-model="queryParams.type" clearable placeholder="请选择职称类型" @change="handleQuery">
          <el-option label="工程类" value="工程类" />
          <el-option label="经济类" value="经济类" />
          <el-option label="政工类" value="政工类" />
          <el-option label="企业法律顾问类" value="企业法律顾问类" />
        </el-select>
      </el-form-item>
      <el-form-item label="职称级别" prop="level">
        <el-select v-model="queryParams.level" clearable placeholder="请选择职称级别" @change="handleQuery">
          <el-option label="正高级" value="正高级"></el-option>
          <el-option label="副高级" value="副高级"></el-option>
          <el-option label="高级" value="高级"></el-option>
          <el-option label="中级" value="中级"></el-option>
          <el-option label="初级" value="初级"></el-option>
          <el-option label="员级" value="员级"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="userId">
        <el-select v-model="queryParams.userId" filterable clearable @change="handleQuery">
          <el-option v-for="item in $store.state.user.userList" :key="item.userId" :label="item.nickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </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="['oa:title: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="['oa:title: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="['oa:title: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="['oa:title:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="evalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="userId">
        <template slot-scope="scope">
          {{ getUserName(scope.row.userId) }}
        </template>
      </el-table-column>
      <el-table-column label="职称类型" align="center" prop="type" />
      <el-table-column label="职称级别" align="center" prop="level" />
      <el-table-column label="职称专业名称" align="center" prop="titleProfession" />
      <el-table-column label="评审单位" align="center" prop="institude" />
      <el-table-column label="证书取得时间" align="center" prop="obtainTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.obtainTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="文件" align="center" prop="publicityFile">
        <template slot-scope="scope">
          <el-link type="primary" @click="reviewWord(`${baseUrl}${'/profile/upload' + scope.row.publicityFile}`)">
            {{ getFileName(scope.row.publicityFile) }}
          </el-link>
          <br>
          <el-link class="ml20" type="warning" :href="`${baseUrl}${'/profile/upload' + scope.row.publicityFile}`"
            :underline="false" target="_blank">
          </el-link>
          <el-link type="primary" @click="reviewWord(`${baseUrl}${'/profile/upload' + scope.row.scanFile}`)">
            {{ getFileName(scope.row.scanFile) }}
          </el-link>
          <el-link class="ml20" type="warning" :href="`${baseUrl}${'/profile/upload' + scope.row.scanFile}`"
            :underline="false" target="_blank">
          </el-link>
        </template>
      </el-table-column>
      <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="['oa:title:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['oa:title: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="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="formRules" label-width="120px">
        <el-form-item label="姓名" prop="userId">
          <el-select v-model="form.userId" filterable clearable>
            <el-option v-for="item in $store.state.user.userList" :key="item.userId" :label="item.nickName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职称类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择职称类型" style="width: 100%">
            <el-option label="工程类" value="工程类" />
            <el-option label="经济类" value="经济类" />
            <el-option label="政工类" value="政工类" />
            <el-option label="企业法律顾问类" value="企业法律顾问类" />
          </el-select>
        </el-form-item>
        <el-form-item label="职称级别" prop="level">
          <el-select v-model="form.level" placeholder="请选择职称级别" style="width: 100%">
            <el-option label="员级" value="员级" />
            <el-option label="助理级" value="助理级" />
            <el-option label="中级" value="中级" />
            <el-option label="副高级" value="副高级" />
            <el-option label="正高级" value="正高级" />
          </el-select>
        </el-form-item>
        <el-form-item label="职称专业名称" prop="titleProfession">
          <el-input v-model="form.titleProfession" placeholder="请输入职称专业名称" maxlength="50" show-word-limit />
        </el-form-item>
        <el-form-item label="评审单位" prop="institude">
          <el-select v-model="form.institude" placeholder="请选择评审单位" style="width: 100%">
            <el-option label="中国电建" value="中国电建" />
            <el-option label="成都人社" value="成都人社" />
            <el-option label="四川省测绘局" value="四川省测绘局" />
            <el-option label="四川省建设厅" value="四川省建设厅" />
            <el-option label="其他" value="其他" />
          </el-select>
        </el-form-item>

        <el-form-item label="公示红头文件" prop="publicityFile">
          <file-upload v-model="form.publicityFile" :filePathName="'职称评审/公示红头文件/' + form.annual + '/' + getUserName(form.userId)" :fileType="['pdf']"
            @input="getPublicityFilePath" />
        </el-form-item>
        <el-form-item label="证书扫描件" prop="scanFile">
          <file-upload v-model="form.scanFile" :filePathName="'职称评审/证书扫描件/' + form.annual + '/' + getUserName(form.userId)" :fileType="['pdf']"
            @input="getScanFilePath" />
        </el-form-item>
        <el-form-item label="证书取得时间" prop="obtainTime">
          <el-date-picker clearable v-model="form.obtainTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择证书取得时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listEval,
  getEval,
  delEval,
  addEval,
  updateEval,
} from "@/api/oa/titles/titles";
import { Snowflake } from "@/utils/snowFlake.js";

export default {
  name: "Eval",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 职称信息表格数据
      evalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        institude: null,
        type: null,
        level: null,
        titleProfession: null,
        sheet: null,
        sheetStamp: null,
        material: null,
        isApproved: null,
        publicityFile: null,
        scanFile: null,
        obtainTime: null,
        applyTime: null,
        titleUserId: null,
        titleTime: null,
        titleComment: null,
        materialUploadTime: null,
        confirmer: null,
        confirmTime: null,
        confirmStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  computed: {
    formRules() {
      return {
        userId: [
          { required: true, message: '请选择申请人', trigger: 'change' }
        ],
        institude: [
          { required: true, message: '请选择评审单位', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请选择职称类型', trigger: 'change' }
        ],
        level: [
          { required: true, message: '请选择职称级别', trigger: 'change' }
        ],
        titleProfession: [
          { required: true, message: '请输入职称专业名称', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        sheet: [
          { required: true, message: '请上传评审表(盖章前)', trigger: 'change' }
        ],
        sheetStamp: [
          { required: true, message: '请上传评审表(盖章后)', trigger: 'change' }
        ],
        material: [
          { required: true, message: '请上传其他材料', trigger: 'change' }
        ],
        publicityFile: [
          { required: () => this.form.isApproved == '1', message: '请上传公示红头文件', trigger: 'change' }
        ],
        scanFile: [
          { required: () => this.form.isApproved == '1', message: '请上传证书扫描件', trigger: 'change' }
        ],
        obtainTime: [
          { required: () => this.form.isApproved == '1', message: '请选择证书取得时间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    /** 查询职称信息列表 */
    getList() {
      this.loading = true;
      listEval(this.queryParams).then((response) => {
        this.evalList = response.rows.filter(item => item.isApproved === "1");
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getPublicityFilePath(val) {
      let arr = val.split('/upload')
      this.form.publicityFile = arr[1]
      if (val == "") {
        this.form.publicityFile = ""
      }
    },
    getScanFilePath(val) {
      let arr = val.split('/upload')
      this.form.scanFile = arr[1]
      if (val == "") {
        this.form.scanFile = ""
      }
    },
    // 表单重置
    reset() {
      this.form = {
        titleEvalId: null,
        userId: null,
        annual: null,
        institude: null,
        type: null,
        level: null,
        titleProfession: null,
        sheet: null,
        sheetStamp: null,
        material: null,
        isApproved: null,
        publicityFile: null,
        scanFile: null,
        obtainTime: null,
        applyTime: null,
        titleUserId: null,
        titleTime: null,
        titleComment: null,
        materialUploadTime: null,
        confirmer: null,
        confirmTime: null,
        confirmStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.titleEvalId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加职称信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const titleEvalId = row.titleEvalId || this.ids;
      getEval(titleEvalId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改职称信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.titleEvalId != null) {
            updateEval(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.titleEvalId = new Snowflake(1n, 1n, 0n).nextId().toString();
            this.form.titleUserId = this.$store.state.user.id;
            this.form.isApproved = "1";
            addEval(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const titleEvalIds = row.titleEvalId || this.ids;
      this.$modal
        .confirm('是否确认删除职称信息编号为"' + titleEvalIds + '"的数据项？')
        .then(function () {
          return delEval(titleEvalIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "oa/titleEval/export",
        {
          ...this.queryParams,
        },
        `eval_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped></style>