<template>
  <div class="container-accountability assessmentOfCityAdd">
    <el-row>
      <el-col :span="24">
        <h3 class="titH3">考核年度</h3>
      </el-col>
    </el-row>
    <el-form ref="form" :model="form" :rules="rules" label-width="195px">
      <el-row>
        <el-col :span="10" :offset="7">
          <el-form-item label="考核年度" prop="year">
            <el-select
              @change="yearClick"
              disabled="disabled"
              v-model="form.year"
              placeholder="请选择考核年度"
              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-row>
      <el-row>
        <el-col :span="24">
          <h3 class="titH3">考核时间设置</h3>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10" :offset="7">
          <el-form-item label="市级牵头部门考核起止时间">
            <el-date-picker
              v-model="sjQtTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              disabled="disabled"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10" :offset="7">
          <el-form-item label="市级配合部门考核起止时间">
            <el-date-picker
              v-model="sjPhTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              disabled="disabled"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10" :offset="7">
          <el-form-item label="区级自评部门考核起止时间" prop="sjZpbmTime">
            <el-date-picker
              v-model="form.sjZpbmTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :disabled="type == 'see'"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10" :offset="7">
          <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="结束日期"
              :disabled="type == 'see'"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10" :offset="7">
          <el-form-item label="区级配合部门考核起止时间" prop="sjPhbmTime">
            <el-date-picker
              v-model="form.sjPhbmTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              :disabled="type == 'see'"
              end-placeholder="结束日期"
              style="width: 100%;"
            ></el-date-picker>
          </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
              v-if="type != 'see'"
              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/scoringWeight";
