import { ProFormSelect } from '@ant-design/pro-components';
import { ProFieldFCRenderProps } from '@ant-design/pro-provider';
import { useState } from 'react';
import { OrderModal } from '@/components';

export interface IOrderFormItemProps
	extends Omit<ProFieldFCRenderProps, 'value' | 'onChange'> {
	value?: BusinessAPI.OrderVO['orderId'];
	onChange?: (value?: BusinessAPI.OrderVO['orderId']) => void;
}

export default function OrderFormItem(
	props: IOrderFormItemProps,
) {
	const { value, onChange } = props;

	const [showOrderModal, setShowOrderModal] =
		useState<boolean>(false);
	const [orderList, setOrderList] =
		useState<(BusinessAPI.OrderVO | undefined)[]>();

	return (
		<>
			<ProFormSelect
				fieldProps={{
					open: false,
					onClear: () => {
						onChange?.(undefined);
					},
					onClick: () => {
						setShowOrderModal(true);
					},
					value: value,
					placeholder: '请选择采购单',
					options: orderList?.map(
						(dealerVO?: BusinessAPI.OrderVO) => {
							return {
								value: dealerVO?.orderId,
								label: `${dealerVO?.orderSn}`,
							};
						},
					),
				}}
			/>
			<OrderModal
				title={'选择采购单'}
				open={showOrderModal}
				onOk={() => setShowOrderModal(false)}
				onCancel={() => setShowOrderModal(false)}
				onFinish={async (orderVOList) => {
					if (orderVOList.length > 0) {
						const orderVO = orderVOList[0];
						onChange?.(orderVO?.orderId);
						setOrderList(orderVOList);
						setShowOrderModal(false);
					}
				}}
				type={'radio'}
			/>
		</>
	);
}
