<script setup lang="ts" name="AddClass">
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import CustomUpload from "@/components/CustomUpload/index.vue";
import { importfile } from "@/api/ruleConfig/dictionary";

const emit = defineEmits(["cancel", "confirm"]);

const uploadLoading = ref(false);
const uploadFile = ref({});
const smart = ref(1)

// 取消
const cancel = () => {
  emit("cancel");
};

// 确认
const confirm = () => {
  // emit("confirm");
  if (!uploadFile.value.name) {
    ElMessage.error("请先选择文件");
    return;
  }
  uploadLoading.value = true;
  const formData = new FormData();
  formData.append('fileUpload', uploadFile.value.raw);
  formData.append('smart', smart.value.toString());
  importfile(formData).then((res) => {
    const { flag } = res;
    if (flag) {
      uploadLoading.value = false;
      ElMessage.success('导入成功');
      emit("confirm");
    }
  });
};

const handleFileChange = (file) => {
  uploadFile.value = file;
};
</script>

<template>
  <div>
    <div class="mb20">
      <CustomUpload :prompt='uploadFile ? uploadFile.name : ""' :loading="uploadLoading"
        @file-change="handleFileChange" />
    </div>
    <el-form-item label="上传方式" label-width="200px">
      <el-radio-group v-model="smart">
        <el-radio :label="1">覆盖原数据</el-radio>
        <el-radio :label="0">追加数据</el-radio>
      </el-radio-group>
    </el-form-item>
    <div class="btn">
      <el-button type="primary" :loading="uploadLoading" style="width: 80px;" @click="confirm">确认</el-button>
      <el-button style="width: 80px;" @click="cancel">取消</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.btn {
  padding: 20px;
  display: flex;
  justify-content: center;
}
</style>