<template>
  <batch-import-dialoga
    v-if="type == 1"
    :dialogVisible.sync="dialogVisible"
    temUrl="/oss/images/file/20220518/1652865393160.xlsx"
    apiKey="importDatabankimportDataBackList"
    :isCheck="false"
    :isShowTip="false"
    @success="ExcelSuccess"
  ></batch-import-dialoga>
  <batch-import-dialoga
    v-else
    :dialogVisible.sync="dialogVisible"
    temUrl="/oss/images/file/20220324/1648102107588.docx"
    apiKey="bankquestionimportWordQuestionList"
    :isCheck="false"
    :isShowTip="false"
    :isSuccessBack="true"
    @success="wordSuccess"
    type="Word"
    :param="formData"
  ></batch-import-dialoga>
</template>

<script>
import batchImportDialoga from "@/components/Comon/batchImportDialog.vue";
export default {
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      businObj: {},
      formData: {
        eduId: "",
        businessId: "",
        subjectId: "",
        projectId: "",
      },
      type: 1, //1是execl 2 word
    };
  },
  methods: {
    openBoxs(data, obj, type) {
      this.type = type;
      this.tableData = JSON.parse(JSON.stringify(data));
      if (type == 1) {
        this.businObj = JSON.parse(JSON.stringify(obj));
      } else {
        Object.keys(this.formData).map(
          (e) =>
            (this.formData[e] = e == "eduId" ? obj["educationTypeId"] : obj[e])
        );
      }
      this.dialogVisible = true;
    },
    wordSuccess({ list }) {
      if (list.length == 0) return;
      let numList = this.tableData;
      if (numList.length) {
        let numIndex = 0;
        let childrenIndex = 0;
        numList.forEach((items) => {
          if (items.sort > numIndex) {
            numIndex = items.sort;
          }
          if (items.index > childrenIndex) {
            childrenIndex = items.index;
          }
        });
        list.forEach((items, indexs) => {
          numIndex++;
          childrenIndex++;
          items.sort = numIndex;
          items.index = childrenIndex;
          items.jsonStr = JSON.stringify(items.optionsList);
        });
      } else {
        list.forEach((items, indexs) => {
          items.sort = 1 + indexs;
          items.index = indexs;
          items.jsonStr = JSON.stringify(items.optionsList);
        });
      }
      this.$emit("backData", list);
    },
    ExcelSuccess(data) {
      if (data.fullStatus == "全部成功") {
        this.backDataX(data.questionList);
        this.$message.success("全部导入成功");
      } else {
        let url =
          baseUrls.baseURL + "common/download?fileName=" + data.errorExcel.msg;
        let link = document.createElement("a");
        let fileName = "导入模板" + ".xlsx";
        document.body.appendChild(link);
        link.href = url;
        link.dowmload = fileName;
        link.click();
        link.remove();
        if (data.fullStatus == "部分成功") {
          this.backDataX(data.questionList);
          this.$message.warning("部分导入成功，请打开文档查看错误原因");
        } else {
          this.$message.error("导入失败，请打开文档查看错误原因");
        }
      }
    },
    backDataX(arry) {
      let arr = JSON.parse(JSON.stringify(arry));
      for (let i = 0; i < arr.length; i++) {
        arr[i].partScore = 0;
        arr[i].score = "";
        arr[i].status = 1;
        arr[i].businessList = [this.businObj];
        if (arr[i].type === 4) {
          arr[i].optionsList = JSON.parse(arr[i].jsonStr);
          arr[i].optionsList.forEach((item) => {
            if (item.type === 3 || item.type === 5) {
              item.optionsList = [];
              item.jsonStr = "";
            }
          });
        } else if (arr[i].type === 5) {
          arr[i].jsonStr = "";
          arr[i].optionsList = [];
        } else {
          arr[i].jsonStr = JSON.stringify(arr[i].optionsList);
        }
      }
      if (this.tableData.length) {
        let maxIndex = 0;
        let childrenIndex = 0;
        this.tableData.forEach((item) => {
          if (item.sort > maxIndex) {
            maxIndex = item.sort;
          }
          if (item.index > childrenIndex) {
            childrenIndex = item.index;
          }
        });
        arr.forEach((item, index) => {
          item.sort = maxIndex + 1;
          item.index = childrenIndex + 1;
          maxIndex++;
          childrenIndex++;
        });
      } else {
        arr.forEach((item, index) => {
          item.sort = index + 1;
          item.index = index;
        });
      }
      this.$emit("backData", arr);
    },
  },
  components: {
    batchImportDialoga,
  },
};
</script>

