<template>
  <div class="container-accountability scoringWeightCityAdd">
    <h3 class="titH3">考核年度</h3>
    <el-form ref="form" :model="form" :rules="rules" label-width="230px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="考核年度" prop="year">
            <el-select v-model="form.year" placeholder="请选择考核年度" clearable disabled></el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="titH3">自评起止时间</h3>
      <el-row>
        <el-col :span="10">
          <el-form-item label="配合部门" prop="sjPhbmPsTime">
            <el-date-picker
              v-model="form.sjPhbmPsTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width:100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="牵头部门" prop="sjQtbmPsTime">
            <el-date-picker
              v-model="form.sjQtbmPsTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width:100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="titH3">考核起止时间</h3>
      <el-row>
        <el-col :span="10">
          <el-form-item label="配合部门" prop="sjPhbmTime">
            <el-date-picker
              v-model="form.sjPhbmTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width:100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="牵头部门" prop="sjQtbmTime">
            <el-date-picker
              v-model="form.sjQtbmTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width:100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="titH3">考核评分权重设置</h3>
      <el-row>
        <el-col :span="10">
          <el-form-item label="自评权重(%)" prop="zsZpbmWeight">
            <el-input v-model="form.zsZpbmWeight" type="text" clearable @input="sumWeight"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="考核权重(%)" prop="sjQtbmWeight">
            <el-input v-model="form.sjQtbmWeight" type="text" @input="sumWeight" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="抽查权重(%)">
            <el-input v-model="form.sjQtbmFpWeight" type="text" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <h3 class="titH3">考核等级设置</h3>
      <el-row>
        <el-col :span="20">
          <el-form-item label="">
            <el-table
              :data="form.weightRankList"
              border
              stripe
              highlight-current-row
              style="margin-top: 0px;"
            >
              <el-table-column prop="rankName" label="等级名称" align="center" :show-overflow-tooltip="true"></el-table-column>
              <el-table-column label="分值区间" align="center" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <el-form-item :prop="`weightRankList.${scope.$index}.fzStart`" :rules="rules.fzStart" class="tableStyle">
                    <el-input v-model="scope.row.fzStart"  type="text" size="mini" maxlength="3" clearable></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="分值区间" align="center" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <el-form-item :prop="`weightRankList.${scope.$index}.fzEnd`" :rules="rules.fzEnd" class="tableStyle">
                    <el-input v-model="scope.row.fzEnd" type="text" size="mini" maxlength="3" clearable></el-input>
                  </el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
          <!-- <table border="1" class="tabStyle">
            <tr>
              <td class="tabTit">等级名称</td>
              <td class="tabTit" colspan="2">分值区间</td>
            </tr>
            <tr v-for="(weightRank,index) in form.weightRankList" :key="index">
              <td class="tabTit">{{weightRank.rankName}}</td>
              <td>
                <el-input v-model="weightRank.fzStart" type="text" size="mini"></el-input>
              </td>
              <td>
                <el-input v-model="weightRank.fzEnd" type="text" size="mini"></el-input>
              </td>
            </tr>
          </table> -->
        </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="update"
            >保存</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/scoringWeight";
