import {
  ModalForm,
  ProFormRadio,
  ProFormUploadDragger,
} from '@ant-design/pro-components';
import { message } from 'antd';
import { DictTypeController } from '../../services';
interface AddFormProps {
  trigger?: JSX.Element;
  reloadTable?: () => void;
}
const ImportModal: React.FC<AddFormProps> = (props) => {
  const { trigger, reloadTable } = props;
  const handleImport = async (values: any) => {
    const formData = new FormData();
    // 仅限一个文件
    formData.append('file', values.file[0]?.originFileObj);
    formData.append('updateSupport', values.updateSupport);
    const res = await DictTypeController.importExcelApi(formData);
    message.success(res.message || '导入成功');
    reloadTable?.();
    return true;
  };
  return (
    <ModalForm
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      layout="horizontal"
      title="导入"
      trigger={trigger}
      modalProps={{
        destroyOnClose: true,
      }}
      onFinish={handleImport}
    >
      <ProFormRadio.Group
        label="是否更新"
        name="updateSupport"
        options={[
          { label: '是', value: true },
          { label: '否', value: false },
        ]}
        initialValue={true}
      />
      <ProFormUploadDragger
        colon={false}
        max={1}
        label=" "
        name="file"
        description="支持扩展名: .xlsx、xls"
        fieldProps={{
          beforeUpload: () => false,
        }}
      />
    </ModalForm>
  );
};

export default ImportModal;
