<!--
 * @Descripttion: 导入组件
 * @Author: tqh
-->
<script setup lang="ts">
import { ref, defineProps, watch } from "vue";
import { ElMessageBox, ElMessage, FormRules } from "element-plus";
import { downloadPDF } from "../../utils/utils";
import {getDownloadFile} from '../../api/common'
const isSubmitLoading = ref(false);
const loading = ref(false);
const importref = ref();
const { drawerProps, uploadFileUrl, uploadFileCheckUrl, templateUrl, search } =
  defineProps({
    drawerProps: Object,
    uploadFileUrl: Object,
    uploadFileCheckUrl: Object,
    templateUrl: String,
    search: Function,
  });
const emit = defineEmits(["closeList"]);
watch(
  () => drawerProps.showDrawer,
  () => {
    drawerProps.showConfirm = false;
  }
);

let fileList = ref([]);
/**
 * @description 抽屉事件回传
 */
const handleImportDrawerEvent = (data) => {
  const { eventName = "" } = data;
  // 抽屉点击确定按钮
  if (eventName === "success") {
    importConfirm();
  }
};

const uploadRef = ref({});
/**
 * 文件导入
 * @param uploadFile {object} - 当前导入文件
 * @param uploadFiles {array} - 导入所有文件
 */
const handleChangeUpload = (uploadFile) => {
  loading.value = true;
  let fd = new FormData();
  fd.append("file", uploadFile.raw);
  // 导入校验
  uploadFileCheckUrl(fd)
    .then((res) => {
      const { code, data } = res || {};
      if (code === 200) {
        const { importStatus, message, failInfoList } = data || {};
        uploadInfo.value.type = importStatus ? "success" : "error";
        drawerProps.showConfirm = importStatus ? true : false;
        uploadInfo.value.text = message;
        uploadInfo.value.errorList = failInfoList || [];
        uploadInfo.value.errorList.forEach((item) => {
          item.line = item.row;
          item.content = item.message;
        });
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

// pdf导出
const handleDownloadPdf = () => {
  downloadPDF(document.getElementById("yaPdf"), "文件错误原因");
};
// 确认导入
const importConfirm = () => {
  const fileList = uploadRef.value.fileList;
  if (!fileList[0]) {
    ElMessage.warning("请先选择上传文件");
    return;
  }
  importref.value && (importref.value.loading = true);
  isSubmitLoading.value = true;
  let fd = new FormData();
  fd.append("file", fileList[0].raw);
  uploadFileUrl(fd)
    .then((res) => {
      if (res.code == 200) {
        ElMessage.success("文件导入成功");
        search();
        drawerProps.showDrawer = false;
        fileList.value = reactive([]);
        emit("closeList");
      }
    })
    .finally(() => {
      importref.value.loading = false;
      isSubmitLoading.value = false;
    });
};
const handleCloseImportDrawer = async (done) => {
  emit("closeList");
  done();
};
// 模板下载
const handleDownloadTemplate = (fileName) => {
  debugger
  getDownloadFile(fileName)
};
// 数据移除
const handleDeleteItem = (uploadFile, uploadFiles) => {
  let _data = fileList.findIndex((item) => {
    return item.name == uploadFile.name;
  });
  fileList.splice(_data, 1);
};

const uploadInfo = ref({
  errorList: [],
});
</script>

<template>
  <ya-drawer
    :loading="isSubmitLoading"
    ref="importref"
    :showConfirm="drawerProps.showConfirm"
    @handleEvent="handleImportDrawerEvent"
    :before-close="handleCloseImportDrawer"
    :drawerProps="drawerProps"
    v-if="drawerProps.showDrawer"
  >
    <template #content>
      <YaUpload
        :loading="loading"
        templateUrl="http://10.1.1.18:8102/download/dictImport.xlsx"
        :uploadInfo="uploadInfo"
        @handleChangeUpload="handleChangeUpload"
        @handleDownloadPdf="handleDownloadPdf"
        @handleDownloadTemplate="handleDownloadTemplate"
        :auto-upload="false"
        accept=".xlsx"
        ref="uploadRef"
        :action="''"
      >
      </YaUpload>
    </template>
  </ya-drawer>
</template>
<style lang="scss" scoped></style>
