import {
	BizContainer,
	BizValueType,
	ProFormUploadMaterial,
} from '@/components';
import { business } from '@/services';
import { useIntl } from '@@/exports';
import {
	ProColumns,
	ProFormText,
	ProFormTextArea,
} from '@ant-design/pro-components';
import { ProDescriptionsItemProps } from '@ant-design/pro-descriptions';

export default function ChannelList() {
	const intl = useIntl();
	const intlPrefix = 'channel';

	const formContext = [
		<ProFormText
			key={'title'}
			name={'title'}
			label={intl.formatMessage({ id: intlPrefix + '.form.title.label' })}
			required={true}
			placeholder={intl.formatMessage({
				id: intlPrefix + '.form.title.placeholder',
			})}
			rules={[
				{
					required: true,
					message: intl.formatMessage({
						id: intlPrefix + '.form.title.required',
					}),
				},
			]}
		/>,
		<ProFormTextArea
			key={'subTitle'}
			name={'subTitle'}
			label={intl.formatMessage({ id: intlPrefix + '.form.subTitle.label' })}
			required={true}
			placeholder={intl.formatMessage({
				id: intlPrefix + '.form.subTitle.placeholder',
			})}
			rules={[
				{
					required: true,
					message: intl.formatMessage({
						id: intlPrefix + '.form.subTitle.required',
					}),
				},
			]}
		/>,
		<ProFormText
			key={'domain'}
			name={'domain'}
			label={intl.formatMessage({ id: intlPrefix + '.form.domain.label' })}
			required={true}
			placeholder={intl.formatMessage({
				id: intlPrefix + '.form.domain.placeholder',
			})}
			rules={[
				{
					required: true,
					message: intl.formatMessage({
						id: intlPrefix + '.form.domain.required',
					}),
				},
			]}
		/>,
		<ProFormUploadMaterial
			key={'logo'}
			label={intl.formatMessage({
				id: intlPrefix + '.form.logo.label',
			})}
			name={'logoList'}
			transform={(value) => {
				return {
					logoList: value,
					logo: value[0],
				};
			}}
			fieldProps={{
				maxCount: 1,
			}}
		/>,
		<ProFormUploadMaterial
			key={'backgroundImageUrl'}
			label={intl.formatMessage({
				id: intlPrefix + '.form.backgroundImageUrl.label',
			})}
			name={'backgroundImageUrlList'}
			transform={(value) => {
				return {
					backgroundImageUrlList: value,
					backgroundImageUrl: value[0],
				};
			}}
			fieldProps={{
				maxCount: 1,
			}}
		/>,
		<ProFormText
			key={'technicalSupport'}
			name={'technicalSupport'}
			label={intl.formatMessage({
				id: intlPrefix + '.form.technicalSupport.label',
			})}
			required={true}
			placeholder={intl.formatMessage({
				id: intlPrefix + '.form.technicalSupport.placeholder',
			})}
			rules={[
				{
					required: true,
					message: intl.formatMessage({
						id: intlPrefix + '.form.technicalSupport.required',
					}),
				},
			]}
		/>,
	];

	const columns: ProColumns<BusinessAPI.ChannelVO, BizValueType>[] = [
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.logo' }),
			dataIndex: 'logo',
			valueType: 'image',
			hideInSearch: true,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.title' }),
			dataIndex: 'title',
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.subTitle' }),
			dataIndex: 'subTitle',
			hideInSearch: true,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.domain' }),
			dataIndex: 'domain',
			valueType: 'text',
			hideInSearch: true,
		},
		{
			title: intl.formatMessage({
				id: intlPrefix + '.column.backgroundImageUrl',
			}),
			dataIndex: 'backgroundImageUrl',
			valueType: 'image',
			hideInSearch: true,
		},
		{
			title: intl.formatMessage({
				id: intlPrefix + '.column.technicalSupport',
			}),
			dataIndex: 'technicalSupport',
			hideInSearch: true,
		},
		{
			title: intl.formatMessage({ id: intlPrefix + '.column.createdAt' }),
			dataIndex: 'createdAt',
			valueType: 'dateTime',
			hideInSearch: true,
		},
	];

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

	return (
		<BizContainer<
			typeof business.channel,
			BusinessAPI.ChannelVO,
			BusinessAPI.ChannelPageQry,
			BusinessAPI.ChannelCreateCmd,
			BusinessAPI.ChannelUpdateCmd
		>
			rowKey={'channelId'}
			permission={'operation-channel'}
			func={business.channel}
			method={'channel'}
			methodUpper={'Channel'}
			intlPrefix={intlPrefix}
			modeType={'page'}
			container={{}}
			page={{
				fieldProps: {},
				columns,
			}}
			create={{
				formType: 'drawer',
				formContext,
			}}
			update={{
				formType: 'drawer',
				formContext,
				// @ts-ignore
				transform: (value) => {
					return {
						...value,
						backgroundImageUrlList: [value.backgroundImageUrl],
						logoList: [value.logo],
					};
				},
			}}
			destroy={{}}
			detail={{
				formType: 'drawer',
				columns: detailColumns,
			}}
		/>
	);
}
