<template>
	<a-page-header style="padding: 10px; font-size: 20px" @back="handleBack">
		<template #extra>
			<a-button v-if="route.query.type !== 'SEARCH'" key="1" type="primary" @click="onSubmit">保存</a-button>
		</template>
	</a-page-header>

	<a-card :bordered="false" title="公众号">
		<DynamicForm
			:allDisabled="route.query.type === 'SEARCH'"
			:formItems="officialAccountFormItems"
			:model="formData"
			:rules="formRules"
			ref="formRef1"
		/>
	</a-card>

	<a-card :bordered="false" title="基本信息" class="mt-4">
		<DynamicForm
			:allDisabled="route.query.type === 'SEARCH'"
			:formItems="basicInfoFormItems"
			:model="formData"
			:rules="formRules"
			ref="formRef2"
		/>
	</a-card>
</template>

<script setup name="publicAccountDetail">
	import { required } from '@/utils/formRules'
	import officialAccountApi from '@/api/base/wx/officialAccountApi'
	import useFormHandler from '@/hook/useFormHandler'
	import tool from '@/utils/tool'
	import { useRoute } from 'vue-router'
	const route = useRoute()

	const formRules = {
		name: [required('请输入名称')],
		type: [required('请输入类型')],
		appid: [required('请输入AppID')],
		secret: [required('请输入AppSecret')]
	}

	const officialAccountFormItems = [
		{
			label: '类型：',
			name: 'type',
			type: 'a-select',
			span: 8,
			attrs: {
				placeholder: '请选择类型',
				options: tool.dictList('OFFICIAL_ACCOUNT_TYPE')
			}
		},
		{
			label: '名称：',
			name: 'name',
			type: 'a-input',
			span: 8,
			rules: [required('请输入名称')],
			attrs: {
				placeholder: '请输入名称',
				allowClear: true
			}
		},
		{
			label: '可用状态：',
			name: 'enabledState',
			type: 'a-select',
			span: 8,
			attrs: {
				placeholder: '请选择可用状态',
				options: tool.dictList('COMMON_STATUS')
			},
			defaultValue: 'ENABLE'
		},
		{
			label: '备注：',
			name: 'remarks',
			type: 'a-textarea',
			span: 24,
			attrs: {
				placeholder: '请输入备注',
				allowClear: true
			}
		}
	]

	const basicInfoFormItems = [
		{
			label: 'AppID：',
			name: 'appid',
			type: 'a-input',
			attrs: {
				placeholder: '请输入AppID',
				allowClear: true
			}
		},
		{
			label: 'AppSecret：',
			name: 'secret',
			type: 'a-input',
			attrs: {
				placeholder: '请输入AppSecret',
				allowClear: true
			}
		},
		{
			label: '字符串编码格式：',
			name: 'encodingFormat',
			type: 'a-input',
			attrs: {
				placeholder: '请输入字符串编码格式',
				allowClear: true
			}
		},
		{
			label: '校验码：',
			name: 'token',
			type: 'a-input',
			attrs: {
				placeholder: '请输入校验码',
				allowClear: true
			}
		},
		{
			label: '签名方式：',
			name: 'aesKey',
			type: 'a-input',
			attrs: {
				placeholder: '请输入签名方式',
				allowClear: true
			}
		}
	]

	const formRef1 = ref(null)
	const formRef2 = ref(null)

	const { state, formData, submitLoading, formRefs, onSubmit, handleBack, fetchData } = useFormHandler(
		[...officialAccountFormItems, ...basicInfoFormItems],
		{
			submitForm: officialAccountApi.officialAccountSubmitForm,
			getDetail: officialAccountApi.officialAccountDetail
		}
	)

	onMounted(async () => {
		formRefs.value = [formRef1.value, formRef2.value]
		await fetchData(route.query.type)
	})
</script>
