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

export type IUserSelectProps = {
	onFinish?: (dealerVOList: BusinessAPI.DealerVO[]) => void;
} & ProFormSelectProps;

export default function DealerSelect(props: IUserSelectProps) {
	const intl = useIntl();

	return (
		<ProFormDependency name={['dealerVO', 'canChangeDealer']}>
			{({ dealerVO, canChangeDealer }, form) => {
				return (
					<DealerSearch
						{...(canChangeDealer !== undefined && {
							readonly: !canChangeDealer,
						})}
						className={'dealer-select'}
						form={form}
						{...(dealerVO && { selectedList: [dealerVO] })}
						label={intl.formatMessage({
							id: 'form.dealerId.label',
						})}
						transform={(dealerVO) => {
							return {
								dealerVO: dealerVO,
								dealerId: dealerVO.dealerId,
							};
						}}
						name={'dealerVO'}
						required={true}
						convertValue={(dealerVO) => {
							return dealerVO
								? `${dealerVO?.shortName || dealerVO?.fullName}`
								: undefined;
						}}
						placeholder={intl.formatMessage({
							id: 'form.dealerId.placeholder',
						})}
						rules={[
							{
								required: true,
								message: intl.formatMessage({
									id: 'form.dealerId.required',
								}),
							},
						]}
						{...props}
					/>
				);
			}}
		</ProFormDependency>
	);
}
