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

interface IOrderRebateListProps {
	ghost?: boolean;
	orderRebateId?: BusinessAPI.OrderRebateVO['orderRebateId'];
	search?: boolean;
	onValueChange?: () => void;
	mode?: ModeType;
	trigger?: () => React.ReactNode;
}

export default function OrderRebateList(props: IOrderRebateListProps) {
	const {
		ghost = false,
		orderRebateId,
		search = true,
		mode = 'page',
		trigger,
		onValueChange,
	} = props;
	const intl = useIntl();
	const intlPrefix = 'orderRebate';

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

	const columns: ProColumns<BusinessAPI.OrderRebateVO, BizValueType>[] = [
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.order' }),
			dataIndex: 'orderVO',
			key: 'orderId',
			valueType: 'order',
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.name' }),
			dataIndex: 'name',
			key: 'name',
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.calcMethod' }),
			dataIndex: 'calcMethod',
			key: 'calcMethod',
			search: false,
			valueType: 'select',
			valueEnum: {
				NET_WEIGHT: {
					text: intl.formatMessage({
						id: intlPrefix + '.column.calcMethod.netWeight',
					}),
				},
				NOT_FIXED: {
					text: intl.formatMessage({
						id: intlPrefix + '.column.calcMethod.notFixed',
					}),
				},
				FIXED_AMOUNT: {
					text: intl.formatMessage({
						id: intlPrefix + '.column.calcMethod.fixedAmount',
					}),
				},
			},
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.netWeight' }),
			dataIndex: 'netWeight',
			key: 'netWeight',
			search: false,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.unitPrice' }),
			dataIndex: 'unitPrice',
			key: 'unitPrice',
			valueType: 'money',
			search: false,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.amount' }),
			dataIndex: 'amount',
			key: 'amount',
			valueType: 'money',
			search: false,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.orderCompany' }),
			dataIndex: 'orderCompany',
			valueType: 'select',
			request: async (params) => {
				const { data } = await business.company.listCompany({
					companyListQry: {
						...params,
					},
				});

				return (
					data?.map((item) => {
						return {
							label: item.fullName,
							value: item.companyId,
						};
					}) || []
				);
			},
			render: (_, record) => {
				return (
					<CompanyList
						ghost={true}
						mode={'detail'}
						companyId={record.orderCompany.companyId}
						trigger={() => <a href={'#'}>{record.orderCompany.fullName}</a>}
					/>
				);
			},
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.isPaid' }),
			dataIndex: 'isPaid',
			key: 'isPaid',
			valueType: 'select',
			valueEnum: {
				true: {
					text: intl.formatMessage({
						id: intlPrefix + '.column.isPaid.paid',
					}),
					status: 'success',
				},
				false: {
					text: intl.formatMessage({
						id: intlPrefix + '.column.isPaid.unpaid',
					}),
					status: 'processing',
				},
			},
		},
	];

	const [selectedRows, setSelectedRows] = useState<BusinessAPI.OrderRebateVO[]>(
		[],
	);

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

	return (
		<BizContainer<
			typeof business.orderRebate,
			BusinessAPI.OrderRebateVO,
			BusinessAPI.OrderRebatePageQry
		>
			rowKey={'orderRebateId'}
			permission={'operation-order-cost'}
			func={business.orderRebate}
			method={'orderRebate'}
			methodUpper={'OrderRebate'}
			intlPrefix={intlPrefix}
			modeType={mode}
			onValueChange={onValueChange}
			container={{
				ghost,
			}}
			status={false}
			page={{
				fieldProps: {
					bordered: true,
					ghost,
					//@ts-ignore
					search,
					params: {
						...(activeKey !== 'ALL' && {
							isPaid: activeKey! as any,
						}),
						poStates: ['COMPLETED'],
					},
					rowSelection: {
						onChange: (_, selectedRows) => setSelectedRows(selectedRows),
					},
					tableAlertOptionRender: (props) => {
						return (
							<Space>
								<Button
									key={'cancel'}
									type={'link'}
									onClick={() => props.onCleanSelected()}
									size={'middle'}
								>
									取消选择
								</Button>
							</Space>
						);
					},
					toolbar: {
						menu: {
							type: 'tab',
							activeKey: activeKey,
							items: [
								// 全部
								{
									key: 'ALL',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.all',
									}),
								},
								// 已支付
								{
									key: 'true',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.paid',
									}),
								},
								// 未支付
								{
									key: 'false',
									label: intl.formatMessage({
										id: intlPrefix + '.tab.unpaid',
									}),
								},
							],
							onChange: (key) => {
								setActiveKey(key as string);
							},
						},
					},
				},
				columns,
			}}
			create={false}
			update={false}
			destroy={false}
			detail={{
				rowId: orderRebateId,
				formType: 'drawer',
				columns: detailColumns,
				trigger,
			}}
		/>
	);
}
