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

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

export default function ReconciliationInvoiceFormItem(
	props: IReconciliationInvoiceFormItemProps,
) {
	const { value, onChange } = props;

	const [showReconciliationInvoiceModal, setShowReconciliationInvoiceModal] =
		useState<boolean>(false);
	const [reconciliationInvoiceList, setReconciliationInvoiceList] =
		useState<(BusinessAPI.ReconciliationInvoiceVO | undefined)[]>();

	return (
		<>
			<ProFormSelect
				fieldProps={{
					open: false,
					onClear: () => {
						onChange?.(undefined);
					},
					onClick: () => {
						setShowReconciliationInvoiceModal(true);
					},
					value: value,
					placeholder: '请选择对账开票',
					options: reconciliationInvoiceList?.map(
						(reconciliationInvoiceVO?: BusinessAPI.ReconciliationInvoiceVO) => {
							return {
								value: reconciliationInvoiceVO?.reconciliationInvoiceId,
								label: reconciliationInvoiceVO?.invoiceSn,
							};
						},
					),
				}}
			/>
			<ReconciliationInvoiceModal
				title={'选择对账开票'}
				open={showReconciliationInvoiceModal}
				onOk={() => setShowReconciliationInvoiceModal(false)}
				onCancel={() => setShowReconciliationInvoiceModal(false)}
				onFinish={async (reconciliationInvoiceVOList) => {
					if (reconciliationInvoiceVOList.length > 0) {
						const reconciliationInvoiceVO = reconciliationInvoiceVOList[0];
						onChange?.(reconciliationInvoiceVO?.reconciliationInvoiceId);
						setReconciliationInvoiceList(reconciliationInvoiceVOList);
						setShowReconciliationInvoiceModal(false);
					}
				}}
				type={'radio'}
			/>
		</>
	);
}
