<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/encryption";

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

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

// 取消
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);
  importfile(formData).then((res) => {
    const { flag } = res;
    if (flag) {
      ElMessage.success('恢复成功');
      emit("confirm");
    }
    uploadLoading.value = false;
  }).catch(() => { 
    uploadLoading.value = false;
  });
};

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

<template>
  <div>
    <div class="mb20">
      <CustomUpload :prompt='uploadFile ? uploadFile.name : ""' :loading="uploadLoading"
        @file-change="handleFileChange" />
    </div>
    <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>