import { useState } from "react";
import { request } from '@umijs/max';
import { baseUrl } from '@/constants/index'
import { message } from 'antd';
import { ProFormUploadDragger } from '@ant-design/pro-components';
type UploadModelProps = {
    form_name: string;
    uploadTxt: string;
    onUploadComplete: (filename: string) => void;
};
const UploadModel: React.FC<UploadModelProps> = (props) => {
    const { onUploadComplete, uploadTxt,  form_name} = props;
    const handleUploadComplete = (filename: string) => {
        if (onUploadComplete) {
            onUploadComplete(filename);
        }
    };

    return (
        <>
            <ProFormUploadDragger
                label={uploadTxt}
                name={form_name}
                icon={null}
                fieldProps={{
                    fileList: [],
                    listType: 'picture-card',
                    showUploadList: false,
                    customRequest: (e) => {
                        let formdata = new FormData();
                        formdata.append('doc', e.file);
                        console.log(formdata)
                        request(baseUrl + '/common/doc/upload?doctype=1', {
                            method: 'post',
                            data: formdata,
                            Headers: {
                                Authorization: sessionStorage.getItem('token'),
                                'X-Requested-With': null
                            }
                        }).then(res => {
                            handleUploadComplete(res.data.filename)
                        }).catch(err => {
                            console.log(err)
                            message.error('文件上传失败，请检查文件类型，如有疑问，请咨询管理员')
                        })
                    }
                }}
            >

            </ProFormUploadDragger>

        </>
    )
}

export default UploadModel;