<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>2️⃣模板导入与处理</span>
    </div>
    <el-form
      ref="$form"
      :model="formData"
      label-position="right"
      label-width="100px"
      size="small"
      :rules="rules"
      :disabled="templateImportDisabled"
    >
      <el-form-item prop="templateSheetNum" label="模板所在sheet页">
        <el-input v-model="formData.templateSheetNum" style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="uploadTemplate" label="模板上传">
        <el-upload
          :action="'/api/excel/template?templateSheetNum=' + formData.templateSheetNum"
          :multiple="false"
          :on-success="uploadTemplateSuccess"
          :file-list="templateFileList"
        >
          <el-button type="primary" size="small">点击上传</el-button>
        </el-upload>
      </el-form-item>

      <el-form-item prop="cellNum" label="指定单元格">
        <el-input v-model="formData.cellNum" style="width=200px" :disabled="cellSearchDisable">
          <el-button
            slot="append"
            icon="el-icon-search"
            :disabled="cellSearchDisable"
            @click="cellNumSearch"
          ></el-button>
        </el-input>
      </el-form-item>
      <el-form-item prop="cellData" label="原始数据">
        <el-input
          type="textarea"
          v-model="formData.cellData"
          :disabled="cellSearchDisable"
          readonly
          :rows="5"
        ></el-input>
      </el-form-item>
      <el-form-item prop="newCellData" label="替换后的数据">
        <el-input
          type="textarea"
          v-model="formData.newCellData"
          :disabled="cellSearchDisable"
          :rows="5"
        ></el-input>
      </el-form-item>
      <el-form-item prop="resultExcelName" label="处理后Excel的名字">
        <el-input v-model="formData.resultExcelName"></el-input>
      </el-form-item>

      <el-button type="primary" class="f-right" @click="gotoDownload">去下载文件</el-button>
      <el-button type="primary" class="f-right right-10" @click="executeTemplate">生成Excel</el-button>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "TemplateImport",
  props: {
    templateImportDisabled: {
      type: Boolean,
      require: true,
      default: false
    }
  },
  components: {},
  data() {
    return {
      formData: {
        // 模板所在sheet页
        templateSheetNum: "1",
        cellNum: "",
        cellData: "",
        newCellData: ""
      },
      rules: {},
      templateFileList: [],
      cellSearchDisable: false,
      resultExcelName: ""
    };
  },
  computed: {},
  watch: {},
  methods: {
    uploadTemplateSuccess(response, file, fileList) {
      if (response.success) {
        this.$message.success("模板导入成功");
        this.cellSearchDisable = false;
      } else {
        this.$message.success("模板导入出现了错误，请联系陈达解决");
      }
    },
    cellNumSearch() {
      this.$axios
        .get("/api/excel/cellNumSearch", {
          params: {
            cellRowStr: this.formData.cellNum
          }
        })
        .then(res => {
          const { data } = res;
          if (data.success) {
            this.$message.success("单元格数据获取成功");
            this.formData.cellData = data.data;
          } else {
            this.$message.error(data.msg);
            this.formData.cellData = "";
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    executeTemplate() {
      // this.active++;
      // let a = document.createElement("a"); //创建一个a标签元素
      // a.style.display = "none"; //设置元素不可见
      // a.target = "_blank";
      // a.href = `/api/excel/executeTemplate?newCellData=${encodeURIComponent(
      //   this.model.newCellData
      // )}&cellRowStr=${this.formData.cellNum}`;
      // document.body.appendChild(a); //加入
      // a.click(); //触发点击,下载
      // document.body.removeChild(a) / 释放;
      // setTimeout(() => {
      //   this.active++;
      // }, 2000);

      this.$axios
        .post("/api/excel/executeTemplate", {
          newCellData: this.formData.newCellData,
          cellRowStr: this.formData.cellNum,
          resultExcelName: this.formData.resultExcelName
        })
        .then(res => {
          const { data } = res;
          if (data.success) {
            this.$message.success("Excel处理完成");
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    gotoDownload() {
      this.$axios
        .get("/api/excel/gotoDownload")
        .then(res => {
          const { data } = res;
          if (data.success) {
            this.$emit("nextStep", 2);
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  created() {},
  mounted() {}
};
</script> 

<style scoped>
</style>
