import { BizValueType } from '@/components';
import BizProvider from '@/components/Biz/BizProvider';
import { useIntl } from '@@/exports';
import {
	ProColumns,
	ProTable,
	ProTableProps,
} from '@ant-design/pro-components';
import { Tag } from 'antd';

type IOrderSupplierSelectListProps = ProTableProps<
	BusinessAPI.OrderSupplierVO,
	BusinessAPI.OrderSupplierPageQry,
	BizValueType
>;

export default function OrderSupplierSelectList(
	props: IOrderSupplierSelectListProps,
) {
	const intl = useIntl();
	const intlPrefix = 'orderSupplier';
	const { ...tableProps } = props;
	// 订单列表列定义
	const orderColumns: ProColumns<BusinessAPI.OrderSupplierVO, 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.invoiceAmount' }),
				dataIndex: 'invoiceAmount',
				key: 'invoiceAmount',
				valueType: 'money',
				search: false,
			},
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.depositAmount' }),
				dataIndex: 'depositAmount',
				key: 'depositAmount',
				valueType: 'money',
				search: false,
			},
			// 剩余付款金额
			{
				title: intl.formatMessage({
					id: intlPrefix + '.column.remainingAmount',
				}),
				dataIndex: 'remainingAmount',
				key: 'remainingAmount',
				valueType: 'money',
				search: false,
				renderText: (_, record) => {
					return record.invoiceAmount - record.depositAmount || 0;
				},
			},
			{
				title: intl.formatMessage({
					id: intlPrefix + '.column.supplierInvoice',
				}),
				dataIndex: 'supplierInvoiceVO',
				key: 'supplierInvoiceId',
				search: false,
				valueType: 'supplierInvoice',
			},
			{
				title: intl.formatMessage({ id: intlPrefix + '.column.invoiceUpload' }),
				dataIndex: 'invoiceUpload',
				key: 'invoiceUpload',
				search: false,
				valueType: 'switch',
				render: (_, record) => (
					<Tag color={record.invoiceUpload ? 'green' : 'red'}>
						{record.invoiceUpload
							? intl.formatMessage({
									id: intlPrefix + '.column.invoiceUpload.uploaded',
								})
							: intl.formatMessage({
									id: intlPrefix + '.column.invoiceUpload.notUploaded',
								})}
					</Tag>
				),
			},
			{
				title: '开票状态',
				key: 'invoiceStatus',
				render: (_, orderSupplierVO: BusinessAPI.OrderSupplierVO) => {
					let invoiceStatus = 'NOT_INVOICE';
					if (orderSupplierVO.invoiceUpload && orderSupplierVO.invoiceId) {
						if (
							orderSupplierVO.supplierId ===
							orderSupplierVO.supplierInvoiceVO?.supplierId
						) {
							invoiceStatus = 'SELF';
						}
						if (
							orderSupplierVO.supplierId !==
							orderSupplierVO.supplierInvoiceVO?.supplierId
						) {
							invoiceStatus = 'AGENT';
						}
					} else {
						invoiceStatus = 'NOT_INVOICE';
					}

					switch (invoiceStatus) {
						case 'SELF':
							return '自开票';
						case 'AGENT':
							return '代开票';
						case 'NOT_INVOICE':
							return '未开票';
						default:
					}
				},
			},
			{
				title: intl.formatMessage({
					id: intlPrefix + '.column.contractUpload',
				}),
				dataIndex: 'contractUpload',
				key: 'contractUpload',
				search: false,
				valueType: 'switch',
				render: (_, record) => (
					<Tag color={record.contractUpload ? 'green' : 'red'}>
						{record.contractUpload
							? intl.formatMessage({
									id: intlPrefix + '.column.contractUpload.uploaded',
								})
							: intl.formatMessage({
									id: intlPrefix + '.column.contractUpload.notUploaded',
								})}
					</Tag>
				),
			},
			{
				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',
					},
				},
			},
			...(tableProps.columns || []),
		];

	return (
		<BizProvider>
			<ProTable
				ghost={true}
				search={false}
				options={false}
				{...tableProps}
				rowKey="orderSupplierId"
				columns={orderColumns}
				pagination={false}
				size="small"
				scroll={{
					x: 'max-content',
				}}
			/>
		</BizProvider>
	);
}
