import { SupplierSearch } from '@/components';
import { useIntl } from '@@/exports';
import {
	ProFormDependency,
	ProFormSelectProps,
} from '@ant-design/pro-components';

export type ISupplierSelectProps = {
	onFinish?: (supplierVOList: BusinessAPI.SupplierVO[]) => void;
} & ProFormSelectProps;

export default function SupplierSelect(props: ISupplierSelectProps) {
	const intl = useIntl();

	return (
		<ProFormDependency name={['supplierVO', 'canChangeSupplier']}>
			{({ supplierVO, canChangeSupplier }, form) => {
				return (
					<SupplierSearch
						{...(canChangeSupplier !== undefined && {
							readonly: !canChangeSupplier,
						})}
						className={'supplier-select'}
						form={form}
						{...(supplierVO && { selectedList: [supplierVO] })}
						label={intl.formatMessage({
							id: 'form.supplierId.label',
						})}
						transform={(supplierVO) => {
							return {
								supplierVO: supplierVO,
								supplierId: supplierVO.supplierId,
							};
						}}
						name={'supplierVO'}
						required={true}
						convertValue={(supplierVO) => {
							return supplierVO ? supplierVO?.name : undefined;
						}}
						placeholder={intl.formatMessage({
							id: 'form.supplierId.placeholder',
						})}
						rules={[
							{
								required: true,
								message: intl.formatMessage({
									id: 'form.supplierId.required',
								}),
							},
						]}
						{...props}
					/>
				);
			}}
		</ProFormDependency>
	);
}
