import {
	BizContainer,
	BizValueType,
	DealerSelect,
	ModeType,
} from '@/components';
import { business } from '@/services';
import { useIntl } from '@@/exports';
import {
	ProColumns,
	ProFormDependency,
	ProFormDigit,
	ProFormRadio,
	ProFormText,
} from '@ant-design/pro-components';
import { ProDescriptionsItemProps } from '@ant-design/pro-descriptions';
import React from 'react';

interface IDealerRebateCustomerListProps {
	ghost?: boolean;
	dealerVO?: BusinessAPI.DealerVO;
	search?: boolean;
	onValueChange?: () => void;
	mode?: ModeType;
	trigger?: () => React.ReactNode;
}

export default function DealerRebateCustomerList(
	props: IDealerRebateCustomerListProps,
) {
	const {
		ghost = false,
		dealerVO,
		search = true,
		mode = 'page',
		trigger,
		onValueChange,
	} = props;

	const intl = useIntl();
	const intlPrefix = 'dealerRebateCustomer';

	const columns: ProColumns<
		BusinessAPI.DealerRebateCustomerVO,
		BizValueType
	>[] = [
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.dealerId' }),
			dataIndex: 'dealerVO',
			key: 'dealerId',
			valueType: 'dealer',
			hidden: !!dealerVO,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.name' }),
			dataIndex: 'name',
			key: 'name',
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.calcMethod' }),
			dataIndex: 'calcMethod',
			key: 'calcMethod',
			valueType: 'select',
			valueEnum: {
				NET_WEIGHT: intl.formatMessage({
					id: intlPrefix + '.column.calcMethod.enum.netWeight',
				}),
				FIXED_AMOUNT: intl.formatMessage({
					id: intlPrefix + '.column.calcMethod.enum.fixedAmount',
				}),
				NOT_FIXED: intl.formatMessage({
					id: intlPrefix + '.column.calcMethod.enum.notFixed',
				}),
			},
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.unitPrice' }),
			dataIndex: 'unitPrice',
			key: 'unitPrice',
			valueType: 'money',
			render: (_, record) => {
				if (record.calcMethod === 'NET_WEIGHT' && record.unitPrice) {
					return `${record.unitPrice}元/斤`;
				}
				return '-';
			},
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.amount' }),
			dataIndex: 'amount',
			key: 'amount',
			valueType: 'money',
			render: (_, record) => {
				if (record.calcMethod === 'FIXED_AMOUNT' && record.amount) {
					return `${record.amount}元/车`;
				}
				return '-';
			},
		},
	];

	const formContext = [
		<DealerSelect key={'dealerId'} />,
		<ProFormText
			key={'name'}
			name={'name'}
			label={intl.formatMessage({ id: intlPrefix + '.form.name.label' })}
			required={true}
			placeholder={intl.formatMessage({
				id: intlPrefix + '.form.name.placeholder',
			})}
			rules={[
				{
					required: true,
					message: intl.formatMessage({
						id: intlPrefix + '.form.name.required',
					}),
				},
			]}
		/>,
		<ProFormRadio.Group
			key={'calcMethod'}
			name={'calcMethod'}
			label={intl.formatMessage({ id: intlPrefix + '.form.calcMethod.label' })}
			placeholder={intl.formatMessage({
				id: intlPrefix + '.form.calcMethod.placeholder',
			})}
			valueEnum={{
				NET_WEIGHT: intl.formatMessage({
					id: intlPrefix + '.form.calcMethod.enum.netWeight',
				}),
				FIXED_AMOUNT: intl.formatMessage({
					id: intlPrefix + '.form.calcMethod.enum.fixedAmount',
				}),
				NOT_FIXED: intl.formatMessage({
					id: intlPrefix + '.form.calcMethod.enum.notFixed',
				}),
			}}
			rules={[
				{
					required: true,
					message: intl.formatMessage({
						id: intlPrefix + '.form.calcMethod.required',
					}),
				},
			]}
		/>,
		<ProFormDependency key={'calcMethod'} name={['calcMethod']}>
			{({ calcMethod }) => {
				return (
					<>
						{calcMethod === 'NET_WEIGHT' && (
							<ProFormDigit
								key={'unitPrice'}
								name={'unitPrice'}
								label={intl.formatMessage({
									id: intlPrefix + '.form.unitPrice.label',
								})}
								fieldProps={{
									precision: 2,
									step: 0.01,
								}}
								placeholder={intl.formatMessage({
									id: intlPrefix + '.form.unitPrice.placeholder',
								})}
							/>
						)}
						{calcMethod === 'FIXED_AMOUNT' && (
							<ProFormDigit
								key={'amount'}
								name={'amount'}
								label={intl.formatMessage({
									id: intlPrefix + '.form.amount.label',
								})}
								fieldProps={{
									precision: 2,
									step: 0.01,
								}}
								placeholder={intl.formatMessage({
									id: intlPrefix + '.form.amount.placeholder',
								})}
							/>
						)}
					</>
				);
			}}
		</ProFormDependency>,
	];

	const detailColumns: ProDescriptionsItemProps<
		BusinessAPI.DealerRebateCustomerVO,
		BizValueType
	>[] = columns as ProDescriptionsItemProps<
		BusinessAPI.DealerRebateCustomerVO,
		BizValueType
	>[];

	return (
		<BizContainer<
			typeof business.dealerRebateCustomer,
			BusinessAPI.DealerRebateCustomerVO,
			BusinessAPI.DealerRebateCustomerPageQry,
			BusinessAPI.DealerRebateCustomerCreateCmd,
			BusinessAPI.DealerRebateCustomerUpdateCmd
		>
			rowKey={'customerId'}
			permission={'operation-dealer-rebate-customer'}
			func={business.dealerRebateCustomer}
			method={'dealerRebateCustomer'}
			methodUpper={'DealerRebateCustomer'}
			intlPrefix={intlPrefix}
			modeType={mode}
			onValueChange={onValueChange}
			container={{
				ghost,
			}}
			remark={{
				mode: 'editor',
			}}
			status
			page={{
				fieldProps: {
					bordered: true,
					ghost,
					//@ts-ignore
					search,
					params: {
						...(dealerVO && {
							dealerId: dealerVO?.dealerId,
						}),
					},
				},
				columns,
			}}
			create={{
				formType: 'drawer',
				formContext,
				initValues: {
					...(dealerVO && {
						dealerId: dealerVO?.dealerId,
						dealerVO: dealerVO,
						canChangeDealer: !dealerVO,
					}),
					status: true,
				},
			}}
			update={{
				formType: 'drawer',
				formContext,
			}}
			destroy={{}}
			detail={{
				formType: 'drawer',
				columns: detailColumns,
				trigger,
			}}
		/>
	);
}
