import { ButtonAccess, InsertPosition } from '@/components';
import { business } from '@/services';
import { useIntl } from '@@/exports';
import {
	DrawerForm,
	ProFormDependency,
	ProFormDigit,
	ProFormSwitch,
	ProFormText,
	RouteContext,
	RouteContextType,
} from '@ant-design/pro-components';
import { Card, Divider, message, Space } from 'antd';

interface IDealerPurchaseSettingProps {
	insertPosition?: InsertPosition;
	dealerVO: BusinessAPI.DealerVO;
	onFinish?: () => void;
}

interface IFeatureCardProps {
	title: string;
	description?: string;
	name: string;
	children?: React.ReactNode;
	checkedChildren?: string;
	unCheckedChildren?: string;
}

// 功能卡片组件
const FeatureCard: React.FC<IFeatureCardProps> = ({
	title,
	description,
	name,
	children,
	checkedChildren,
	unCheckedChildren,
}) => {
	return (
		<Card
			size="small"
			style={{ marginBottom: 16 }}
			bodyStyle={{ padding: '16px 20px' }}
		>
			<div
				style={{
					display: 'flex',
					alignItems: 'flex-start',
					justifyContent: 'space-between',
				}}
			>
				<div style={{ flex: 1, marginRight: 16 }}>
					<div style={{ fontSize: 14, fontWeight: 500, marginBottom: 4 }}>
						{title}
					</div>
					{description && (
						<div style={{ fontSize: 12, color: '#666', lineHeight: 1.4 }}>
							{description}
						</div>
					)}
				</div>
				<div style={{ display: 'flex', alignItems: 'center' }}>
					<ProFormSwitch
						name={name}
						fieldProps={{
							checkedChildren,
							unCheckedChildren,
						}}
						noStyle
					/>
				</div>
			</div>
			{children && (
				<>
					<Divider style={{ margin: '16px 0 12px 0' }} />
					<div style={{ paddingLeft: 0 }}>{children}</div>
				</>
			)}
		</Card>
	);
};

const defaultDealerSetting = {
	shareRatio: 50,
	enableShare: false,
	shareAdjusted: false,
	accrualTaxRatio: null,
	freightCostFlag: false,
	enableAccrualTax: false,
	strawMatCostFlag: false,
	companyRebateRatio: null,
	includeFreightFlag: false,
	includePackingFlag: false,
	enableCompanyRebate: false,
	enableLoss: false,
	lossAmount: null,
	enableInitialTrainNo: false,
	initialTrainNo: null,
};

