import type { Ref } from 'vue';
import { message, Modal } from 'ant-design-vue';
import { FileItem } from '/@/components/Upload/src/upload';
import { UploadStatus } from '/@/components/Upload/src/enum';
export function useUpload() {
  // 检验是否全部上传成功了
  const validatorUpload = (fileList: Ref<FileItem[]>) => {
    return new Promise((resolve, reject) => {
      const newFileList = fileList.value.filter((el) => el.status !== UploadStatus.ERROR);
      const uploadingFilelist = newFileList.filter(
        (item) => item.status === UploadStatus.UPLOADING,
      );
      if (uploadingFilelist.length) {
        message.error('还有文件正在上传中，请稍后');
        reject(false);
        return false;
      }
      const doneFileList = newFileList.filter((item) => item.status === UploadStatus.DONE);
      if (doneFileList.length !== newFileList.length) {
        Modal.confirm({
          title: '系统提示',
          content: '您有上传失败的文件，确定提交将忽略上传失败的文件',
          okText: '是否提交',
          onOk: () => {
            fileList.value = doneFileList;
            resolve(true);
            return true;
          },
          onCancel: () => {
            reject(false);
          },
        });
        return false;
      }
      resolve(true);
    });
  };

  return {
    validatorUpload,
  };
}