import * as managementNew from "@/api/headResponsibility/taskReporting";
import * as validate from "@/utils/validate";
export default {
  data() {
    var validatorBlur = (rule, value, callback) => {
      if (this.sjPhTime.length > 0) {
        let phTimeStart = new Date(this.sjPhTime[0].substring(0, 10)).getTime();
        let zptimeStart = new Date(value[0]).getTime();
        let phTimeEnd = new Date(this.sjPhTime[1].substring(0, 10)).getTime();
        let zpTimeEnd = new Date(value[1]).getTime();
        if (phTimeStart > zptimeStart) {
          callback(
            new Error(
              "区级自评部门考核开始时间不能小于市级配合部门考核开始时间"
            )
          );
        } else if (phTimeEnd < zpTimeEnd) {
          callback(
            new Error(
              "区级自评部门考核结束时间不能大于市级配合部门考核结束时间"
            )
          );
        } else {
          callback();
        }
      }
    };
    var validatorBlur2 = (rule, value, callback) => {
      if (this.sjPhTime.length > 0) {
        let phTimeStart = new Date(this.sjPhTime[0].substring(0, 10)).getTime();
        let zptimeStart = new Date(value[0]).getTime();
        let phTimeEnd = new Date(this.sjPhTime[1].substring(0, 10)).getTime();
        let zpTimeEnd = new Date(value[1]).getTime();
        if (phTimeStart > zptimeStart) {
          callback(
            new Error(
              "区级牵头部门考核开始时间不能小于市级配合部门考核开始时间"
            )
          );
        } else if (phTimeEnd < zpTimeEnd) {
          callback(
            new Error(
              "区级牵头部门考核结束时间不能大于市级配合部门考核结束时间"
            )
          );
        } else {
          callback();
        }
      }
    };
    var validatorBlur3 = (rule, value, callback) => {
      if (this.sjPhTime.length > 0) {
        let phTimeStart = new Date(this.sjPhTime[0].substring(0, 10)).getTime();
        let zptimeStart = new Date(value[0]).getTime();
        let phTimeEnd = new Date(this.sjPhTime[1].substring(0, 10)).getTime();
        let zpTimeEnd = new Date(value[1]).getTime();
        if (phTimeStart > zptimeStart) {
          callback(
            new Error(
              "区级配合部门考核开始时间不能小于市级配合部门考核开始时间"
            )
          );
        } else if (phTimeEnd < zpTimeEnd) {
          callback(
            new Error(
              "区级配合部门考核结束时间不能大于市级配合部门考核结束时间"
            )
          );
        } else {
          callback();
        }
      }
    };
    return {
      sjQtTime: [],
      sjPhTime: [],
      totalScore: "",
      form: {
        year: "",
        file: "",
        source: 20,
        areaCodeList: [],
        status: "",
        sjQtbmWeight: "",
        weightRankList: {},
        zsTime: [],
        sjPhbmTime: [],
        sjQtbmTime: [],
        sjZpbmTime: []
      },

      dateyear: [],
      rules: {
        year: [
          { required: true, message: "请选择考核年度", trigger: "change" }
        ],
        sjZpbmTime: [
          {
            required: true,
            message: "请选择区级自评部门考核起止时间",
            trigger: "change"
          },
          { validator: validatorBlur, trigger: "change" }
        ],
        sjPhbmTime: [
          {
            required: true,
            message: "请选择区级配合部门考核起止时间",
            trigger: "change"
          },
          { validator: validatorBlur3, trigger: "change" }
        ],
        sjQtbmTime: [
          {
            required: true,
            message: "请选择区级牵头部门考核起止时间",
            trigger: "change"
          },
          { validator: validatorBlur2, trigger: "change" }
        ]
      },

      loading: false,
      pfbmcList: [],
      cities: [],
      id: "",
      type: ""
    };
  },
  methods: {
    /**
     * 返回按钮
     */
    back() {
      this.$router.back(-1);
    },

    /**
     * 保存按钮
     */
    save() {
      // console.log("this.weightRankList", this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.form.sjZpbmStartTime = this.form.sjZpbmTime[0];
          this.form.sjZpbmEndTime = this.form.sjZpbmTime[1];
          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];
          managementNew.save(this.form).then(res => {
            if (res.infoCode == 200) {
              this.$message({
                type: 'success',
                message: '保存成功',
                duration: 2000
              });
              this.$router.push({
                path: "/accountability/taskReporting"
              });
            } else {
              this.$message.error(res.msg);
              this.loading = false;
            }
          });
        } else {
          return false;
        }
      });
    },
    quxiao() {
      this.$router.push({
        path: "/accountability/taskReporting"
      });
    },

    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];
          // }
        }
      });
    },

    //粮食安全行政首长责任制-----考核评分权重----查询考核等级设置
    findByWeightRank(type, id) {
      management.findByWeightRank(type, id).then(res => {
        if (res.infoCode == 200) {
          this.form.weightRankList = res.data;
        }
      });
    },
    detail() {
      managementNew.detail(this.id).then(res => {
        this.loading = false;
        if (res.infoCode == 200) {
          // console.log(res.data.year);
          this.form = res.data;
          // this.form.year = res.data.year;
          management.findByYearAndStatus(res.data.year, 1).then(res => {
            if (res.infoCode == 200) {
              if (res.data.sjQtbmStartTime) {
                this.sjQtTime.push(res.data.sjQtbmStartTime);
                this.sjQtTime.push(res.data.sjQtbmEndTime);
              }
              if (res.data.sjPhbmStartTime) {
                this.sjPhTime.push(res.data.sjPhbmStartTime);
                this.sjPhTime.push(res.data.sjPhbmEndTime);
              }
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error(res.msg);
        }
      });
    },

    yearClick(val) {}
  },
  created() {
    this.form.totalScore = 100;
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
    }
    if (this.$route.query.type) {
      this.type = this.$route.query.type;
      // console.log(this.type)
    }
    this.findByYear("save");
    this.detail();
    // this.findByWeightRank("save", "save");
  }
};
</script>
<style lang="scss">
.assessmentOfCityAdd {
  .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-bottom: 20px;
    margin-top: 20px;
    td {
      padding: 5px;
    }
    .tabTit {
      padding: 10px;
    }
  }
}
</style>
