<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>

	<a-card :bordered="false" title="操作信息" class="mt-4" v-if="route.query.type !== 'ADD'">
		<OperationalInformation :detailData="detailData" :colSpan="12"></OperationalInformation>
	</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 { basicInfoFormItems, officialAccountFormItems } from '@/views/basicData/client/formItems'
	import { useRoute } from 'vue-router'
	const route = useRoute()

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

	const formRef1 = ref(null)
	const formRef2 = ref(null)
	let detailData = ref({})

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

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