import { BizContainer, BizValueType, ModeType } 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 { Tag } from 'antd';
import React, { useState } from 'react';

interface IOrderShipListProps {
	ghost?: boolean;
	orderShipId?: BusinessAPI.OrderShipVO['orderShipId'];
	search?: boolean;
	onValueChange?: () => void;
	mode?: ModeType;
	trigger?: () => React.ReactNode;
	params: BusinessAPI.OrderShipPageQry;
}

export default function OrderShipList(props: IOrderShipListProps) {
	const {
		ghost = false,
		orderShipId,
		search = true,
		mode = 'page',
		trigger,
		onValueChange,
		params,
	} = props;
	const intl = useIntl();
	const intlPrefix = 'orderShip';

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

	const columns: ProColumns<BusinessAPI.OrderShipVO, BizValueType>[] = [
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.orderSn' }),
			dataIndex: 'orderSn',
			key: 'orderSn',
			renderText: (text: string) => <span className="font-medium">{text}</span>,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.dealerName' }),
			dataIndex: 'dealerName',
			key: 'dealerName',
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.vehicleNo' }),
			dataIndex: ['orderVehicle', 'vehicleNo'],
			key: 'vehicleNo',
			render: (_, record) => {
				return record.orderVehicle?.vehicleNo
					? '第' + record.orderVehicle?.vehicleNo + '车'
					: '-';
			},
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.shippingDate' }),
			dataIndex: 'shippingDate',
			key: 'shippingDate',
			valueType: 'date',
		},
		{
			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 'WAIT_SHIPMENT':
						color = 'default';
						break;
					case 'WAIT_PAYMENT':
						color = 'default';
						break;
					case 'PARTIAL_PAYMENT':
						color = 'processing';
						break;
					case 'FULL_PAYMENT':
						color = 'success';
						break;
					case 'REJECT_FINISH':
						color = 'error';
						break;
					case 'FINISH':
						color = 'success';
						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.OrderShipVO,
		BizValueType
	>[] = columns as ProDescriptionsItemProps<
		BusinessAPI.OrderShipVO,
		BizValueType
	>[];

	return (
		<BizContainer<
			typeof business.orderShip,
			BusinessAPI.OrderShipVO,
			BusinessAPI.OrderShipPageQry,
			BusinessAPI.OrderShipCreateCmd,
			BusinessAPI.OrderShipUpdateCmd
		>
			rowKey={'orderShipId'}
			permission={'operation-ship-order'}
			func={business.orderShip}
			method={'orderShip'}
			methodUpper={'OrderShip'}
			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.OrderShipVO['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: 'WAIT_SHIPMENT',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.waitShipment',
									}),
								},
								{
									key: 'WAIT_PAYMENT',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.waitPayment',
									}),
								},
								{
									key: 'PARTIAL_PAYMENT',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.partialPayment',
									}),
								},
								{
									key: 'FULL_PAYMENT',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.fullPayment',
									}),
								},
								{
									key: 'REJECT_FINISH',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.rejectFinish',
									}),
								},
								{
									key: 'FINISH',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.finish',
									}),
								},
							],
							onChange: (key) => {
								setActiveKey(key as string);
							},
						},
					},
				},
				columns,
			}}
			create={false}
			update={false}
			destroy={false}
			detail={{
				rowId: orderShipId,
				formType: 'drawer',
				columns: detailColumns,
				trigger,
			}}
		/>
	);
}
