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

export type IPaymentTaskSelectProps = {
	onFinish?: (paymentTaskVOList: BusinessAPI.PaymentTaskVO[]) => void;
} & ProFormSelectProps;

export default function PaymentTaskSelect(props: IPaymentTaskSelectProps) {
	const intl = useIntl();

	return (
		<ProFormDependency name={['paymentTaskVO', 'canChangePaymentTask']}>
			{({ paymentTaskVO, canChangePaymentTask }, form) => {
				return (
					<PaymentTaskSearch
						{...(canChangePaymentTask !== undefined && {
							readonly: !canChangePaymentTask,
						})}
						className={'payment-task-select'}
						form={form}
						{...(paymentTaskVO && { selectedList: [paymentTaskVO] })}
						label={intl.formatMessage({
							id: 'form.paymentTaskId.label',
						})}
						transform={(paymentTaskVO) => {
							return {
								paymentTaskVO: paymentTaskVO,
								paymentTaskId: paymentTaskVO.paymentTaskId,
							};
						}}
						name={'paymentTaskVO'}
						required={true}
						convertValue={(paymentTaskVO) => {
							return paymentTaskVO
								? `${paymentTaskVO?.taskName || paymentTaskVO?.paymentTaskSn}`
								: undefined;
						}}
						placeholder={intl.formatMessage({
							id: 'form.paymentTaskId.placeholder',
						})}
						rules={[
							{
								required: true,
								message: intl.formatMessage({
									id: 'form.paymentTaskId.required',
								}),
							},
						]}
						{...props}
					/>
				);
			}}
		</ProFormDependency>
	);
}
