<template>
	<xn-form-container
		:title="pageType === 'EDIT' ? '编辑生产组织' : pageType === 'ADD' ? '增加生产组织' : '查看生产组织'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="24">
					<a-form-item label="编码：" name="number">
						<a-input
							:disabled="pageType === 'SEARCH'"
							v-model:value="formData.number"
							placeholder="请输入编码"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="类型：" name="type">
						<a-select
							:disabled="pageType !== 'ADD'"
							v-model:value="formData.type"
							placeholder="请选择组织类型"
							:options="typeOptions"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="工厂名称：" name="name">
						<a-input
							:disabled="pageType === 'SEARCH'"
							v-model:value="formData.name"
							placeholder="请输入工厂名称"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="仓库：" name="storeId">
						<a-tree-select
							:disabled="pageType === 'SEARCH'"
							v-model:value="formData.storeId"
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							:tree-data="treeData"
							placeholder="请选择仓库名称"
							:fieldNames="{
								children: 'children',
								label: 'name',
								value: 'id'
							}"
						>
						</a-tree-select>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="上级组织：" name="parentId" v-if="formData.type !== 'FACTORY'">
						<a-tree-select
							:disabled="pageType === 'SEARCH'"
							v-model:value="formData.parentId"
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							:tree-data="parentTreeData"
							placeholder="请选择上级组织"
							:fieldNames="{
								children: 'children',
								label: 'name',
								value: 'id'
							}"
						>
						</a-tree-select>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="厂家名称：" name="manufacturer" v-if="formData.type === 'FACTORY'">
						<a-input
							:disabled="pageType === 'SEARCH'"
							v-model:value="formData.manufacturer"
							placeholder="请输入厂家名称"
							allow-clear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="启用状态：" name="enabledState">
						<a-select
							:disabled="pageType === 'SEARCH'"
							v-model:value="formData.enabledState"
							placeholder="请选择启用状态"
							:options="enabledStateOptions"
						/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button v-if="pageType !== 'SEARCH'" type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="productionOrganizationForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import productionOrganizationApi from '@/api/base/production-organization/productionOrganizationApi'
	import sysStoreApi from '@/api/base/store/sysStoreApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	let formData_enum = {
		type: 'FACTORY',
		enabledState: 'ENABLE'
	}
	const formData = ref({})
	const submitLoading = ref(false)
	const typeOptions = ref([])
	const enabledStateOptions = ref([])
	let pageType = ref('ADD')

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			pageType.value = record.pageType
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		} else {
			pageType.value = 'ADD'
			formData.value = formData_enum
		}

		typeOptions.value = tool.dictList('PRODUCTION_ORGANIZATION_TYPE')
		enabledStateOptions.value = tool.dictList('COMMON_STATUS')

		sysStoreApi.sysStoreTree().then((res) => {
			treeData.value = res || []
		})

		productionOrganizationApi.productionOrganizationTree().then((res) => {
			parentTreeData.value = res
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 默认要校验的refresh
	const formRules = {
		name: [required('请输入名称')],
		type: [required('请输入类型')],
		parentId: [required('请选择上级仓库')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			productionOrganizationApi
				.productionOrganizationSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}

	let treeData = ref([])
	let parentTreeData = ref([])
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
