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

    return (
        <>
            <ProFormUploadButton
                name={form_name}
                icon={null}
                title={uploadTxt}
                fieldProps={{
                    fileList: [],
                    listType: 'picture-card',
                    showUploadList: false,
                    customRequest: (e) => {
                        let formdata = new FormData();
                        formdata.append('image', e.file);
                        console.log(formdata)
                        request(baseUrl + '/common/image/upload?imgtype=' + image_type, {
                            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('上传失败')
                        })
                    }
                }}
            >

            </ProFormUploadButton>
        </>
    )
}

export default UploadModel;