import { UploadMaterial } from '@/components';
import {
	ProFormDependency,
	ProFormField,
	ProFormItemProps,
} from '@ant-design/pro-components';
import React from 'react';

interface ProFormUploadMaterialProps extends ProFormItemProps {
	fieldProps: {
		maxCount: number;
		onChange?: (fileList: any[]) => void;
		fileList?: any[];
	};
}

const ProFormUploadMaterial: React.FC<ProFormUploadMaterialProps> = ({
	fieldProps,
	...rest
}) => {
	return (
		<ProFormDependency name={[rest.name]}>
			{(data, form) => (
				<ProFormField {...rest}>
					<UploadMaterial
						onChange={(fileList) => {
							form.setFieldValue(
								rest.name,
								fileList?.map((file: any) => file.url),
							);
						}}
						fileList={data[rest.name]?.map((url: string, index: number) => {
							return {
								uid: index.toString(),
								name: url,
								// @ts-ignore
								status: 'success',
								thumbUrl: url,
								url: url,
							};
						})}
						{...fieldProps}
					/>
				</ProFormField>
			)}
		</ProFormDependency>
	);
};
export default ProFormUploadMaterial;
