<template>
  <div class="container-accountability assessmentOfCityEdit">
    <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="考核年度">
            <el-select v-model="form.year" placeholder="请选择考核年度" disabled>
              <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="评分表名称">
            <el-select v-model="form.pfbName" disabled>
              <el-option
                :key="item.index"
                :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 {
      isSeclaration: '',
      isIndeterminate: false,
      checkAll: false,
      loadingSend: false,
      loadingSave: false,
      form: {
        year: "",
        file: "",
        source: 20,
        areaCodeList: [],
        status:""
      },
      dateyear: [],
      rules: {
        areaCodeList:[
            { required: true, message: "请勾选区", trigger: "change" }
        ]
      },
      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',
        query: {markYear: this.$route.query.markYear}
      })
    },
    //根据ID 查询评分表明细
    getPfbInfo(id){
      management.responsibilityFindByIds(id).then(res => {
        if (res.infoCode == 200) {
          this.form.pfbName=res.data.pfbmc
          this.form.year=res.data.year
          this.form.areaCodeList=res.data.shiAreaCode;
          this.getCitiesList(this.form.year);
        }else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 2000
          })
        }
     });
    },

    /**
     * 确定按钮  status 0 倮存  1  发布
     */
    save(status) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(status == 1) {
            this.loadingSend = true;
          }else {
            this.loadingSave = true;
          }
          this.form.id=this.$route.query.id
          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",
                query: {markYear: this.$route.query.markYear}
              });
            } else {
              this.$message({
                type: 'error',
                message: res.msg,
                duration: 2000
              });
              if(status == 1) {
                this.loadingSend = false;
              }else {
                this.loadingSave = false;
              }
            }
          });
        } else {
          return false;
        }
      });
    },
    getCitiesList(year) {
      let data = {
        year: year,
        type:'update',
        responsibilityId: this.$route.query.id
      };
      management
        .responsibilityFindByFindByRespResponsibilityCity(data)
        .then(res => {
          if (res.infoCode == 200) {
            const needArr = []
            this.allCities = res.data;
            res.data.lscq.map(item => {
              needArr.push(item.areaCode)
            })
            if(needArr.indexOf(this.form.areaCodeList[0]) != -1) {
              this.isIndeterminate = true
              this.cities = res.data.lscq
              this.isSeclaration = '1'
            }else {
              this.isIndeterminate = true
              this.cities = res.data.lsfcq
              this.isSeclaration = '2'
            }
          }else {
            this.$message({
              type: 'error',
              message: res.msg,
              duration: 2000
            })
          }
        });
    }
  },
  created() {
   //根据ID 查询评分表明细
    this.getPfbInfo(this.$route.query.id);
  }
};
</script>
<style lang="scss">
.assessmentOfCityEdit {
  .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>
