<template>
  <div>
    <div class="app-container" v-if="isList">
      <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-form-item label="状态" prop="status">
              <el-select
                size="mini"
                v-model="queryParams.status"
                placeholder=""
                clearable
              >
                <el-option
                  v-for="dict in dict.type.ehr_pm_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="月份" prop="month">
              <el-date-picker type="month" value-format="yyyy-MM" v-model="queryParams.month">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="评估人" prop="evaluatorName">
              <el-input
                v-model="queryParams.evaluatorName"
                clearable
                @keyup.enter.native="btnSearch"
              />
            </el-form-item>
            <el-form-item label="只看自己的">
              <el-checkbox v-model="queryParams.params.oneself" @change="btnOneself"></el-checkbox>
            </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-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="float:right">
        <el-badge :value="verifyCount" class="item" style="margin: 0 10px" v-if="verifyCount > 0">
          <el-button type="primary" size="mini" @click="btnPmVerify">待确认</el-button>
        </el-badge>
        <el-badge :value="markCount" class="item" style="margin: 0 10px"  v-if="markCount > 0">
          <el-button type="primary" size="mini" @click="btnPmMark">待评分</el-button>
        </el-badge>
        <el-badge :value="adjustCount" class="item" style="margin: 0 10px"  v-if="adjustCount> 0">
          <el-button type="primary" size="mini" @click="btnGradeAdjust">待分数调整</el-button>
        </el-badge>
        <el-button type="primary" size="mini" @click="btnCurrentMonth">本月绩效</el-button>
        <el-button type="primary" size="mini" @click="btnNextMonth">下月绩效</el-button>
      </div>
      <el-table size="mini" height="500px" v-loading="loading" :data="listData" @row-dblclick="btnDetails">
        <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="状态" align="center" prop="status" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.ehr_pm_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-search"
              @click="btnDetails(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>
    <detail v-model="isList" v-if="!isList" :pageStu="page" :rowId="rowId" :ids="ids" @refresh="refresh" />

    <el-dialog title="分数调整" :visible.sync="stageListOpen" width="1000px" :close-on-click-modal="false">
      <el-table size="mini" height="500px" v-loading="stageListLoading" :data="stageList" @row-dblclick="btnDetails">
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="月份" align="center" prop="month" />
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-search"
              @click="btnEntryAdjust(scope.row)"
            >进入调整</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import detail from './detail.vue'
import { listPerformance,getCurrentMonthId,getNextMonthId} from "@/api/business/ehr/pm/performance";
import { getGradeAdjustConut,toDoList} from "@/api/business/ehr/pm/gradeadjust";
import { getPmVerifyConut} from "@/api/business/ehr/pm/pmverify";
import { getPmMarkConut} from "@/api/business/ehr/pm/pmmark";
import { listDept} from "@/api/business/ehr/ehr/dept";

export default {
  name: "assess",
  dicts: ['ehr_pm_status'],
  components: {detail},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      listData: [],
      // id集合
      ids: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params:{
          dataPermission: 'Y',
          oneself: true,
          staffCodeOrName: null,
        },
        status: null,
        month: null,
        dept: null,
      },
      // 查询日期范围
      dateRange: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      //页面显示
      isList: true,
      //页面状态
      page: '',
      //详情id
      rowId: '',
      //等级调整数量
      adjustCount: null,
      //等级调整周期列表
      stageList:[],
      //等级调整周期列表弹出层
      stageListOpen:false,
      //等级调整周期列表遮罩层
      stageListLoading: true,
      //待确认数量
      verifyCount: null,
      //待评分数量
      markCount: null,
      //部门树
      deptTree: [],
      //value
      treeValue: [],
    };
  },
  async created() {
    //查询等级调整数量
    let res = await getGradeAdjustConut();
    this.adjustCount = res.row;
    //查询待确认数量
    res = await getPmVerifyConut();
    this.verifyCount = res.row;
    //查询待评分数量
    res = await getPmMarkConut();
    this.markCount = res.row;
    this.getList();
    //如果是跳转过来的页面带了参数flowId则直接进入填写页面
    if(this.$route['query'] !== undefined && this.$route.query['flowId'] !== undefined){
      this.rowId = this.$route.query.performanceId;
      this.page = 'add';
      this.ids = [this.$route.query.performanceId];
      this.isList = false;
    }
    listDept().then(response => {
      let arr = response.rows;
      arr.forEach(function(element) {
        element.parent_id = element.superiorsDept;
        element.value = element.code;
        element.label = element.name;
      });
      console.log("arr",arr);
      this.deptTree = this.arrayToTree(arr,null);
      console.log("deptTree",this.deptTree);
    });
  },
  methods: {
    /** 查询绩效列表 */
    getList() {
      this.loading = true;
      this.queryParams.dept = this.treeValue[this.treeValue.length - 1];
      listPerformance(this.addDateRange(this.queryParams, this.dateRange)).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.dateRange = [];
      this.treeValue = [];
      this.queryParams.params.staffCodeOrName = null;
      this.btnSearch();
    },
    /** 刷新 */
    refresh(){
      this.resetForm("queryForm");
      this.dateRange = [];
      this.treeValue = [];
      this.getList();
    },
    /** 进入详情 */
    btnDetails(row){
      this.rowId = row.id;
      this.page = 'add';
      this.isList = false;
    },
    //只看自己按钮
    btnOneself(){
      this.btnSearch();
    },
    //等级调整
    btnGradeAdjust(){
      this.stageListOpen = true;
      this.getlistStage();
    },
    //查看当月绩效
    async btnCurrentMonth(){
      let res = await getCurrentMonthId();
      if(!res.id){
        this.$modal.msgSuccess("未查询到本月绩效！");
      }else{
        this.rowId = res.id;
        this.page = 'add';
        this.ids = [res.id];
        this.isList = false;
      }
    },
    //查看下月绩效
    async btnNextMonth(){
      let res = await getNextMonthId();
      if(!res.id){
        this.$modal.msgSuccess("未查询到下月绩效！");
      }else{
        this.rowId = res.id;
        this.page = 'add';
        this.ids = [res.id];
        this.isList = false;
      }
    },
    //查询周期列表
    getlistStage() {
      this.stageListLoading = true;
      toDoList().then(response => {
        this.stageList = response.rows;
        this.stageListLoading = false;
      });
    },
    //进入调整
    btnEntryAdjust(row){
      this.$router.push({ name: 'AdjustGrade', query: { id: row.id,flowId: row.flowId } });
    },
    //待确认
    btnPmVerify(){
      this.$router.push({name:'PmVerify'});
    },
    //待评分
    btnPmMark(){
      this.$router.push({name:'PmMark'});
    },
    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;
    },
  }
};
</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>
