import { BizContainer, BizValueType, ModeType } from '@/components';
import { business } from '@/services';
import { formatCurrency } from '@/utils/format';
import { useIntl } from '@@/exports';
import { ArrowDownOutlined, ArrowUpOutlined } from '@ant-design/icons';
import { ProColumns } from '@ant-design/pro-components';
import { ProDescriptionsItemProps } from '@ant-design/pro-descriptions';
import { Tag } from 'antd';
import React from 'react';

interface IDealerAccountRecordListProps {
	ghost?: boolean;
	dealerVO?: BusinessAPI.DealerVO;
	orderVO?: BusinessAPI.OrderVO;
	dealerAccountRecordId?: BusinessAPI.DealerAccountRecordVO['dealerAccountRecordId'];
	search?: boolean;
	onValueChange?: () => void;
	mode?: ModeType;
	trigger?: () => React.ReactNode;
}

export default function DealerAccountRecordList(
	props: IDealerAccountRecordListProps,
) {
	const {
		ghost = false,
		dealerVO,
		orderVO,
		dealerAccountRecordId,
		search = true,
		mode = 'page',
		trigger,
		onValueChange,
	} = props;
	const intl = useIntl();
	const intlPrefix = 'dealerAccountRecord';

	// 变动类型映射
	const targetTypeMap: Record<string, { label: string; color: string }> = {
		ORIGIN_PURCHASE_SHIP: {
			label: intl.formatMessage({
				id: intlPrefix + '.targetType.originPurchaseShip',
			}),
			color: 'blue',
		},
		MARKET_PURCHASE_SHIP: {
			label: intl.formatMessage({
				id: intlPrefix + '.targetType.marketPurchaseShip',
			}),
			color: 'green',
		},
		MARKET_TRANSFER_SHIP: {
			label: intl.formatMessage({
				id: intlPrefix + '.targetType.marketTransferShip',
			}),
			color: 'orange',
		},
	};

	const columns: ProColumns<BusinessAPI.DealerAccountRecordVO, BizValueType>[] =
		[
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.dealer' }),
				dataIndex: 'dealerVO',
				key: 'dealerId',
				valueType: 'dealer',
				hidden: !!dealerVO, // 如果已传入 dealerId，则隐藏此列
			},
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.order' }),
				dataIndex: 'orderVO',
				key: 'orderId',
				valueType: 'order',
			},
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.orderShip' }),
				dataIndex: ['orderShipVO', 'orderSn'],
				key: 'targetId',
			},
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.recordSn' }),
				dataIndex: 'recordSn',
				key: 'recordSn',
			},
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.targetType' }),
				dataIndex: 'targetType',
				key: 'targetType',
				valueType: 'select',
				valueEnum: Object.entries(targetTypeMap).reduce(
					(acc, [key, value]) => ({
						...acc,
						[key]: { text: value.label, status: value.color as any },
					}),
					{},
				),
				render: (_, record) => {
					const typeInfo = targetTypeMap[record.targetType];
					return typeInfo ? (
						<Tag color={typeInfo.color}>{typeInfo.label}</Tag>
					) : (
						<Tag>{record.targetType}</Tag>
					);
				},
			},
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.beforeAmount' }),
				dataIndex: 'beforeAmount',
				key: 'beforeAmount',
				valueType: 'money',
				search: false,
				render: (_, record) => (
					<span className="text-gray-500">
						￥{formatCurrency(record.beforeAmount)}
					</span>
				),
			},
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.amount' }),
				dataIndex: 'amount',
				key: 'amount',
				valueType: 'money',
				search: false,
				render: (_, record) => (
					<span
						className={record.amount >= 0 ? 'text-green-600' : 'text-red-600'}
					>
						{record.amount >= 0 ? <ArrowUpOutlined /> : <ArrowDownOutlined />}
						<span className="ml-1">
							￥{formatCurrency(Math.abs(record.amount))}
						</span>
					</span>
				),
			},
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.afterAmount' }),
				dataIndex: 'afterAmount',
				key: 'afterAmount',
				valueType: 'money',
				search: false,
				render: (_, record) => (
					<span className="text-blue-600 font-medium">
						￥{formatCurrency(record.afterAmount)}
					</span>
				),
			},
		];

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

	return (
		<BizContainer<
			typeof business.dealerAccountRecord,
			BusinessAPI.DealerAccountRecordVO,
			BusinessAPI.DealerAccountRecordPageQry
		>
			rowKey={'dealerAccountRecordId'}
			permission={'operation-dealer-account-record'}
			func={business.dealerAccountRecord}
			method={'dealerAccountRecord'}
			methodUpper={'DealerAccountRecord'}
			intlPrefix={intlPrefix}
			modeType={mode}
			onValueChange={onValueChange}
			container={{
				ghost,
			}}
			remark={{
				mode: 'editor',
			}}
			status={false}
			page={{
				fieldProps: {
					bordered: true,
					ghost,
					//@ts-ignore
					search,
					params: {
						...(dealerVO && { dealerId: dealerVO.dealerId }),
						...(orderVO && { orderId: orderVO.orderId }),
					},
				},
				columns,
				options: () => [],
			}}
			create={false}
			update={false}
			destroy={false}
			detail={{
				rowId: dealerAccountRecordId,
				formType: 'drawer',
				columns: detailColumns,
				trigger,
			}}
		/>
	);
}
