<template>
  <div class="container-accountability assessmentOfCityAdd">
    <el-row>
      <el-col :span="24">
        <h3 class="tit">自评部门选择</h3>
      </el-col>
    </el-row>
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-row>
        <el-col :span="8" :offset="4">
          <el-form-item label="考核年度" prop="year">
            <el-select @change="yearClick" v-model="form.year" disabled="disabled" 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="8">
          <el-form-item label="考核方式">
            <el-input
              v-model="form.khfs"
              disabled
            ></el-input>
          </el-form-item>
        </el-col> -->
      </el-row>
      <el-row>
        <el-col :span="16" :offset="4">
          <el-form-item label="评分标准" prop="khrwpfbz">
            <el-input v-model="form.khrwpfbz" type="textarea" :rows="4" disabled="disabled" resize="none"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="16" :offset="4">
          <el-form-item label="考核指标" prop="khzb">
            <el-input v-model="form.khzb" type="textarea" disabled="disabled" :rows="4" resize="none"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16" :offset="4">
          <el-form-item label="重点考核事项">
            <el-input
              v-model="form.khsx"
              type="textarea"
              :rows="4"
              disabled
              resize="none"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="16" :offset="4">
          <el-form-item label="考核内容">
            <el-input
              v-model="form.khnr"
              type="textarea"
              :rows="4"
              disabled
              resize="none"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row>
        <el-col :span="16" :offset="4">
          <el-form-item label="备注">
            <el-input
              type="textarea"
              v-model="form.pfyj"
              disabled
              resize="none"
              :rows="4"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" :offset="4">
          <el-form-item label="农业分值">
            <el-input
              v-model="form.nyfz"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="非农业分值">
            <el-input
              v-model="form.fnyfz"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" :offset='4'>
          <el-form-item label="市牵头部门">
            <el-input
              v-model="form.cityQtbm"
              disabled
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="市配合部门">
            <el-input
              v-model="form.cityPhbm"
              disabled
            ></el-input>
            <!-- <el-select
              multiple
              v-model="form.cityPhbm"
              disabled
            >
              <el-option
                v-for="item in assessmentTargetDeptName2"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select> -->
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" :offset="4">
          <el-form-item label="自评牵头部门" prop="qtbm">
            <el-select placeholder="请选择" v-model="form.qtbm" :disabled="type == 'see'" >
              <el-option v-for="item in zpbmList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="自评配合部门">
            <el-select placeholder="请选择" multiple v-model="form.phbm" :disabled="type == 'see'" >
              <el-option :key="item.id" :label="item.name" :value="item.id" v-for="item in zpbmList"></el-option>
            </el-select>
          </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; padding-bottom:64px;">
            <el-button
              type="primary"
              size='small'
              class="iconfont iconbaocun"
              :loading="loading"
              @click="save"
              v-if="type != 'see'"
            >保存</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 man from "@/api/foodSecurity";
import * as managementNew from "@/api/headResponsibility/taskAudit";
import * as validate from "@/utils/validate";
export default {
  data() {
    return {
      totalScore: "",
      form: {
        year: "",
        ids: "",
        // totalScore: 20, //分值
        khzb: "",
        khrwpfbz: "",
        phbm: [],
        qtbm: '',
        // taskName: "",
        khsx: '',
        khnr: '',
        pfyj: '',
        khfs: '',
        nyfz: '',
        fnyfz: '',
        cityQtbm: '',
        cityPhbm: ''
      },
      assessmentTargetDeptName2: [],
      dateyear: [],
      rules: {
        /* year: [
          { required: true, message: "请选择考核年度", trigger: "change" }
        ], */
        // taskName: [{ required: true, message: "请输入", trigger: "blur" }],
        // khzb: [{ required: true, message: "请输入", trigger: "blur" }],
        totalScore: [{ required: true, message: "请输入", trigger: "blur" }],
        // pfbz: [{ required: true, message: "请输入", trigger: "blur" }],
        qtbm: [
          { required: true, message: "请选择自评牵头部门", trigger: ["blur","change"] }
        ],
      },

      loading: false,
      pfbmcList: [],
      cities: [],
      id: "",
      type: "",
      zpbmList: [],
      bmList: []
    };
  },
  methods: {
    /**
     * 返回按钮
     */
    back() {
      // this.$router.back(-1);
      this.$router.push({
        path: '/accountability/taskAudit',
        query: {
          markYear: this.$route.query.markYear
        }
      })
    },

    /**
     * 倮存
     */
    save() {
      // console.log("this.weightRankList", this.form);
      let phbm = "";//配合部门
       for(let item in this.form.phbm){
        phbm += this.form.phbm[item] +","
      }

      let data = {
        ids: this.form.ids,
        // khzb: this.form.khzb,
        // pfbz: this.form.pfbz,
        phbm: phbm.substring(0,phbm.length-1),
        qtbm: this.form.qtbm,
        // taskName: this.form.taskName,
        // totalScore: this.form.totalScore,
        year: this.form.year
      };
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          managementNew.update(data).then(res => {
            if (res.infoCode == 200) {
              this.$message({
                type: 'success',
                message: '保存成功',
                duration: 2000
              });
              this.$router.push({
                path: "/accountability/taskAudit",
                query: {
                  markYear: this.$route.query.markYear
                }
              });
            } else {
              this.$message.error(res.msg);
              this.loading = false;
            }
          });
        } 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() {
      let zsZpbmWeight = this.form.zsZpbmWeight;
      if (zsZpbmWeight <= 100 && zsZpbmWeight > 0) {
        this.form.sjQtbmWeight = 100 - zsZpbmWeight;
      }
    },

    //粮食安全行政首长责任制-----考核评分权重----查询考核等级设置
    findByWeightRank(type, id) {
      management.findByWeightRank(type, id).then(res => {
        if (res.infoCode == 200) {
          this.form.weightRankList = res.data;
        }
      });
    },
    detail() {
      managementNew.see(this.id).then(res => {
        if (res.infoCode == 200) {
          setTimeout(item => {
            this.form = res.data;
            this.form.qtbm = this.form.qtbm ? this.form.qtbm : ''
            this.form.phbm = this.form.phbm ? this.form.phbm.split(",") : []
          },1000)
        }else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 2000
          })
        }
      });
    },
    getBmList() {
      man.searchBranch("1").then(res => {//自评部门下拉数据
        if (res.infoCode == 200) {
          this.bmList = res.data;
        }else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 2000
          })
        }
      });
      man.searchBranch("0").then(res => {
        if (res.infoCode == 200) {
          this.zpbmList = res.data;
        }else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 2000
          })
        }
      });
    },

    yearClick(val) {}
  },
  created() {
    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.findByWeightRank("save", "save");
    this.getBmList();
    this.detail();
  }
};
</script>
<style lang="scss">
.assessmentOfCityAdd {
  .tit {
    text-align: center;
    margin-bottom: 32px;
  }
  // .lastDialog{
  //   .el-form-item__label{
  //     line-height: 20px;
  //     padding-top: 10px;
  //   } 
  // }
  .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>
