<template>
  <div class="batch-import-com">
    <!-- 导入弹窗 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" on-error="handleFileError" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="cancelimport">取 消</el-button>
      </div>
    </el-dialog>
    <!--导入结果-->
    <el-dialog title="导入失败" :visible.sync="upload.showResult" width="60%" class="batch-import-upload-dialog-container"
      custom-class="batch-import-upload-dialog" @close="importResultClose">
      <div class="batch-import-upload-inner">
        <el-table :data="uploadResult.errorMsg" border stripe v-if="uploadResult.errorMsg.length" class="error-msg-list-table" max-height="500px">
          <el-table-column label="导入状态">
            <template slot-scope="scope">
              <span style="color:red;">导入失败</span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="投标人">
            <template slot-scope="scope">
              <span></span>
            </template>
          </el-table-column> -->
          <el-table-column label="导入失败原因">
            <template slot-scope="scope">
              <span style="color:red;">{{scope.row}}</span>
            </template>
          </el-table-column>
        </el-table>
        <el-row style="padding-top: 20px;text-align: right">
          <el-button @click="upload.showResult = false">取消</el-button>
          <el-button type="primary" @click="upload.showResult = false">确定</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "batchImportCom",
  props: {
    uploadOptions: {
      type: Object,
      default: () => ({})
    }
  },
  watch: {
    "uploadOptions": {
      handler(newValue, oldValue) {
        this.upload = newValue;
      },
      deep: true
    }
  },
  data() {
    return {
      // 导入参数
      upload: this.uploadOptions,
      // 导入结果
      uploadResult: {
        successCount: 0,
        errorCount: 0,
        errorMsg: []
      }

    };
  },
  //可访问data属性
  created() {

  },
  //计算集
  computed: {

  },
  //方法集
  methods: {
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      if (response.code == 200) {
        this.$message.success('上传成功!');
        if (response.data) {
          this.uploadResult.successCount = response.data.sucessCount;
          this.uploadResult.errorCount = response.data.errorCount;
          (response.data?.errorMsg && response.data?.errorMsg?.length) ? ((this.uploadResult.errorMsg = response.data.errorMsg) && (this.upload.showResult = true)) : null;
        }
        this.$emit("success-upload");
      } else {
        this.$message.error(`上传失败,${response.msg ? response.msg : "请根据模板正确填写表格内容!"}`);
      }
    },
    handleFileError(err, file, fileList) {
      this.$message.error(`上传失败,${response.msg ? response.msg : "请根据模板正确填写表格内容!"}`);
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      let a = document.createElement("a");
      a.setAttribute("href", this.upload.fileTemplateUrl);
      a.setAttribute("download", `${this.upload.fileName}_${new Date().getTime()}.xlsx`);
      document.body.appendChild(a);
      a.click();
      a.remove();
    },
    importResultClose() {
      this.upload.showResult = false;
      this.uploadResult = this.$options.data.call(this).uploadResult;
    },
    cancelimport() {
      this.$emit("cancel-import");
    }
  },
}
</script>
<style lang="scss" scoped>
.batch-import-com {
  ::v-deep.batch-import-upload-dialog-container {
    .batch-import-upload-dialog {
      width: 100%;
      max-height: 85%;
      overflow: auto;

      .el-dialog__header {
        position: sticky;
        top: 0px;
        background: #fff;
        z-index: 99;
        padding-bottom: 0px;
      }
      .batch-import-upload-inner {
        width: 100%;
        display: flex;
        flex-direction: column;

        .result-count {
          line-height: 32px;

          .success {
            color: #67c23a;
          }

          .error {
            color: #f56c6c;
          }
        }

        .error-msg-list-table {
          margin-top: 8px;
        }
      }
    }
  }
}
</style>
