<template>
  <div class="container-accountability assessmentOfCity">
    <h3 class="tit">下发评分表</h3>
    <el-form ref="form" :model="form" :rules="rules" label-width="95px">
      <el-row :gutter="20">
        <el-col :span="10" :offset="7">
          <el-form-item label="考核年度" prop="year">
            <el-select @change="yearClick" 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-col :span="10" :offset="7">
          <el-form-item label="评分表名称" prop="id">
            <el-select v-model="form.id" clearable>
              <el-option
                :key="item.ID"
                :label="item.PFBMC"
                :value="item.ID"
                v-for="item in pfbmcList"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10" :offset="7">
            <el-form-item label-width="10px" class="singleSel">
                <el-radio v-model="isSeclaration" label="1" @change="changeCq">粮食产区</el-radio>
                <el-radio v-model="isSeclaration" label="2" @change="changeCq">非粮食产区</el-radio>
            </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="2" :offset="7" class="selectAll">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
        </el-col>
        <el-col :span="10">
          <el-form-item prop="areaCodeList" label-width="0" class="cityStyle">
            <el-checkbox-group v-model="form.areaCodeList" @change="handleCheckedCitiesChange">
              <el-checkbox
                v-for="city in cities"
                :label="city.areaCode"
                :key="city.orgName"
              >{{city.orgName}}</el-checkbox>
            </el-checkbox-group>
          </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 iconfabu"
              :loading="loadingSend"
              @click="save(1)"
              >发布</el-button>
            <el-button
              type="primary"
              size='small'
              class="iconfont iconbaocun"
              :loading="loadingSave"
              @click="save(0)"
            >保存</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";
export default {
  data() {
    return {
      checkAll: false,
      isIndeterminate: false,
      isSeclaration: '1',
      form: {
        year: "",
        source: 20,
        areaCodeList: [],
        status:"",
        id: ''
      },
      dateyear: [],
      rules: {
        year: [
          { required: true, message: "请选择考核年度", trigger: "change" }
        ],
        id: [{ required: true, message: "请选择评分表名称", trigger: "blur" }],
        selfEvaluationTime: [
          { required: true, message: "请选择自评起止时间", trigger: "blur" }
        ],
        examinationTime: [
          { required: true, message: "请选择考核起止时间", trigger: "blur" }
        ],
        areaCodeList:[
            { required: true, message: "请勾选区", trigger: "change" }
        ]
      },
      loadingSend: false,
      loadingSave: false,
      pfbmcList: [],
      cities: [],
      allCities: []
    };
  },
  methods: {
    changeCq(val) {
      this.checkAll = false
      this.form.areaCodeList = []
      this.isIndeterminate = false
      if(val == 2) {
        this.cities = this.allCities.lsfcq
      }else {
        this.cities = this.allCities.lscq
      }
    },
    handleCheckAllChange(val) {
      let needArr = []
      this.cities.map(item => {
        needArr.push(item.areaCode)
      })
      this.form.areaCodeList = val ? needArr : [];
      this.isIndeterminate = false;
    },
    handleCheckedCitiesChange(value) {
      console.log('当前的',value)
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
    },
    /**
     * 返回按钮
     */
    back() {
      // this.$router.back(-1);
      this.$router.push({
        path: '/accountability/assessmentOfCity'
      })
    },

    /**
     * 确定按钮  status 0 倮存  1  发布
     */
    save(status) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(status == 1) {
            this.loadingSend = true;
          }else {
            this.loadingSave = true;
          }
          this.form.status=status
          management.responsibilityFindByUpdatePfb(this.form).then(res => {
            if (res.infoCode == 200) {
              this.$message({
                type: 'success',
                message: status == 1 ? '发布成功' : '保存成功',
                duration: 2000
              });
              this.$router.push({
                path: "/accountability/assessmentOfCity"
              });
            } else {
              this.$message({
                type: 'error',
                message: res.msg,
                duration: 2000
              });
              if(status == 1) {
                this.loadingSend = false;
              }else {
                this.loadingSave = false;
              }
            }
          });
        } else {
          return false;
        }
      });
    },
    getpfbmcList(year) {
      let data = {
        source: 20,
        year: year,
        type: 'xf'
      };
      management.responsibilityFindByRespResponsibilityInfo(data).then(res => {
        if (res.infoCode == 200) {
          this.pfbmcList = res.data.pfbList;
        }
      });
    },
    getCitiesList(year) {
      let data = {
        year: year,
        type:'save'
      };
      management
        .responsibilityFindByFindByRespResponsibilityCity(data)
        .then(res => {
          if (res.infoCode == 200) {
            let initArr = []
            this.allCities = res.data;
            this.cities = res.data.lscq
            //默认全选逻辑
            this.cities.map(item => {
              initArr.push(item.areaCode)
            })
            this.isSeclaration = '1'
            this.checkAll = true
            this.form.areaCodeList = initArr
          }else {
            this.$message({
              type: 'error',
              message: res.msg,
              duration: 2000
            })
          }
        });
    },
    yearClick(val) {
      this.form.areaCodeList = [];
      this.checkAll = false
      this.form.id = ''
      this.getpfbmcList(val);
      this.getCitiesList(val);
    },

      /**
       * 查询 实施细则管理  所有已发布或可选的年分
       */
      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];
            this.getpfbmcList(this.form.year);
            this.getCitiesList(this.form.year);
          }
        }
      });
    }
  },
  created() {
    this.findByYear('page');
  }
};
</script>
<style lang="scss">
.assessmentOfCity {
  .tit {
    text-align: center;
    margin-bottom: 32px;
  }
  .el-select {
    display: block;
  }
  .el-date-editor {
    width: 100%;
  }
  .el-date-editor .el-range-separator {
    padding: 0 0;
  }
  .selectAll .el-checkbox {
    margin-left: 12px !important;
  }
  .selectAll.el-col-2 {
    width: auto;
  }
  .cityStyle {
    .el-form-item__content {
      line-height: 22px;
    }
    .el-form-item__error {
      left: 20px;
    }
    .el-checkbox {
      margin-right: 0;
      margin-left: 15px;
    }
  }
  .singleSel{
    &.el-form-item {
      margin-bottom: 10px;
    }
    .el-form-item__content {
      line-height: 20px;
    }
  }
}
</style>
