<template>
	<a-page-header style="padding: 10px; font-size: 20px" @back="handleBack">
		<template #extra>
			<a-button key="1" type="primary" @click="onSubmitForm">保存</a-button>
		</template>
	</a-page-header>
	<a-card :bordered="false" title="单位信息">
		<DynamicForm
			:allDisabled="route.query.type === 'SEARCH'"
			:formItems="unitFormItems"
			: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="basicDataUnitDetail">
	import tool from '@/utils/tool'
	import { required } from '@/utils/formRules'
	import unitGroupsApi from '@/api/base/unit/unitGroupsApi'
	import useFormHandler from '@/hook/useFormHandler'
	import { useRoute } from 'vue-router'
	import unitApi from '@/api/base/unit/unitApi'
	const route = useRoute()

	const formRef1 = ref()
	let activeKey = ref('1')
	let extendData = ref([])

	// 默认要校验的
	const formRules = {
		unitGroupId: [required('请选择单位')],
		name: [required('请输入名称')],
		isBase: [required('请选择是否基本单位')]
	}

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

		// 单位组
		const unitGroupsList = await unitGroupsApi.sysUnitGroupList({
			enabledState: 'ENABLE'
		})

		unitGroupsList &&
			unitFormItems.forEach((item) => {
				if (item.name === 'unitGroupId') {
					item.attrs.options = unitGroupsList
				}
			})

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

	const onSubmitForm = () => {
		onSubmit({
			isDeep: true,
			...formData,
			extJson: JSON.stringify(extendFormData.value) || ''
		})
	}

	const unitFormItems = reactive([
		{
			label: '名称：',
			name: 'name',
			type: 'a-input',
			span: 6,
			rules: [required('请输入名称')],
			attrs: {
				placeholder: '请输入名称',
				allowClear: true
			}
		},
		{
			label: '编码：',
			name: 'number',
			type: 'a-input-number',
			span: 6,
			attrs: {
				placeholder: '请输入编码',
				allowClear: true
			}
		},
		{
			label: '单位组：',
			name: 'unitGroupId',
			type: 'a-select',
			span: 6,
			attrs: {
				placeholder: '请选择可用状态',
				options: [],
				fieldNames: {
					label: 'name',
					value: 'id'
				}
			},
			defaultValue: ''
		},
		{
			label: '换算率：',
			name: 'number',
			type: 'a-input-number',
			span: 6,
			attrs: {
				placeholder: '请输入换算率',
				allowClear: true
			}
		},
		{
			label: '是否基本单位：',
			name: 'isBase',
			type: 'a-select',
			span: 6,
			attrs: {
				placeholder: '请选择是否基本单位',
				options: tool.dictList('YES_NO')
			},
			defaultValue: 'NO'
		},
		{
			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 { formData, formRefs, inform, extendFormData, onSubmit, handleBack, fetchData, getExtendField } =
		useFormHandler([...unitFormItems], {
			submitForm: unitApi.sysUnitSubmitForm,
			getDetail: unitApi.sysUnitDetail
		})
</script>
