<template>
  <div class="container-accountability managementAdd">
    <h3 class="tit">导入评分表</h3>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="10" :offset="7">
          <el-form-item label="考核年度" prop="year">
            <el-select 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="总分">
            <el-input v-model="weightDO.totalScore" type="text" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="7">
          <el-form-item label="评分表名称" prop="pfbmc">
            <el-input v-model="form.pfbmc" type="text"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="7">
          <el-form-item label="文件">
            <el-row>
              <el-col :span="12">
                <el-upload
                  class="upload-demo"
                  action
                  :on-change="handleChange"
                  :on-remove="handleRemove"
                  :file-list="fileList"
                  :auto-upload="false"
                  :on-exceed="handleExceed"
                  :limit="1"
                >
                  <el-button type="primary" class="outCancelButton">点击上传</el-button>
                </el-upload>
              </el-col>
              <!--<el-col :span="12">-->
                <!--<el-button type="primary" class="outCancelButton"></el-button>-->
              <!--</el-col>-->
            </el-row>
          </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
              style="margin: 0 50px;"
              type="primary"
              class="outCancelButton"
              :loading="loading"
              @click="save()"
            >确定</el-button>
            <el-button
              style="margin: 0 50px;"
              type="primary"
              class="outCancelButton"
              @click="back"
            >取消</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import * as management from "@/api/headResponsibility/management";
import * as scoringWeight from "@/api/headResponsibility/scoringWeight";
import * as validate from "@/utils/validate";
export default {
  data() {
      // 根据评分表名称和年分查询数据是否存在
      const findByPfbmc = (rule, value, callback) => {
        if (!value) {
          return callback()
        } else {
          const data = {
            pfbmc: value,
            year: this.form.year
          }
           management.findByPfbmc(data).then(res => {
            if (res.infoCode==200) {
            callback()
          } else {
            return callback(new Error(res.msg))
          }
        })
      }
    }

    return {
      form: {
        year:"",
        file: "",
        source: 20,
      },
      weightDO:{},
      dateyear: [],
      rules: {
        year: [
          { required: true, message: "请选择考核年度", trigger: "change" }
        ],
        totalScore: [
          { required: true, message: "请输入总分", trigger: "blur" },
          { validator: validate.validateCheckRate, trigger: "blur" }
        ],
        pfbmc: [
          { required: true, message: "请输入评分表名称", trigger: "blur" },
          {trigger: ['blur', 'change'], validator: findByPfbmc},
          { validator: validate.validateFive, trigger: "blur" }
        ]
      },
      fileList: [],
      loading: false
    };
  },
  methods: {
    /**
     * 上传文件 start
     */
    handleExceed(files, fileList) {
      this.$message.warning(`上传文件超出最大个数，当前只允许上传1个文件`);
    },
    handleChange(file, fileList) {
      this.form.file = file.raw;
    },
    handleRemove(file, fileList) {
      this.form.file = "";
    },
    /**
     * 上传文件 end
     */
    /**
     * 返回按钮
     */
    back() {
      this.$router.back(-1);
    },
    /**
     * 确定按钮
     */
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.form.file == "") {
            this.$message.warning("请选择上传文件!");
            this.loading = false;
          } else {
            this.form.totalScore=this.weightDO.totalScore;
            const formData = new FormData();
            formData.append("file", this.form.file);
            management.responsibilityUpload(this.form, formData).then(res => {
              this.loading = false;
              if (res.infoCode == 200) {
                this.$message.success(res.msg);
                this.$router.push({
                  path:"/accountability/management"
                });
              } else if (res.infoCode == 504) {
                this.$message.warning(res.msg);
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
    },

      /**
       * 查询 实施细则管理  所有已发布或可选的年分
       */
      findByYearAndStatus(year,status){
        scoringWeight.findByYearAndStatus(year,status).then(res => {
          if (res.infoCode == 200) {
            this.weightDO=res.data;
            console.log("this.weightDO",this.weightDO);
          }
         });
      },


      /**
       * 查询 实施细则管理  所有已发布或可选的年分
       */
      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.findByYearAndStatus(this.form.year,1);
          }
        }
      });
    }
  },
  created() {
  this.findByYear('page');
  }
};
</script>
<style lang="scss">
.managementAdd {
  .tit {
    text-align: center;
    margin-bottom: 32px;
  }
  .el-select {
    display: block;
  }
}
</style>
