import { MaterialList } from '@/components';
import { business } from '@/services';
import { formatParam } from '@/utils/formatParam';
import { formLayout } from '@/utils/formLayout';
import { ActionType, ProForm, ProFormText } from '@ant-design/pro-components';
import { ProFormUploadMaterial } from '@chageable/components';
import { Col, message, Row, Space } from 'antd';
import { useRef } from 'react';

export default function WxMaConfig() {
	const actionRef = useRef<ActionType>();
	return (
		<ProForm<BusinessAPI.WxMaConfigValue>
			{...formLayout()}
			request={async () => {
				const { data } = await business.setting.showSetting({
					settingShowQry: {
						settingKey: 'WX_MP_CONFIG',
					},
				});

				const wxMaConfigValue =
					data?.settingValue as BusinessAPI.WxMaConfigValue;
				return {
					...wxMaConfigValue,
					wxMaLogoList: [wxMaConfigValue?.wxMaLogo],
				} as BusinessAPI.WxMaConfigValue;
			}}
			onFinish={async (values) => {
				await business.setting.updateSetting({
					settingKey: 'WX_MP_CONFIG',
					settingValue: {
						...values,
						settingKey: 'WX_MP_CONFIG',
					},
				});

				message.success('更新公众号配置成功');
			}}
			submitter={{
				render: (_, dom) => {
					return (
						<Row>
							<Col span={14} offset={4}>
								<Space>{dom}</Space>
							</Col>
						</Row>
					);
				},
			}}
		>
			<ProFormText hidden={true} name={'settingId'} />
			<ProFormText
				name="wxMpName"
				label="公众号名称"
				placeholder={'请输入公众号名称'}
				required={true}
				rules={[
					{
						required: true,
						message: '公众号名称为必填项',
					},
				]}
			/>
			<ProFormText
				name="appId"
				label="AppID(公众号ID)"
				placeholder={'请输入公众号AppID'}
				required={true}
				rules={[
					{
						required: true,
						message: '公众号AppID为必填项',
					},
				]}
			/>
			<ProFormText.Password
				name="appSecret"
				label="AppSecret(公众号密钥)"
				placeholder={'请输入公众号AppSecret'}
				required={true}
				rules={[
					{
						required: true,
						message: '公众号AppSecret为必填项',
					},
				]}
			/>
			<ProFormText
				name="wxMpOriginalId"
				label="公众号原始ID"
				placeholder={'请输入公众号原始ID'}
				required={true}
				rules={[
					{
						required: true,
						message: '公众号原始ID为必填项',
					},
				]}
			/>
			<ProFormUploadMaterial
				key={'wxMpLogo'}
				label={'公众号Logo'}
				name={'wxMpLogoList'}
				transform={(value) => {
					return {
						wxMaLogo: value[0],
					};
				}}
				fieldProps={{
					maxCount: 1,
					actionRef: actionRef,
					toolBarRender: () => [
						<MaterialList
							key={'create'}
							ghost={true}
							mode={'create'}
							search={false}
							onValueChange={() => actionRef.current?.reload()}
						/>,
					],
					request: async (params, sorter, filter) => {
						const { data, success, totalCount } =
							await business.material.pageMaterial({
								materialPageQry: formatParam<typeof params>(
									params,
									sorter,
									filter,
								),
							});

						return {
							data: data || [],
							total: totalCount,
							success,
						};
					},
				}}
			/>
		</ProForm>
	);
}
