import {
	BizContainer,
	BizValueType,
	ModeType,
	OrderFormItem,
} from '@/components';
import { business } from '@/services';
import { useIntl } from '@@/exports';
import { ProColumns } from '@ant-design/pro-components';
import { ProDescriptionsItemProps } from '@ant-design/pro-descriptions';
import { Space, Tag } from 'antd';
import React, { useState } from 'react';

interface IOrderListProps {
	ghost?: boolean;
	orderId?: BusinessAPI.OrderVO['orderId'];
	search?: boolean;
	onValueChange?: () => void;
	mode?: ModeType;
	trigger?: () => React.ReactNode;
	params?: BusinessAPI.OrderPageQry;
}

export default function OrderList(props: IOrderListProps) {
	const {
		ghost = false,
		orderId,
		search = true,
		mode = 'page',
		trigger,
		onValueChange,
		params,
	} = props;
	const intl = useIntl();
	const intlPrefix = 'order';

	const [activeKey, setActiveKey] = useState<string>('ALL');

	const columns: ProColumns<BusinessAPI.OrderVO, BizValueType>[] = [
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.order' }),
			key: 'orderId',
			renderFormItem: (_, props) => {
				return (
					//@ts-ignore
					<OrderFormItem {...props} {...props?.fieldProps} />
				);
			},
			render: (_, orderVO: BusinessAPI.OrderVO) => {
				return (
					orderVO && (
						<OrderList
							ghost={true}
							mode={'detail'}
							orderId={orderVO.orderId}
							trigger={() => (
								<Space>
									<a>
										{`${orderVO.orderVehicle?.dealerName} - 第 ${orderVO.orderVehicle?.vehicleNo || '暂无'} 车 - ${orderVO.orderSn || '暂无'}`}
									</a>
								</Space>
							)}
						/>
					)
				);
			},
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.dealer' }),
			valueType: 'dealer',
			key: 'dealerId',
			hideInTable: true,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.origin' }),
			dataIndex: ['orderVehicle', 'origin'],
			search: false,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.destination' }),
			dataIndex: ['orderVehicle', 'destination'],
			search: false,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.plate' }),
			dataIndex: ['orderVehicle', 'plate'],
			key: 'plate',
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.driver' }),
			dataIndex: ['orderVehicle', 'driver'],
			key: 'driver',
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.phone' }),
			dataIndex: ['orderVehicle', 'phone'],
			key: 'phone',
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.state' }),
			dataIndex: 'state',
			key: 'state',
			valueType: 'select',
			render: (_, record) => {
				const stateText = intl.formatMessage({
					id: `${intlPrefix}.column.state.${record.state?.toLowerCase() || 'unknown'}`,
				});

				let color = 'default';
				switch (record.state) {
					case 'DRAFT':
						color = 'default';
						break;
					case 'AUDITING':
						color = 'processing';
						break;
					case 'COMPLETED':
						color = 'success';
						break;
					case 'CLOSED':
						color = 'error';
						break;
					default:
						color = 'default';
				}

				return <Tag color={color}>{stateText}</Tag>;
			},
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.createdAt' }),
			dataIndex: 'createdAt',
			key: 'createdAt',
			valueType: 'dateTime',
			search: false,
		},
	];

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

	return (
		<BizContainer<
			typeof business.order,
			BusinessAPI.OrderVO,
			BusinessAPI.OrderPageQry,
			BusinessAPI.OrderCreateCmd,
			BusinessAPI.OrderUpdateCmd
		>
			rowKey={'orderId'}
			permission={'operation-order'}
			func={business.order}
			method={'order'}
			methodUpper={'Order'}
			intlPrefix={intlPrefix}
			modeType={mode}
			onValueChange={onValueChange}
			container={{
				ghost,
			}}
			status={false}
			page={{
				fieldProps: {
					bordered: true,
					ghost,
					//@ts-ignore
					search,
					params: {
						...(activeKey !== 'ALL' && {
							state: activeKey! as BusinessAPI.OrderVO['state'],
						}),
						...params,
					},
					toolbar: {
						menu: {
							type: 'tab',
							activeKey: activeKey,
							items: [
								// 全部
								{
									key: 'ALL',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.all',
									}),
								},
								{
									key: 'DRAFT',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.draft',
									}),
								},
								{
									key: 'AUDITING',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.auditing',
									}),
								},
								{
									key: 'COMPLETED',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.completed',
									}),
								},
								{
									key: 'CLOSED',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.closed',
									}),
								},
							],
							onChange: (key) => {
								setActiveKey(key as string);
							},
						},
					},
				},
				columns,
			}}
			create={false}
			update={false}
			destroy={false}
			detail={{
				rowId: orderId,
				formType: 'drawer',
				columns: detailColumns,
				trigger,
			}}
		/>
	);
}
