<template>
  <div>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true">
        <el-row >
          <el-col :span="18">
            <el-form-item label="部门">
              <el-cascader
                v-model="treeValue"
                :options="deptTree"
                :props="{ checkStrictly: true }"
                ref="cascaderHandle"
                clearable
                @change="handleChange"
                placeholder=""
                >
              </el-cascader>
            </el-form-item>
            <el-form-item label="员工">
              <el-input
                v-model="queryParams.params.staffCodeOrName"
                clearable
                @keyup.enter.native="btnSearch"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="float:right">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="btnSearch">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="btnResetQuery">重置</el-button>
              <el-button size="mini" @click="btnBack" style="float:right">返回</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="float:right">
        <el-dropdown @command="btnImport">
            <el-button type="primary" size="mini">
              导入<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-button size="mini" @click="useImportTemplate">模板下载</el-button>
              <el-upload ref="upload" action="" :http-request="onUpload">
                <el-button size="mini" type="primary">数据导入</el-button>
              </el-upload>
            </el-dropdown-menu>
          </el-dropdown>
      </div>
      <el-table size="mini" height="500px" v-loading="loading" :data="listData">
        <el-table-column label="绩效编号" align="center" prop="id" />
        <el-table-column label="员工姓名" align="center" prop="staffName" />
        <el-table-column show-overflow-tooltip label="评估周期" align="center" prop="name" />
        <el-table-column label="月度" align="center" prop="month" />
        <el-table-column label="自评分" align="center" prop="saMark" />
        <el-table-column label="上级评分" align="center" prop="ldMark" />
        <el-table-column label="综合得分" align="center" prop="mark" />
        <el-table-column label="等级" align="center" prop="grade" />
        <el-table-column label="绩效系数" align="center" prop="coefficient" />
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="btnDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="paginationClass">
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { listKanban,importData,delKanban} from "@/api/business/ehr/pm/kanban";
import { listDept} from "@/api/business/ehr/ehr/dept";

export default {
  name: "assess",
  dicts: ['ehr_pm_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      listData: [],
      // id集合
      ids: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params:{
          staffCodeOrName: null,
        },
        dept: null,
      },
      //部门树
      deptTree: [],
      //value
      treeValue: [],
    };
  },
  async created() {
    this.queryParams.sourceId = this.$route.query.id;
    this.getList();
    listDept().then(response => {
      let arr = response.rows;
      arr.forEach(function(element) {
        element.parent_id = element.superiorsDept;
        element.value = element.code;
        element.label = element.name;
      });
      this.deptTree = this.arrayToTree(arr,null);
    });
  },
  methods: {
    /** 查询绩效列表 */
    getList() {
      this.loading = true;
      this.queryParams.dept = this.treeValue[this.treeValue.length - 1];
      listKanban(this.queryParams).then(response => {
        this.listData = response.rows;
        this.ids = response.rows.map(item => item.id);
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    async btnSearch() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    btnResetQuery() {
      this.resetForm("queryForm");
      this.treeValue = [];
      this.queryParams.params.staffCodeOrName = null;
      this.btnSearch();
    },
    arrayToTree(data, pid) {
      let result = []
      this.getChildren(data, result, pid)
      return result
    },
    getChildren(data, result, pid) {
      for (const item of data) {
        if (item.parent_id === pid) {
          const newItem = { children: [], ...item }
          result.push(newItem)
          this.getChildren(data, newItem.children, item.code)
        }
      }
    },
    //
    handleChange(value){
      console.log("value",value);
      this.$refs.cascaderHandle.dropDownVisible = false;
    },
    //导入按钮
    btnImport(command){
      if(command == 'useImportTemplate'){
        this.useImportTemplate();
      }
      if(command == 'useImportData'){
        this.useImportData();
      }
    },
    //模板下载
    useImportTemplate(){
      this.download('ehr/pm/kanban/importTemplate', {
      }, `看板数据导入模板_${new Date().getTime()}.xlsx`)
    },
    //导入
    useImportData(){
      console.log("导入");
    },
    // 上传文件
    onUpload (file) {
      this.loading = true;
      let formData = new FormData()
      formData.append('file',file.file);
      formData.append('stageId',this.$route.query.id);
      importData(formData).then((res) => {
        if(res.code == '200'){
          this.$message.success(res.msg);
        }else{
          this.$message.success(res.msg);
        }
      }).catch((e) => {
        this.$message.error(e.message)
      }).finally((e) => {
        this.$refs['upload'].clearFiles();
        this.getList();
        this.loading = false;
      })
    },
    //返回
    btnBack(){
      this.$router.back();
    },
    //删除
    btnDel(item){
      let that = this;
      this.$modal.confirm('确认要删除员工【' + item.staffName + '】的数据吗？').then(async function() {
        that.loading = true;
        return await delKanban(item.id);
      }).then(() => {
        that.getList();
        that.$modal.msgSuccess("操作成功");
      }).catch(() => {
        that.loading = false;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
.paginationClass {
  z-index: 500;
  position: fixed;
  bottom: 10px;
  right: 10px;
  width: 100%;
  line-height: var(--footer-height);
  color: #fff;
}
</style>
