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

interface IDealerStallListProps {
	ghost?: boolean;
	supplierId?: BusinessAPI.SupplierVO['supplierId'];
	dealerVO?: BusinessAPI.DealerVO;
	search?: boolean;
	onValueChange?: () => void;
	mode?: ModeType;
	trigger?: () => React.ReactNode;
}

export default function DealerStallList(props: IDealerStallListProps) {
	const {
		ghost = false,
		dealerVO,
		supplierId,
		search = true,
		mode = 'page',
		trigger,
		onValueChange,
	} = props;
	const intl = useIntl();
	const intlPrefix = 'dealerStall';

	const columns: ProColumns<BusinessAPI.SupplierVO, 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',
			renderText: (text: string) => <span className="font-medium">{text}</span>,
		},
	];

	const formContext = [
		<DealerSelect
			key={'dealerId'}
			params={{
				dealerType: 'MARKET',
			}}
		/>,
		<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',
					}),
				},
			]}
		/>,
	];

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

	return (
		<BizContainer<
			typeof business.supplier,
			BusinessAPI.SupplierVO,
			BusinessAPI.SupplierPageQry,
			BusinessAPI.SupplierCreateCmd,
			BusinessAPI.SupplierUpdateCmd
		>
			rowKey={'supplierId'}
			permission={'operation-supplier'}
			func={business.supplier}
			method={'supplier'}
			methodUpper={'Supplier'}
			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,
						}),
						type: 'STALL',
					},
				},
				columns,
			}}
			create={{
				formType: 'drawer',
				formContext,
				initValues: {
					...(dealerVO && {
						dealerId: dealerVO?.dealerId,
						dealerVO: dealerVO,
						canChangeDealer: !dealerVO,
					}),
					status: true,
					type: 'STALL',
				},
			}}
			update={{
				formType: 'drawer',
				formContext,
			}}
			destroy={{}}
			detail={{
				rowId: supplierId,
				formType: 'drawer',
				columns: detailColumns,
				trigger,
			}}
		/>
	);
}
