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

interface ICostItemListProps {
	ghost?: boolean;
	costItemId?: BusinessAPI.CostItemVO['costItemId'];
	search?: boolean;
	onValueChange?: () => void;
	mode?: ModeType;
	trigger?: () => React.ReactNode;
}
export default function CostItemList(props: ICostItemListProps) {
	const {
		ghost = false,
		costItemId,
		search = true,
		mode = 'drag',
		trigger,
		onValueChange,
	} = props;
	const intl = useIntl();
	const intlPrefix = 'costItem';
	const [type, setType] =
		useState<BusinessAPI.CostItemPageQry['type']>('MATERIAL_TYPE');

	const columns: ProColumns<BusinessAPI.CostItemVO, BizValueType>[] = [
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.name' }),
			dataIndex: 'name',
			key: 'name',
			renderText: (text: string) => <span className="font-medium">{text}</span>,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.unit' }),
			dataIndex: 'unit',
			key: 'unit',
			search: false,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.price' }),
			dataIndex: 'price',
			key: 'price',
			valueType: 'money',
			search: false,
		},
		// type
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.type' }),
			dataIndex: 'type',
			valueType: 'select',
			valueEnum: {
				MATERIAL_TYPE: intl.formatMessage({
					id: intlPrefix + '.column.type.enum.materialType',
				}),
				ARTIFICIAL_TYPE: intl.formatMessage({
					id: intlPrefix + '.column.type.enum.artificialType',
				}),
			},
		},
		// rule
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.rule' }),
			dataIndex: 'rule',
			valueType: 'select',
			valueEnum: {
				INPUT_QUANTITY: intl.formatMessage({
					id: intlPrefix + '.column.rule.enum.inputQuantity',
				}),
				SELECT_BOX: intl.formatMessage({
					id: intlPrefix + '.column.rule.enum.selectBox',
				}),
				INPUT_QUANTITY_AND_AMOUNT: intl.formatMessage({
					id: intlPrefix + '.column.rule.enum.inputQuantityAndAmount',
				}),
			},
		},
	];

	const formContext = [
		<ProFormText key={'type'} name={'type'} hidden={true} />,
		<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',
					}),
				},
			]}
		/>,
		<ProFormText
			key={'unit'}
			name={'unit'}
			label={intl.formatMessage({ id: intlPrefix + '.form.unit.label' })}
			placeholder={intl.formatMessage({
				id: intlPrefix + '.form.unit.placeholder',
			})}
		/>,
		<ProFormMoney
			key={'price'}
			name={'price'}
			label={intl.formatMessage({ id: intlPrefix + '.form.price.label' })}
			placeholder={intl.formatMessage({
				id: intlPrefix + '.form.price.placeholder',
			})}
			fieldProps={{
				precision: 2,
			}}
		/>,
		// rule ProFormRadio.Group
		<ProFormRadio.Group
			key={'rule'}
			name={'rule'}
			label={intl.formatMessage({ id: intlPrefix + '.form.rule.label' })}
			required={true}
			rules={[
				{
					required: true,
					message: intl.formatMessage({
						id: intlPrefix + '.form.rule.required',
					}),
				},
			]}
			valueEnum={{
				INPUT_QUANTITY: intl.formatMessage({
					id: intlPrefix + '.form.rule.enum.inputQuantity',
				}),
				SELECT_BOX: intl.formatMessage({
					id: intlPrefix + '.form.rule.enum.selectBox',
				}),
				INPUT_QUANTITY_AND_AMOUNT: intl.formatMessage({
					id: intlPrefix + '.form.rule.enum.inputQuantityAndAmount',
				}),
			}}
		/>,
		<ProFormRadio.Group
			key={'type'}
			name={'type'}
			label={intl.formatMessage({ id: intlPrefix + '.form.type.label' })}
			required={true}
			rules={[
				{
					required: true,
					message: intl.formatMessage({
						id: intlPrefix + '.form.type.required',
					}),
				},
			]}
			valueEnum={{
				MATERIAL_TYPE: intl.formatMessage({
					id: intlPrefix + '.form.type.enum.materialType',
				}),
				ARTIFICIAL_TYPE: intl.formatMessage({
					id: intlPrefix + '.form.type.enum.artificialType',
				}),
			}}
		/>,
	];

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

	return (
		<BizContainer<
			typeof business.costItem,
			BusinessAPI.CostItemVO,
			BusinessAPI.CostItemPageQry,
			BusinessAPI.CostItemCreateCmd,
			BusinessAPI.CostItemUpdateCmd
		>
			rowKey={'costItemId'}
			permission={'operation-cost-item'}
			func={business.costItem}
			method={'costItem'}
			methodUpper={'CostItem'}
			intlPrefix={intlPrefix}
			modeType={mode}
			onValueChange={onValueChange}
			container={{
				ghost,
				fieldProps: {
					tabActiveKey: type,
					onTabChange: (key) => {
						setType(key as BusinessAPI.CostItemPageQry['type']);
					},
					tabList: [
						{
							key: 'MATERIAL_TYPE',
							tab: intl.formatMessage({
								id: intlPrefix + '.type.materialType',
							}),
						},
						{
							key: 'ARTIFICIAL_TYPE',
							tab: intl.formatMessage({
								id: intlPrefix + '.type.artificialType',
							}),
						},
					],
				},
			}}
			remark={{
				mode: 'editor',
			}}
			status
			drag={{
				fieldProps: {
					bordered: true,
					ghost,
					//@ts-ignore
					search,
					params: {
						type: type,
					},
				},
				columns,
			}}
			create={{
				formType: 'drawer',
				formContext,
				initValues: {
					type: type,
					requireQuantityAndPrice: true,
					status: true,
				},
			}}
			update={{
				formType: 'drawer',
				formContext,
			}}
			destroy={{}}
			detail={{
				rowId: costItemId,
				formType: 'drawer',
				columns: detailColumns,
				trigger,
			}}
		/>
	);
}