import * as validate from "@/utils/validate";
export default {
  data() {
    var validatorSum1 = (rule, value, callback) => {//考核权重校验
      // console.log('pppppp',Number(value)+Number(this.form.zsZpbmWeight))
      if(Number(value)+Number(this.form.zsZpbmWeight) > 100){
        callback(new Error("自评权重和考核权重之和不能大于100"));
      }else {
        callback()
      }
    };
    var validatorSum2 = (rule, value, callback) => {//自评权重校验
    // console.log('hhhhhh',Number(value)+Number(this.form.sjQtbmWeight))
      if(Number(value)+Number(this.form.sjQtbmWeight) > 100){
        callback(new Error("自评权重和考核权重之和不能大于100"));
      }else {
        callback()
      }
    };
    return {
      form: {
        year: "",
        source: 20,
        sjQtbmWeight: "",
        sjQtbmFpWeight: "",
        zsZpbmWeight: '',
        weightRankList: [],
        sjQtbmPsTime: [],
        sjPhbmPsTime: []
      },
      dateyear: [],
      rules: {
        year: [
          { required: true, message: "请选择考核年度", trigger: "change" }
        ],
        sjPhbmTime: [
          { required: true, message: "请选择配合部门起止时间", trigger: "blur" }
        ],
        sjQtbmTime: [
          { required: true, message: "请选择牵头部门起止时间", trigger: "blur" }
        ],
        sjQtbmPsTime: [
          { required: true, message: "请选择牵头部门起止时间", trigger: "blur" }
        ],
        sjPhbmPsTime: [
          { required: true, message: "请选择配合部门起止时间", trigger: "blur" }
        ],
        sjQtbmWeight: [
          {
            required: true,
            message: "考核权重不能为空",
            trigger: "blur"
          },
          { trigger: ["blur", "change"], validator: validate.validateNumber },
          {trigger: ['blur', 'change'], validator: validatorSum1}
        ],
        zsZpbmWeight: [
          {
            required: true,
            message: "自评权重不能为空",
            trigger: "blur"
          },
          { trigger: ["blur", "change"], validator: validate.validateNumber },
          {trigger: ['blur', 'change'], validator: validatorSum2}
        ],
        fzStart: [
          { required: true, message: "请输入分值", trigger: "blur" },
          { validator: validate.isPositiveInteger, trigger: "blur" }
        ],
        fzEnd: [
          { required: true, message: "请输入分值", trigger: "blur" },
          { validator: validate.isPositiveInteger, trigger: "blur" }
        ]
      },
      loading: false
    };
  },
  methods: {
    /**
     * 返回按钮
     */
    back() {
      this.$router.back(-1);
    },
    update() {
      // console.log("this.weightRankList",this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          let needArr = this.form.weightRankList
          let score1 = Number(needArr[0].fzEnd)
          let score2 = Number(needArr[0].fzStart)
          let score3 = Number(needArr[1].fzEnd)
          let score4 = Number(needArr[1].fzStart)
          let score5 = Number(needArr[2].fzEnd)
          let score6 = Number(needArr[2].fzStart)
          let score7 = Number(needArr[3].fzEnd)
          let score8 = Number(needArr[3].fzStart)
          if(
            score1 > score2 && score2 > score3 && score3 > score4 && score4 > score5 && score5 > score6 && score6 > score7 && score7 > score8
            // score1 > score2 > score3 > score4 > score5 > score6 > score7 > score8
          ) {
            this.loading = true;
            this.form.id = this.$route.query.id;
            this.form.sjPhbmStartTime = this.form.sjPhbmTime[0];//市考核起止时间配合部门开始时间
            this.form.sjPhbmEndTime = this.form.sjPhbmTime[1];//市考核起止时间配合部门结束时间
            this.form.sjQtbmStartTime = this.form.sjQtbmTime[0];//市考核起止时间牵头部门开始时间
            this.form.sjQtbmEndTime = this.form.sjQtbmTime[1];//市考核起止时间牵头部门结束时间
            this.form.sjQtbmPsStartTime = this.form.sjQtbmPsTime[0]//区自评起止时间牵头部门开始时间
            this.form.sjQtbmPsEndTime = this.form.sjQtbmPsTime[1]//区自评起止时间牵头部门结束时间
            this.form.sjPhbmPsStartTime = this.form.sjPhbmPsTime[0]//区自评起止时间配合部门开始时间
            this.form.sjPhbmPsEndTime = this.form.sjPhbmPsTime[1]//区自评起止时间配合部门结束时间
            management.update(this.form).then(res => {
              if (res.infoCode == 200) {
                this.$message({
                    message: '保存成功',
                    type: 'success',
                    duration: 2000
                })
                this.$router.push({
                  path: "/accountability/scoringWeight"
                });
              } else {
                this.$message({
                    message: res.msg,
                    type: 'error',
                    duration: 2000
                })
                this.loading = false;
              }
            });
          }else {
            this.$message({
                message: '请输入正确的分值区间',
                type: 'error',
                duration: 2000
            })
          }
        } else {
          return false;
        }
      });
    },

    findByYear(type) {
      management.responsibilityFindByYear(type).then(res => {
        if (res.infoCode == 200) {
          this.dateyear = res.data;
          if (this.dateyear != null) {
            this.form.year = this.dateyear[0];
          }
        }
      });
    },

    //考核评分权重设置 计算分值
    sumWeight(){
      if(this.form.sjQtbmWeight == '' || this.form.zsZpbmWeight == '') {
        this.form.sjQtbmFpWeight = ''
        return
      }
      const reg = /(^[0-9](\d+)?(\.\d{1,2})?$)|(^[1-9]$)|(^\d\.[1-9]{1,2}$)|(^\d\.[0]{1}[1-9]{1}$|(^\d\.[1-9]{1}[0]{1}$)$)/;
      if( reg.test(this.form.sjQtbmWeight) && reg.test(this.form.zsZpbmWeight)){
        this.form.sjQtbmFpWeight=100-this.form.sjQtbmWeight-this.form.zsZpbmWeight;
      }
    },
    //粮食安全行政首长责任制-----考核评分权重----查询考核等级设置
    findByWeightRank(type, id) {
      management.findByWeightRank(type, id).then(res => {
        if (res.infoCode == 200) {
          this.form.weightRankList = res.data;
        }
      });
    },

    // 粮食安全行政首长责任制-----考核评分权重----根据id查询考核内容--明细
    findById(id) {
      management.findById(id).then(res => {
        if (res.infoCode == 200) {
          this.form = res.data;
        }
      });
    }
  },
  created() {
    this.findById(this.$route.query.id);
  }
};
</script>
<style lang="scss">
.scoringWeightCityAdd {
  .el-select {
    display: block;
  }
  .el-date-editor {
    width: 100%;
  }
  .el-date-editor .el-range-separator {
    padding: 0 0;
  }
  .el-checkbox {
    margin-left: 20px;
  }
  .tabStyle {
    width: 100%;
    height: 100%;
    border-collapse: collapse;
    border-spacing: 0;
    font-size: 16px;
    table-layout: fixed;
    border: 1px solid #ccc;
    text-align: center;
    margin: 20px 0 20px 200px;
    td {
      padding: 5px;
    }
    .tabTit {
      padding: 10px;
    }
  }
  .tableStyle.el-form-item{
    margin-bottom: 14px;
    .el-form-item__error {
      padding-top: 0px;
    }
  }
}
</style>
