<template>
    <el-upload
    style="display: inline-block;margin-right: 10px;"
    :limit="1"
    accept=".csv"
    action="#" 
    :http-request="requestUpload" 
    :show-file-list="false" 
    :before-upload="beforeUpload"
    :disabled="loadingUpload"
    ref="uploadRef"
    >
    <el-button icon="el-icon-upload2" type="primary">导入特色客户标签</el-button>
    </el-upload>
</template>
<script>
import { uploadTag } from '@/api/grid/mycustomer.js'
export default {
  data() {
    return {
      loadingUpload: false,
    };
  },
  methods: {
    // 上传之前格式校验
    beforeUpload(file) {
        const isExcel = file.name.search('csv') !== -1;
        if (!isExcel) {
            this.$message.warn("请导入正确的格式！");
            return false;
        }
        return true;
    },

    // 导入
    async requestUpload(fileOut) {
        const { file } = fileOut;
        const formData = new FormData();
        formData.append('file', file);
        this.loadingUpload = true;
        try {
          const res = await uploadTag(formData);
          if (res.code === 200) {
              this.$message.success(res.msg || "导入完成！");
          } else {
              this.$message.warn(res.data?.msg);
          }
          this.loadingUpload = false;
        } catch (error) {
          this.loadingUpload = false;
        }
        this.$refs.uploadRef.clearFiles()
    }
  }
};
</script>
