<template>
	<a-drawer
		:title="formData.id ? '编辑企业客户粮库' : '增加企业客户粮库'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="企业客户：" name="depotGrainName">
				<a-input v-model:value="formData.depotGrainName" placeholder="请输入企业客户" allow-clear/>
			</a-form-item>
			<a-form-item label="选择组织：" name="orgId">
				<a-tree-select
					v-model:value="formData.orgId"
					style="width: 100%"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择组织"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					:tree-default-expanded-keys="treeDefaultExpandedKeys"
					:field-names="{
										children: 'children',
										label: 'name',
										value: 'id'
									}"
				></a-tree-select>
			</a-form-item>
			<a-form-item v-if="!formData.id">
				<p>在线状态：5分钟检查一次，30分钟后确认离线并报警</p>
				<p>扦样控制：如果需要跳过扦样流程，请提交后在列表页面手动关闭</p>
				<p>卸货控制：如果需要跳过卸货流程，请提交后在列表页面手动关闭</p></a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="depotGrainForm">
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import depotGrainApi from '@/api/biz/depotGrainApi'
import tool from "@/utils/tool";
import router from "@/router";
import bizUserApi from "@/api/biz/bizUserApi";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const treeData = ref([])
const treeDefaultExpandedKeys = ref([])
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
	// 机构选择器数据
	bizUserApi.userOrgTreeSelector().then((res) => {
		if (res !== null) {
			treeData.value = res
			// 默认展开2级
			treeData.value.forEach((item) => {
				// 因为0的顶级
				if (item.parentId === '0') {
					treeDefaultExpandedKeys.value.push(item.id)
					// 取到下级ID
					if (item.children) {
						item.children.forEach((items) => {
							treeDefaultExpandedKeys.value.push(items.id)
						})
					}
				}
			})
		}
	})
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	treeData.value = []
	treeDefaultExpandedKeys.value = []
	visible.value = false
}
// 默认要校验的
const formRules = {
	depotGrainName: [required('请输入')],
	orgId: [required('请选择')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			depotGrainApi
				.depotGrainSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.catch((err) => {
					emit(err)
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
