<template>
  <el-drawer
    title="导入商品"
    :visible.sync="importVisible"
    size="700px"
    append-to-body
    :wrapperClosable="false"
  >
    <div class="import-goods-container">
      <el-upload
        ref="uploadRef"
        :limit="1"
        accept=".xlsx, .xls, .csv"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        :before-upload="beforeUpload"
        drag
      >
        <div>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">
            支持格式：Excel (.xlsx, .xls) 或 CSV (.csv)
          </div>
        </div>
      </el-upload>
      <div class="import-template-container">
        <a class="import-template" @click.stop="importTemplate"
          >下载标准模版文件</a
        >
      </div>
      <div class="warning-container">
        导入过程不可逆，请务必先下载模板并仔细核对数据格式！
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="importVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确认导入</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import { getToken } from "@/utils/auth";
export default {
  name: "importGoods",
  data() {
    return {
      importVisible: false,
      // 用户导入参数
      upload: {
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData",
      },
    };
  },
  methods: {
    importTemplate() {
      return;
      this.download("", {}, `标准模版_${new Date().getTime()}.xlsx`);
    },
    open() {
      this.importVisible = true;
    },
    handleConfirm() {
      this.$refs.uploadRef.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.uploadRef.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    beforeUpload(file) {
      console.log(file);
      //   const isLt2M = file.size / 1024 / 1024 < 2;
      //   if (!isLt2M) {
      //     this.$message.error("上传文件大小不能超过 2MB!");
      //   }
      return false;
    },
  },
};
</script>

<style lang="scss" scoped>
.import-goods-container {
  padding: 0 40px 40px;

  ::v-deep .el-upload-dragger {
    width: 620px;
    margin-bottom: 1%;
  }
  .import-template-container {
    width: 620px;
    text-align: center;
    margin-bottom: 20px;
    color: #409eff;
  }
  .warning-container {
    width: 620px;
    text-align: center;
    margin-bottom: 20px;
    color: red;
    font-size: 14px;
  }
}
</style>