export default function DealerPurchaseSetting(
	props: IDealerPurchaseSettingProps,
) {
	const { onFinish, dealerVO } = props;
	const intl = useIntl();
	const intlPrefix = 'dealer';
	const commonYes = intl.formatMessage({ id: 'common.yes' });
	const commonNo = intl.formatMessage({ id: 'common.no' });

	return (
		<RouteContext.Consumer>
			{(value: RouteContextType) => {
				const { isMobile } = value;
				return (
					<DrawerForm<BusinessAPI.DealerVO, BusinessAPI.DealerVO>
						trigger={
							<ButtonAccess permission={''} type="link">
								采购设置
							</ButtonAccess>
						}
						title="采购设置"
						layout="vertical"
						width={isMobile ? '100%' : '45%'}
						drawerProps={{
							destroyOnHidden: true,
						}}
						request={async () => {
							const { data } = await business.dealer.showDealer({
								dealerShowQry: {
									dealerId: dealerVO.dealerId,
								},
							});

							return {
								...data!,
							};
						}}
						onFinish={async (formData) => {
							await business.dealer.updateDealerSettings({
								dealerId: formData.dealerId!,
								enableLoss:
									formData?.enableLoss || defaultDealerSetting.enableLoss,
								lossAmount:
									formData?.lossAmount || defaultDealerSetting.lossAmount,
								shareRatio:
									formData?.shareRatio || defaultDealerSetting.shareRatio,
								enableShare:
									formData?.enableShare || defaultDealerSetting.enableShare,
								shareAdjusted:
									formData?.shareAdjusted || defaultDealerSetting.shareAdjusted,
								initialTrainNo:
									formData?.initialTrainNo ||
									defaultDealerSetting.initialTrainNo,
								accrualTaxRatio:
									formData?.accrualTaxRatio ||
									defaultDealerSetting.accrualTaxRatio,
								freightCostFlag:
									formData?.freightCostFlag ||
									defaultDealerSetting.freightCostFlag,
								enableAccrualTax:
									formData?.enableAccrualTax ||
									defaultDealerSetting.enableAccrualTax,
								strawMatCostFlag:
									formData?.strawMatCostFlag ||
									defaultDealerSetting.strawMatCostFlag,
								companyRebateRatio:
									formData?.companyRebateRatio ||
									defaultDealerSetting.companyRebateRatio,
								includeFreightFlag:
									formData?.includeFreightFlag ||
									defaultDealerSetting.includeFreightFlag,
								includePackingFlag:
									formData?.includePackingFlag ||
									defaultDealerSetting.includePackingFlag,
								enableCompanyRebate:
									formData?.enableCompanyRebate ||
									defaultDealerSetting.enableCompanyRebate,
								enableInitialTrainNo:
									formData?.enableInitialTrainNo ||
									defaultDealerSetting.enableInitialTrainNo,
							} as any);

							onFinish?.();
							message.success('更新经销商设置成功');

							return true;
						}}
					>
						<ProFormText name={'dealerId'} hidden={true} />

						{/* 诚信志远分成功能卡片 */}
						<FeatureCard
							title={intl.formatMessage({
								id: intlPrefix + '.form.enableShare.label',
							})}
							description="是否开启诚信志远分红比例设置"
							name="enableShare"
							checkedChildren={commonYes}
							unCheckedChildren={commonNo}
						>
							<ProFormDependency name={['enableShare']}>
								{({ enableShare }) => {
									return enableShare ? (
										<Space
											direction="vertical"
											size="middle"
											style={{ width: '100%' }}
										>
											<ProFormDigit
												name={'shareRatio'}
												label={intl.formatMessage({
													id: intlPrefix + '.form.shareRatio.label',
												})}
												fieldProps={{
													precision: 2,
													step: 0.01,
													min: 0,
													max: 100,
													suffix: '%',
												}}
												placeholder={intl.formatMessage({
													id: intlPrefix + '.form.shareRatio.placeholder',
												})}
												dependencies={['enableShare']}
												rules={[
													{
														required: true,
														message: intl.formatMessage({
															id: intlPrefix + '.form.shareRatio.required',
														}),
													},
												]}
											/>
											<ProFormSwitch
												name={'shareAdjusted'}
												label={intl.formatMessage({
													id: intlPrefix + '.form.shareAdjusted.label',
												})}
												fieldProps={{
													checkedChildren: commonYes,
													unCheckedChildren: commonNo,
												}}
											/>
										</Space>
									) : null;
								}}
							</ProFormDependency>
						</FeatureCard>

						{/* 成本相关功能卡片 */}
						<FeatureCard
							title={intl.formatMessage({
								id: intlPrefix + '.form.freightCostFlag.label',
							})}
							description="运费是否作为成本计算"
							name="freightCostFlag"
							checkedChildren={commonYes}
							unCheckedChildren={commonNo}
						/>

						<FeatureCard
							title={intl.formatMessage({
								id: intlPrefix + '.form.strawMatCostFlag.label',
							})}
							description="草帘是否作为成本计算"
							name="strawMatCostFlag"
							checkedChildren={commonYes}
							unCheckedChildren={commonNo}
						/>

						<FeatureCard
							title={intl.formatMessage({
								id: intlPrefix + '.form.includePackingFlag.label',
							})}
							description="发货单合计金额是否含包装费"
							name="includePackingFlag"
							checkedChildren={commonYes}
							unCheckedChildren={commonNo}
						/>

						<FeatureCard
							title={intl.formatMessage({
								id: intlPrefix + '.form.includeFreightFlag.label',
							})}
							description="发货单合计金额是否含运费"
							name="includeFreightFlag"
							checkedChildren={commonYes}
							unCheckedChildren={commonNo}
						/>

						{/* 计提税金功能卡片 */}
						<FeatureCard
							title={intl.formatMessage({
								id: intlPrefix + '.form.enableAccrualTax.label',
							})}
							description="是否开启计提税金功能"
							name="enableAccrualTax"
							checkedChildren={commonYes}
							unCheckedChildren={commonNo}
						>
							<ProFormDependency name={['enableAccrualTax']}>
								{({ enableAccrualTax }) => {
									return enableAccrualTax ? (
										<ProFormDigit
											name={'accrualTaxRatio'}
											label={intl.formatMessage({
												id: intlPrefix + '.form.accrualTaxRatio.label',
											})}
											fieldProps={{
												precision: 2,
												step: 0.01,
												min: 0,
												max: 100,
												suffix: '%',
											}}
											placeholder={intl.formatMessage({
												id: intlPrefix + '.form.accrualTaxRatio.placeholder',
											})}
											dependencies={['enableAccrualTax']}
											rules={[
												{
													required: true,
													message: intl.formatMessage({
														id: intlPrefix + '.form.accrualTaxRatio.required',
													}),
												},
											]}
										/>
									) : null;
								}}
							</ProFormDependency>
						</FeatureCard>

						{/* 公司返点功能卡片 */}
						<FeatureCard
							title={intl.formatMessage({
								id: intlPrefix + '.form.enableCompanyRebate.label',
							})}
							description="是否开启公司返点功能"
							name="enableCompanyRebate"
							checkedChildren={commonYes}
							unCheckedChildren={commonNo}
						>
							<ProFormDependency name={['enableCompanyRebate']}>
								{({ enableCompanyRebate }) => {
									return enableCompanyRebate ? (
										<ProFormDigit
											name={'companyRebateRatio'}
											label={intl.formatMessage({
												id: intlPrefix + '.form.companyRebateRatio.label',
											})}
											fieldProps={{
												precision: 2,
												step: 0.01,
												min: 0,
												max: 100,
												suffix: '%',
											}}
											placeholder={intl.formatMessage({
												id: intlPrefix + '.form.companyRebateRatio.placeholder',
											})}
											dependencies={['enableCompanyRebate']}
											rules={[
												{
													required: true,
													message: intl.formatMessage({
														id:
															intlPrefix + '.form.companyRebateRatio.required',
													}),
												},
											]}
										/>
									) : null;
								}}
							</ProFormDependency>
						</FeatureCard>

						{/* 损耗功能卡片 */}
						<FeatureCard
							title={intl.formatMessage({
								id: intlPrefix + '.form.enableLoss.label',
							})}
							description="是否开启损耗设置"
							name="enableLoss"
							checkedChildren={commonYes}
							unCheckedChildren={commonNo}
						>
							<ProFormDependency name={['enableLoss']}>
								{({ enableLoss }) => {
									return enableLoss ? (
										<ProFormDigit
											name={'lossAmount'}
											label={intl.formatMessage({
												id: intlPrefix + '.form.lossAmount.label',
											})}
											fieldProps={{
												precision: 2,
												step: 0.01,
												min: 0,
												max: 9999,
												suffix: '元',
											}}
											placeholder={intl.formatMessage({
												id: intlPrefix + '.form.lossAmount.placeholder',
											})}
											dependencies={['enableLoss']}
											rules={[
												{
													required: true,
													message: intl.formatMessage({
														id: intlPrefix + '.form.lossAmount.required',
													}),
												},
											]}
										/>
									) : null;
								}}
							</ProFormDependency>
						</FeatureCard>

						{/* 初始车次号功能卡片 */}
						<FeatureCard
							title={intl.formatMessage({
								id: intlPrefix + '.form.enableInitialTrainNo.label',
							})}
							description="是否开启初始车次号设置"
							name="enableInitialTrainNo"
							checkedChildren={commonYes}
							unCheckedChildren={commonNo}
						>
							<ProFormDependency name={['enableInitialTrainNo']}>
								{({ enableInitialTrainNo }) => {
									return enableInitialTrainNo ? (
										<ProFormDigit
											name={'initialTrainNo'}
											label={intl.formatMessage({
												id: intlPrefix + '.form.initialTrainNo.label',
											})}
											placeholder={intl.formatMessage({
												id: intlPrefix + '.form.initialTrainNo.placeholder',
											})}
											fieldProps={{
												step: 1,
												min: 0,
												max: 9999,
												suffix: '号',
											}}
											dependencies={['enableInitialTrainNo']}
											rules={[
												{
													required: true,
													message: intl.formatMessage({
														id: intlPrefix + '.form.initialTrainNo.required',
													}),
												},
											]}
										/>
									) : null;
								}}
							</ProFormDependency>
						</FeatureCard>
					</DrawerForm>
				);
			}}
		</RouteContext.Consumer>
	);
}
