import { Upload, Button, message } from "antd";
import { UploadOutlined } from "@ant-design/icons";
import { parseUploadFileList, resolveUploadFileId } from "~/utils/mockUpload";

export default function FilingUpload({
  value,
  onChange,
  disabled,
  maxCount = 1,
  accept,
}) {
  const fileList = Array.isArray(value) ? value : parseUploadFileList(value);

  return (
    <Upload
      maxCount={maxCount}
      accept={accept}
      action={`${window.process.env.app.API_HOST}/safetyEval/file/upload`}
      disabled={disabled}
      fileList={fileList}
      beforeUpload={(file) => {
        const ext = "." + file.name.split(".").pop().toLowerCase();
        if (ext === ".gif") {
          message.error("不支持 GIF 格式图片上传");
          return Upload.LIST_IGNORE;
        }
        return true;
      }}
      onChange={(info) => {
        if (disabled) return;
        onChange?.(info.fileList);
      }}
    >
      {(!fileList || fileList.length < maxCount) && (
        <Button icon={<UploadOutlined />}>上传</Button>
      )}
    </Upload>
  );
}

export function resolveFilingUploadUrl(files) {
  return resolveUploadFileId(files);
}
