<template>
  <div class="container-accountability managementAdd" style="overflow-x:hidden">
    <h3 class="tit">复制评分表</h3>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="10" :offset="7">
          <el-form-item label="考核年度" prop="year">
            <el-select v-model="form.year" placeholder="请选择考核年度" @change="yearChange" clearable>
              <el-option :key="item.index" :label="item" :value="item" v-for="item in dateyear"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="7">
          <el-form-item label="原评分表名称" prop="idNew">
            <el-select v-model="form.idNew" clearable>
              <el-option
                :key="item.index"
                :label="item.pfbmc"
                :value="item.pfbmc"
                v-for="item in pfbmcList"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="10" :offset="7">
          <el-form-item label="新考核年度" prop="yearNew">
            <el-select v-model="form.yearNew" placeholder="请选择考核年度" clearable>
              <el-option :key="item.index" :label="item" :value="item" v-for="item in dateyearNew"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="7">
          <el-form-item label="新评分表名称" prop="id">
            <el-input v-model="form.id" type="text" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <div style="width: 80%; margin: 0 auto; text-align: center; ">
            <el-button
              type="primary"
              size='small'
              class="iconfont iconbaocun"
              :loading="loading"
              @click="save"
            >保存</el-button>
            <el-button
              type="primary"
              size='small'
              class="iconfont iconfanhui"
              @click="back"
            >返回</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import * as management from "@/api/headResponsibility/management";
import * as validate from "@/utils/validate";
export default {
  data() {
    // 根据评分表名称和年分查询数据是否存在
    const findByPfbmc = (rule, value, callback) => {
      if (!value) {
        return callback()
      } else {
        const data = {
          pfbmc: value,
          year: this.form.yearNew
        }
        if(this.form.yearNew) {
          management.findByPfbmc(data).then(res => {
            if (res.infoCode==200) {
              callback()
            } else {
              return callback(new Error(res.msg))
            }
          })
        }
      }
    }
    return {
      pfbmcList: [],
      form: {
        year: "",
        id: "",
        yearNew: "",
        idNew: ""
      },
      dateyear: [],
      dateyearNew: [],
      rules: {
        year: [
          { required: true, message: "请选择考核年度", trigger: "change" }
        ],
        yearNew: [
          { required: true, message: "请选择考核年度", trigger: "change" }
        ],
        idNew: [
          { required: true, message: "请选择评分表名称", trigger: "blur" }
        ],
        id: [
          { required: true, message: "请输入评分表名称", trigger: "change" },
          {trigger: ['blur', 'change'], validator: findByPfbmc},
          { validator: validate.validateFive, trigger: "blur" }
        ]
      },
      fileList: [],
      loading: false
    };
  },
  methods: {
    /**
     * 返回按钮
     */
    back() {
      this.$router.back(-1);
    },
    yearChange(){
      this.form.idNew = ''//原评分表名称
      this.form.id = ''//新评分表名称
      this.form.yearNew = ''//新考核年度
      this.getpfbmcList(this.form.year)
    },
    getpfbmcList(year) {
      let data = {
        source: 20,
        year: year,
        type: 'copy'
      };
      management.responsibilityFindByRespResponsibilityInfo(data).then(res => {
        if (res.infoCode == 200) {
          this.pfbmcList = res.data.pfbList;
        }
      });
    },
    /**
     * 保存按钮
     */
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let data = {
            oldPfbmc: this.form.idNew,
            newYear: this.form.yearNew,
            newPfbmc: this.form.id,
            oldYear: this.form.year
          };
          this.loading = true;
          management.copyPfb(data).then(res => {
            if (res.infoCode == 200) {
              this.$message({
                type: 'success',
                message: '保存成功',
                duration: 2000
              });
              this.$router.push({
                path: "/accountability/management"
              });
            } else if (res.infoCode == 504) {
              this.$message.warning(res.msg);
              this.loading = false;
            } else {
              this.$message.error(res.msg);
              this.loading = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    findNewYear(curYear) {
      management.newExamineYear().then(res => {
        if (res.infoCode == 200) {
          this.dateyear = res.data.khndList
          this.dateyearNew = res.data.newKhnfList
        }else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 2000
          })
        }
      });
    },
  },
  created() {
    this.findNewYear()
  }
};
</script>
<style lang="scss">
.managementAdd {
  .tit {
    text-align: center;
    margin-bottom: 32px;
  }
  .el-select {
    display: block;
  }
}
</style>
