<template>
  <div class="container-accountability managementAdd" style="overflow-x:hidden">
    <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="评分表名称" prop="pfbmc">
            <el-input v-model="form.pfbmc" type="text" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="7">
          <el-form-item label="文件" prop="">
            <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-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 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/management";
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,
      },
      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 {
            let formData = new FormData();
            formData.append("file", this.form.file);
            management.importExcel(this.form, formData).then(res => {
              this.loading = false;
              if (res.infoCode == 200) {
                this.$message({
                  type: 'success',
                  message: '保存成功',
                  duration: 2000
                });
                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;
        }
      });
    },

    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];

          }
        }
      });
    },
    
  },
  created() {
    this.findByYear('save');
  }
};
</script>
<style lang="scss">
.managementAdd {
  .tit {
    text-align: center;
    margin-bottom: 32px;
  }
  .el-select {
    display: block;
  }
}
</style>
