import { Upload, message, Button } from 'antd';
import { UploadOutlined } from '@ant-design/icons';
import { _apiUrl_ } from '@/utils/common';

const UploadFile = (props) => {
    const newProps = {
        name: 'docFile',
        action: `${_apiUrl_}/account-service/file/document`,
        headers: {
            Authorization: 'bearer ' + localStorage.getItem("Authorization")
        },
        onChange(info) {
            console.log(`info is:=========>`, info);
            if (info.file?.status !== 'uploading') {
                console.log(info.file, info.fileList);
            }
            if (info.file.status === 'done') {
                message.success(`${info.file.name} 上传成功!`);
            } else if (info.file.status === 'error' && info?.file?.response?.success === false) {
                message.error(`${info.file.name} 上传失败`);
            }

            let fileList = [...info.fileList];
            // fileList = fileList.slice(-1);
            fileList = fileList.map(file => {
                if (file.response) {
                    // Component will show file.url as link
                    file.url = file?.response?.data?.url;
                }
                return file;
            });
            props.changeUpload(fileList);
        },
    };
    return (
        <Upload {...newProps} showUploadList={props.showUploadList}>
            <Button icon={<UploadOutlined />}>上传文件</Button>
        </Upload>
    )
};

export default UploadFile