<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="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 label="项目编号" prop="projectId">
        <el-select v-model="queryParams.projectId" clearable filterable remote reserve-keyword placeholder="请输入项目编号"
          :remote-method="remoteMethod" :loading="loading" style="width: 400px;">
          <el-option v-for="project in projectList" :key="project.projectId"
            :label="project.projectNumber + '-' + project.projectName" :value="project.projectId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="考核时间" prop="assessTime">
        <el-date-picker clearable
          v-model="queryParams.assessTime"
          type="month"
          value-format="yyyy-MM"
          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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['oa:assess:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assessList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="总考核表id" align="center" prop="assessId" /> -->
      <el-table-column label="考核类型" width="155" align="center" prop="assessType">
        <template slot-scope="scope">
          <el-tag :type="getAssessStyle(scope.row.assessType)">{{ getAssessType(scope.row.assessType) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="被考核人" align="center" prop="userId">
        <template slot-scope="scope">
          <span>{{ getUserName(scope.row.userId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目编号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.project ? scope.row.project.projectNumber : "" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.project ? scope.row.project.projectName : "" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考核时间" align="center" prop="assessTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assessTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门考核等级" align="center" prop="deptAssessment">
        <template slot-scope="scope">
          <el-tag :type="getAssessmentStyle(scope.row.deptAssessment)">{{ getAssessmentType(scope.row.deptAssessment) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="部门考核意见" align="center" prop="deptAdvice" />
      <el-table-column label="部门考核时间" align="center" prop="deptTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deptTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门考核负责人" align="center" prop="deptAssesser">
        <template slot-scope="scope">
          <span>{{ getUserName(scope.row.deptAssesser) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分管考核等级" align="center" prop="managerAssessment">
        <template slot-scope="scope">
          <el-tag :type="getAssessmentStyle(scope.row.managerAssessment)">{{ getAssessmentType(scope.row.managerAssessment) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="分管考核意见" align="center" prop="managerAdvice" />
      <el-table-column label="分管考核时间" align="center" prop="managerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.managerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分管考核负责人" align="center" prop="managerAssesser">
        <template slot-scope="scope">
          <span>{{ getUserName(scope.row.managerAssesser) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="自述" align="center" prop="selfAssess" />
      <el-table-column label="考核填报时间" align="center" prop="fillTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fillTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="其他工作" align="center" prop="otherWork" /> -->
      <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-view"
            @click="handleView(scope.row)"
            v-hasPermi="['oa:assess:query']"
          >查看</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="infoOpen" width="70%" append-to-body>
      <sc-table :taskForm="taskForm" :taskName="''" :isFlow="false" v-if="taskForm.assessType == '0'"></sc-table>
      <jy-table :taskForm="taskForm" :taskName="''" :isFlow="false" v-if="taskForm.assessType == '1'"></jy-table>
      <zh-table :taskForm="taskForm" :taskName="''" :isFlow="false" v-if="taskForm.assessType == '2'"></zh-table>
      <js-table :taskForm="taskForm" :taskName="''" :isFlow="false" v-if="taskForm.assessType == '3'"></js-table>
      <cw-table :taskForm="taskForm" :taskName="''" :isFlow="false" v-if="taskForm.assessType == '4'"></cw-table>
    </el-dialog>
  </div>
</template>

<script>
import { listAssess, getAssess, delAssess, addAssess, updateAssess } from "@/api/oa/assess/assess";
import { listProject } from '@/api/oa/project/project';
import ScTable from '@/views/flowable/form/access/scTable.vue';
import JyTable from '@/views/flowable/form/access/jyTable.vue';
import ZhTable from '@/views/flowable/form/access/zhTable.vue';
import JsTable from '@/views/flowable/form/access/jsTable.vue';
import CwTable from '@/views/flowable/form/access/cwTable.vue';

export default {
  name: "Assess",
  components: {
    ScTable,
    JyTable,
    ZhTable,
    JsTable,
    CwTable,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc考核表格数据
      assessList: [],
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        projectId: null,
        assessTime: null,
        deptAssessment: null,
        deptAdvice: null,
        deptTime: null,
        deptAssesser: null,
        managerAssessment: null,
        managerAdvice: null,
        managerTime: null,
        managerAssesser: null,
        selfAssess: null,
        fillTime: null,
        otherWork: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      taskForm: {
        formId: ''
      },
      infoOpen: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc考核列表 */
    getList() {
      this.loading = true;
      listAssess(this.queryParams).then(response => {
        this.assessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    remoteMethod(val) {
      listProject({
        pageNum: 1,
        pageSize: 20,
        projectNumber: val
      }).then(res => {
        this.projectList = res.rows;
      })
    },
    // 表单重置
    reset() {
      this.form = {
        assessId: null,
        userId: null,
        projectId: null,
        assessTime: null,
        deptAssessment: null,
        deptAdvice: null,
        deptTime: null,
        deptAssesser: null,
        managerAssessment: null,
        managerAdvice: null,
        managerTime: null,
        managerAssesser: null,
        selfAssess: null,
        fillTime: null,
        otherWork: 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.assessId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc考核";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const assessId = row.assessId || this.ids
      getAssess(assessId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc考核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.assessId != null) {
            updateAssess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const assessIds = row.assessId || this.ids;
      this.$modal.confirm('是否确认删除cmc考核编号为"' + assessIds + '"的数据项？').then(function() {
        return delAssess(assessIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/assess/export', {
        ...this.queryParams
      }, `assess_${new Date().getTime()}.xlsx`)
    },
    handleView(row) {
      this.taskForm.formId = row.assessId;
      this.taskForm.assessType = row.assessType;
      this.infoOpen = true;
    },
    getAssessmentType(assessment){
      if (assessment == '0')
        return '不合格';
      else if (assessment == '1')
        return '合格';
      else if (assessment == '2')
        return '良好';
      else if (assessment == '3')
        return '优秀';
    },
    getAssessmentStyle(assessment){
      if (assessment == '0')
        return 'danger';
      else if (assessment == '1')
        return 'warning';
      else if (assessment == '2')
        return 'primary';
      else if (assessment == '3')
        return 'success';
    },
    getAssessType(assess){
      if (assess == '0')
        return '生产部门考核';
      else if (assess == '1')
        return '经营发展部考核';
      else if (assess == '2')
        return '综合事务部考核';
      else if (assess == '3')
        return '技术质量与安全部考核';
      else if (assess == '4')
        return '财务部考核';
    },
    getAssessStyle(assess){
      if (assess == '0')
        return 'info';
      else if (assess == '1')
        return 'success';
      else if (assess == '2')
        return 'warning';
      else if (assess == '3')
        return 'primary';
      else if (assess == '4')
        return 'danger';
    }
  }
};
</script>
