<template>
  <div>
    <div slot="header" class="clearfix">
      <span style="font-weight:bold">分数调整</span>
      <div style="float: right; padding: 3px 0" type="text">
        <el-button size="mini" plain @click="btnBack">返回</el-button>
      </div>
    </div>
    <div class="app-container" v-if="isList">
      <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" @submit.native.prevent>
        <el-form-item label="员工" prop="staffName">
          <el-input
            v-model="queryParams.staffName"
            clearable
            @keyup.enter.native="btnSearch"
          />
        </el-form-item>
        <el-form-item>
          <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-form>
      <div style="float:right">
        <el-button-group>
          <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>
        </el-button-group>
      </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 label="评估周期" align="center" prop="name" />
        <el-table-column label="月度" align="center" prop="month" />
        <el-table-column label="权重" align="center" prop="weight" />
        <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="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="btnAdjustMark(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>

    <el-dialog title="分数调整" :visible.sync="formOpen" width="1000px" :close-on-click-modal="false" :show-close="false">
      <el-descriptions :column="1">
        <el-descriptions-item label="员工编号">{{form.staff}}</el-descriptions-item>
        <el-descriptions-item label="员工姓名">{{form.staffName}}</el-descriptions-item>
        <el-descriptions-item label="自评分">{{form.saMark}}</el-descriptions-item>
        <el-descriptions-item label="上级评分">{{form.ldMark}}</el-descriptions-item>
        <el-descriptions-item label="调整后分数">{{ajaMark}}</el-descriptions-item>
      </el-descriptions>
      <el-form size="mini" label-position="right" ref="form" :model="adjustlogForm" :rules="formRules" label-width="80px">
        <el-form-item label="调整分" prop="ajMark">
          <el-input size="mini" type="number" v-model="adjustlogForm.ajMark" v-only-number="{max:100,min:-100,precision:1}"/>
        </el-form-item>
        <el-form-item label="调整原因" prop="ajCause">
          <el-input v-model="adjustlogForm.ajCause"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="btnClose">取 消</el-button>
        <el-button size="mini" type="primary" @click="btnConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listMarkAdjust,getPerformance} from "@/api/business/ehr/pm/markadjust";
import { addAdjustlog,importData} from "@/api/business/ehr/pm/adjustlog";

export default {
  name: "AdjustMark",
  dicts: ['ehr_pm_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      listData: [],
      //绩效表单数据
      form:{},
      //调整记录表单数据
      adjustlogForm:{},
      //是否显示行明细弹出层
      formOpen:false,
      //当行明细校验规则
      formRules:{
        ajMark: [
          { required: true, message: "调整分不能为空", trigger: "blur" },
        ],
        ajCause: [
          { required: true, message: "调整原因不能为空", trigger: "blur" },
        ],
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sourceId: null,
        staffName: null,
      },
      //页面显示
      isList: true,
      //页面状态
      page: '',
      //详情id
      rowId: '',
    };
  },
  computed: {
    //调整后分数
    ajaMark: function () {
      console.log('this.form.mark',this.form.mark);
      console.log('this.adjustlogForm.ajMark',this.adjustlogForm.ajMark);
      console.log(this.form.mark - this.adjustlogForm.ajMark);
      return Number(this.form.mark)+Number(this.adjustlogForm.ajMark || 0);
    },
	},
  created() {
    this.queryParams.sourceId = this.$route.params.id;
    this.getList();
  },
  methods: {
    /** 查询绩效列表 */
    getList() {
      this.loading = true;
      listMarkAdjust(this.queryParams).then(response => {
        this.listData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    btnSearch() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    btnResetQuery() {
      this.resetForm("queryForm");
      this.btnSearch();
    },
    /** 刷新 */
    refresh(){
      this.resetForm("queryForm");
      this.getList();
    },
    //调整分数
    async btnAdjustMark(row){
      await getPerformance(row.id).then(res => {
        if (res.code === 200) {
          this.form = res.data
          this.formOpen = true;
         }
      })
    },
    //取消
    btnClose(){
      this.form = {};
      this.adjustlogForm = {};
      this.formOpen = false;
    },
    //确定
    btnConfirm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.adjustlogForm.performanceId = this.form.id;
          addAdjustlog(this.adjustlogForm).then(response => {
            this.$modal.msgSuccess("保存成功");
            this.adjustlogForm = {};
            this.formOpen = false;
            this.getList();
          });
        }
      });
    },
    //返回
    btnBack(){
      this.$router.back();
    },
    //导入按钮
    btnImport(command){
      if(command == 'useImportTemplate'){
        this.useImportTemplate();
      }
      if(command == 'useImportData'){
        this.useImportData();
      }
    },
    //模板下载
    useImportTemplate(){
      this.download('ehr/pm/adjustlog/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.params.id);
      importData(formData).then((res) => {
        console.log('res',res);
        if(res.code == '200'){
          this.$message.success(res.msg);
        }else{
          this.$message.success(res.msg);
        }
      }).catch((e) => {
        console.log('e',e);
      }).finally((e) => {
        this.$refs['upload'].clearFiles();
        this.getList();
        this.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>
