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

export type IOrderSelectProps = {
	onFinish?: (orderVOList: BusinessAPI.OrderVO[]) => void;
} & ProFormSelectProps;

export default function OrderSelect(props: IOrderSelectProps) {
	const intl = useIntl();

	return (
		<ProFormDependency name={['orderVO', 'canChangeOrder']}>
			{({ orderVO, canChangeOrder }, form) => {
				return (
					<OrderSearch
						{...(canChangeOrder !== undefined && {
							readonly: !canChangeOrder,
						})}
						className={'purchase-order-select'}
						form={form}
						{...(orderVO && { selectedList: [orderVO] })}
						label={intl.formatMessage({
							id: 'form.orderId.label',
						})}
						transform={(orderVO) => {
							return {
								orderVO: orderVO,
								orderId: orderVO.orderId,
							};
						}}
						name={'orderVO'}
						required={true}
						convertValue={(orderVO) => {
							return orderVO?.orderSn;
						}}
						placeholder={intl.formatMessage({
							id: 'form.orderId.placeholder',
						})}
						rules={[
							{
								required: true,
								message: intl.formatMessage({
									id: 'form.orderId.required',
								}),
							},
						]}
						{...props}
					/>
				);
			}}
		</ProFormDependency>
	);
}
