<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="onSubmitForm">保存</a-button>
		</template>
	</a-page-header>

	<a-card :bordered="false" title="品牌">
		<DynamicForm
			:allDisabled="route.query.type === 'SEARCH'"
			:formItems="brandFormItems"
			:model="formData"
			:rules="formRules"
			ref="formRef1"
		/>
	</a-card>

	<a-card :bordered="false" class="mt-4" style="height: 100%">
		<a-tabs v-model:activeKey="activeKey">
			<a-tab-pane key="1" tab="扩展字段" forceRender v-if="extendData.length > 0">
				<DynamicForm
					:allDisabled="route.query.type === 'SEARCH'"
					:formItems="extendData"
					:model="extendFormData"
					:rules="formRules"
					v-if="extendData.length > 0"
				/>

				<a-empty v-else />
			</a-tab-pane>
			<a-tab-pane key="2" tab="操作信息" v-if="route.query.type !== 'ADD'">
				<OperationalInformation :detailData="inform" :colSpan="6"></OperationalInformation>
			</a-tab-pane>
		</a-tabs>
	</a-card>
</template>

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

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

	const brandFormItems = reactive([
		{
			label: '名称：',
			name: 'name',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请输入名称',
				allowClear: true
			}
		},
		{
			label: '编码：',
			name: 'number',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请输入编码',
				allowClear: true
			}
		},
		{
			label: '简称：',
			name: 'shortName',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请输入简称',
				allowClear: true
			}
		},
		{
			label: '所属企业：',
			name: 'company',
			type: 'a-input',
			span: 6,
			attrs: {
				placeholder: '请输入所属企业',
				allowClear: true
			}
		},
		{
			label: '公众号：',
			name: 'officialAccountId',
			type: 'a-select',
			span: 6,
			attrs: {
				placeholder: '请输入公众号',
				options: [],
				fieldNames: {
					children: 'children',
					label: 'name',
					value: 'id'
				}
			}
		},
		{
			label: '启用状态：',
			name: 'enabledState',
			type: 'a-select',
			span: 6,
			attrs: {
				placeholder: '请选择启用状态',
				options: tool.dictList('COMMON_STATUS')
			},
			defaultValue: 'ENABLE'
		},
		{
			label: '备注：',
			name: 'remarks',
			type: 'a-textarea',
			span: 24,
			attrs: {
				placeholder: '请输入备注',
				allowClear: true
			}
		}
	])

	const formRef1 = ref(null)
	let extendData = ref([])

	let { formData, formRefs, inform, extendFormData, onSubmit, handleBack, fetchData, getExtendField } = useFormHandler(
		brandFormItems,
		{
			submitForm: sysBrandApi.sysBrandSubmitForm,
			getDetail: sysBrandApi.sysBrandDetail
		}
	)

	onMounted(async () => {
		formRefs.value = [formRef1.value]
		await fetchData(route.query.type)

		officialAccountApi
			.officialAccountList({
				enabledState: 'ENABLE'
			})
			.then((res) => {
				brandFormItems.forEach((item) => {
					if (item.name === 'officialAccountId') {
						item.attrs.options = res
					}
				})
			})

		extendData.value = await getExtendField('MATERIAL')
	})

	// 添加域名
	const domainFormRef = ref(null)
	const domainFormRules = {
		domainName1: [required('请输入域名')]
	}

	let domainFormData = reactive({})

	let domainFormItems = reactive([
		{
			label: '域名1',
			value: 'domainName1'
		}
	])

	const handleAddDomain = () => {
		domainFormItems.push({
			label: '域名' + (domainFormItems.length + 1),
			value: 'domainName' + domainFormItems.length
		})

		domainFormRules['domainName' + domainFormItems.length] = [required('请输入域名')]
	}

	const handleDelDomain = (index) => {
		domainFormItems.splice(index, 1)
		delete domainFormRules['domainName' + (index + 1)]
	}

	const onSubmitForm = async () => {
		await onSubmit({ ...formData, ...domainFormData })
	}

	let activeKey = ref('1')
</script>
