import {
	CompanyList,
	DealerFormItem,
	DealerList,
	OrderCostList,
	OrderFormItem,
	OrderList,
	PaymentTaskFormItem,
	PaymentTaskList,
	Remark,
	RemarkFormItem,
	SupplierFarmerList,
	SupplierFormItem,
	SupplierInvoiceList,
	UserFormItem,
	UserList,
} from '@/components';
import { business } from '@/services';
import groupby from '@/utils/groupby';
import { useIntl } from '@@/exports';
import { UserOutlined } from '@ant-design/icons';
import { ProFormSelect, ProProvider } from '@ant-design/pro-components';
import { Avatar, Space } from 'antd';
import { useContext } from 'react';

export default function BizProvider(props: any) {
	const values = useContext(ProProvider);
	const intl = useIntl();
	return (
		<ProProvider.Provider
			value={{
				...values,
				valueTypeMap: {
					dealer: {
						renderFormItem: (_, props) => {
							return <DealerFormItem {...props} {...props?.fieldProps} />;
						},
						render: (dealerVO: BusinessAPI.DealerVO) => {
							return (
								dealerVO && (
									<DealerList
										ghost={true}
										mode={'detail'}
										dealerId={dealerVO.dealerId}
										trigger={() => (
											<Space>
												<a>{`${dealerVO?.dealerType === 'MARKET' ? '市场' : '超市'} | ${dealerVO?.shortName}`}</a>
											</Space>
										)}
									/>
								)
							);
						},
					},
					company: {
						renderFormItem: (_, props) => {
							return (
								<ProFormSelect
									{...props}
									{...props?.fieldProps}
									request={async (params) => {
										const { data } = await business.company.listCompany({
											companyListQry: {
												...params,
											},
										});

										return (
											data?.map((item) => {
												return {
													label: `${item.shortName} | ${item.fullName}`,
													value: item.companyId,
												};
											}) || []
										);
									}}
								/>
							);
						},
						render: (companyVO: BusinessAPI.CompanyVO) => {
							return (
								companyVO && (
									<CompanyList
										ghost={true}
										mode={'detail'}
										companyId={companyVO.companyId}
										trigger={() => (
											<Space>
												<a>
													{companyVO.shortName} | {companyVO.fullName}
												</a>
											</Space>
										)}
									/>
								)
							);
						},
					},
					supplierInvoice: {
						renderFormItem: (_, props) => {
							return (
								<ProFormSelect
									{...props}
									{...props?.fieldProps}
									request={async (params) => {
										const { data } =
											await business.supplierInvoice.listSupplierInvoice({
												supplierInvoiceListQry: {
													...params,
												},
											});

										return (
											data?.map((item) => {
												return {
													label: `${item.supplierName} | ${item.invoiceSn}`,
													value: item.supplierInvoiceId,
												};
											}) || []
										);
									}}
								/>
							);
						},
						render: (supplierInvoiceVO: BusinessAPI.SupplierInvoiceVO) => {
							return (
								<SupplierInvoiceList
									ghost={true}
									mode={'detail'}
									supplierInvoiceId={supplierInvoiceVO.supplierInvoiceId}
									trigger={() => (
										<Space>
											<a>
												{`${supplierInvoiceVO.supplierName} | ${supplierInvoiceVO.invoiceSn}`}
											</a>
										</Space>
									)}
								/>
							);
						},
					},
					orderCost: {
						renderFormItem: (_, props) => {
							return (
								<ProFormSelect
									{...props}
									{...props?.fieldProps}
									request={async () => {
										const { data } = await business.cost.listCost({
											costListQry: {},
										});

										const costGroup = groupby(data || [], (item) => item.type);

										const options = Object.keys(costGroup).map((key) => ({
											label: intl.formatMessage({
												id:
													'cost.column.type.enum.' +
													key
														.toLowerCase()
														.replace(/_([a-z])/g, (_, char) =>
															char.toUpperCase(),
														),
											}),
											title: key,
											options: costGroup[key].map((item) => ({
												label: item.name,
												value: item.costId,
											})),
										}));

										return options || [];
									}}
								/>
							);
						},
						render: (orderCostVO: BusinessAPI.OrderCostVO) => {
							return (
								orderCostVO && (
									<OrderCostList
										ghost={true}
										mode={'detail'}
										orderCostId={orderCostVO.orderCostId}
										trigger={() => (
											<Space>
												<a>{`${orderCostVO.principal} | ${orderCostVO.name}`}</a>
											</Space>
										)}
									/>
								)
							);
						},
					},
					paymentTask: {
						renderFormItem: (_, props) => {
							return <PaymentTaskFormItem {...props} {...props?.fieldProps} />;
						},
						render: (paymentTaskVO: BusinessAPI.PaymentTaskVO) => {
							console.log('paymentTaskVO', paymentTaskVO);
							return (
								paymentTaskVO && (
									<PaymentTaskList
										ghost={true}
										mode={'detail'}
										paymentTaskId={paymentTaskVO.paymentTaskId}
										trigger={() => (
											<Space>
												<a>{paymentTaskVO.paymentTaskSn}</a>
											</Space>
										)}
									/>
								)
							);
						},
					},
					supplier: {
						renderFormItem: (_, props) => {
							return (
								<SupplierFormItem
									{...props}
									{...props?.fieldProps}
									params={{
										type: 'FARMER',
									}}
								/>
							);
						},
						render: (supplierVO: BusinessAPI.SupplierVO) => {
							return (
								supplierVO && (
									<SupplierFarmerList
										ghost={true}
										mode={'detail'}
										supplierId={supplierVO.supplierId}
										trigger={() => (
											<Space>
												<a>{supplierVO.name}</a>
											</Space>
										)}
									/>
								)
							);
						},
					},
					order: {
						renderFormItem: (_, props) => {
							return <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>
										)}
									/>
								)
							);
						},
					},
					user: {
						renderFormItem: (_, props) => {
							return <UserFormItem {...props} {...props?.fieldProps} />;
						},
						render: (userVO: BusinessAPI.UserVO) => {
							return (
								userVO && (
									<UserList
										ghost={true}
										mode={'detail'}
										userId={userVO.userId}
										trigger={() => (
											<Space>
												<Avatar
													src={userVO?.avatar}
													icon={<UserOutlined />}
													size={24}
												/>
												<a>{userVO.nickname || userVO.name}</a>
											</Space>
										)}
									/>
								)
							);
						},
					},
					remark: {
						renderFormItem: (_, props) => {
							return <RemarkFormItem {...props} {...props?.fieldProps} />;
						},
						render: (remark: string, props) => {
							// @ts-ignore
							const { record } = props;
							return (
								<Remark remark={remark} {...record} {...props?.fieldProps} />
							);
						},
					},
				},
			}}
		>
			{props.children}
		</ProProvider.Provider>
	);
}
