<template>
  <div>
    <BaseDialog
      width="500px"
      :isShow.sync="isImportDialog"
      title="批量导入"
      @close="close"
      :appendToBody="true"
      :confirmStatus="false"
      :isShowFooter="false"
    >
      <div class="content" v-loading="loading">
        <el-button type="primary" size="medium" @click="clickHandleImport"
          >导入数据</el-button
        >
        <input
          id="importData"
          type="file"
          ref="importData"
          style="display: none"
          accept=".xlsx, .xls"
          @change="handleImport"
        />

        <div class="down" @click="downloadTemplate">下载导入模板</div>
      </div>
    </BaseDialog>
  </div>
</template>
  
<script>
import { importCertificateList } from "@/api/company";
export default {
  props: {
    api: {
      type: Function,
      default: () => {},
    },
    type: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      loading: false,
      ruleForm: {},
      isImportDialog: false,
      localApi: this.api
    };
  },

  mounted() {},

  methods: {
    openBoxs(row) {
      this.isImportDialog = true;
    },
    clickHandleImport() {
      this.$refs.importData.click();
    },
    // 下载导入模版
    downloadTemplate() {
      let str = "";
      switch (this.type) {
        case 1:
          str = "oss/images/file/20240516/1715830553927.xlsx"; //1： 营业执照模板
          break;
        case 2:
          str = "oss/images/file/20240516/1715830865573.xlsx"; //2： 企业资质证书模板
          break;
        case 3:
          str = "oss/images/file/20240516/1715831036768.xlsx"; //3： 安全生产许可证模板
          break;
        case 4:
          str = "oss/images/file/20240516/1715830908445.xlsx"; //4： 其他证书模板
          break;
        case "员工证书":
          str = "oss/images/file/20240516/1715830944265.xlsx"; // 导入员工证书
          break;
        case "员工信息":
          str = "oss/images/file/20240516/1715830970481.xlsx"; // 导入员工信息
          break;
      }
      this.$methodsTools.exportData(str, false)
    },
    // 导入操作
    handleImport(e) {
      var file = e.target.files[0];
      if (file === undefined) {
        return;
      }
      const type = file.name.toLowerCase().split(".").splice(-1);
      const fileType = ["xlsx", "xls"].some((item) => item == type);
      if (!fileType) {
        this.$message.error("上传格式需为：.xlsx/.xls");
        e.target.value = "";
        return;
      }
      this.loading = true;
      let formData = new FormData();
      formData.append("file", file);
      // 企业证书
      if ([1, 2, 3, 4].includes(this.type)) {
        this.localApi = importCertificateList;
        formData.append("certificateType", this.type);
      }
      this.localApi(formData)
        .then((res) => {
          this.$message.success(res.msg);
          this.isImportDialog = false;
          
        })
        .finally(() => {
          e.target.value = "";
          this.loading = false;
        });
    },
    close() {},
  },
};
</script>
  
<style lang="scss" scoped>
.content {
  font-size: 14px;
  text-align: center;
  .down {
    margin: 10px;
    color: dodgerblue;
    text-decoration: underline;
    &:hover {
      text-decoration: underline;
      // border-bottom: 1px solid #3498db;
    }
    cursor: pointer;
  }
}
</style>
  