<template>
	<xn-form-container
		:title="formData.id ? '编辑栋舍' : '增加栋舍'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="部门名称：" name="departmentName">
				<a-input v-model:value="formData.departmentName" placeholder="请输入部门名称" allow-clear />
			</a-form-item>
			<a-form-item label="上级部门：" name="parentId">
				<a-tree-select
					v-model:value="formData.parentId"
					style="width: 100%"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择上级部门"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					:field-names="{
						children: 'children',
						label: 'name',
						value: 'id'
					}"
					selectable="false"
					tree-line
				/>
			</a-form-item>
			<a-form-item label="部门负责人：" name="departmentManager">
				<a-select v-model:value="formData.departmentManager">
					<a-select-option v-for="item in userList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="部门电话：" name="departmentPhone">
				<a-input v-model:value="formData.departmentPhone" placeholder="请输入部门电话" allow-clear />
			</a-form-item>
			<a-form-item label="所在区域：" name="departmentRegion">
				<a-cascader
					v-model:value="formData.departmentRegionList"
					@change="areaChange"
					:options="area"
					:fieldNames="{ label: 'name', value: 'name', children: 'children' }"></a-cascader>
			</a-form-item>
			<a-form-item label="详细地址：" name="detailLocation">
				<a-input v-model:value="formData.detailLocation" placeholder="请输入详细地址" allow-clear />
			</a-form-item>
			<a-form-item label="排序：" name="sortCode">
				<a-input v-model:value="formData.sortCode" placeholder="请输入排序" allow-clear />
			</a-form-item>
			<a-form-item label="描述：" name="remarks">
				<a-input v-model:value="formData.remarks"></a-input>
			</a-form-item>
			<a-form-item label="部门照片" name="departmentImgUrl">
				<x-img-upload @onImgChange="onImgChange"></x-img-upload>
			</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>
	</xn-form-container>
</template>

<script setup name="departmentForm">
	import { Empty } from 'ant-design-vue'
	import { cloneDeep, isEmpty } from 'lodash-es'
	import { required, rules } from '@/utils/formRules'
	import bizDepartmentApi from "@/api/biz/bizDepartmentApi"
	import area from '@/assets/json/area.json'
	import XImgUpload from "@/components/XnUpload/XImgUpload.vue"

	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const treeData = ref([])
	const userList = ref([])
	const submitLoading = ref(false)
	const imageUrl = ref('')

	const onImgChange = (item) => {
		if (item.length > 0) {
			formData.value.departmentImgUrl = item[0]
		} else {
			formData.value.departmentImgUrl = undefined
		}
	}

	// 打开抽屉
	const onOpen = (record, parentId) => {
		visible.value = true
		if (parentId) {
			formData.value.parentId = parentId
		}
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			formData.value.departmentRegionList = recordData.departmentRegion.split('-')
		}
		bizDepartmentApi.getDepartmentTree().then((res) => {
			treeData.value = [
				{
					id: 0,
					parentId: '-1',
					name: '顶级',
					children: res
				}
			]
		})
		bizDepartmentApi.getDepartmentUser().then((res) => {
			userList.value = res
		})
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}

	const areaChange = (value) => {
		formData.value.departmentRegion = value.join('-')
	}

	// 默认要校验的
	const formRules = {
		departmentName: [required('请输入部门名称')],
		parentId: [required('请选择上级部门')],
		departmentRegion: [required('请选择所在区域')],
		detailLocation: [required('请输入详细地址')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			if (formData.value.id) {
				const fileData = new FormData()
				if (formData.value.departmentImgUrl) {
					fileData.append('departmentImgUrl', formData.value.departmentImgUrl)
				}
				if (formData.value.departmentManager) {
					fileData.append('departmentManager', formData.value.departmentManager)
				}
				if (formData.value.departmentPhone) {
					fileData.append('departmentPhone', formData.value.departmentPhone)
				}
				if (formData.value.remarks) {
					fileData.append('departmentPhone', formData.value.remarks)
				}
				if (formData.value.sortCode) {
					fileData.append('sortCode', formData.value.sortCode)
				}
				fileData.append('id', formData.value.id)
				fileData.append('departmentName', formData.value.departmentName)
				fileData.append('departmentRegion', formData.value.departmentRegion)
				fileData.append('detailLocation', formData.value.detailLocation)
				fileData.append('parentId', formData.value.parentId)
				bizDepartmentApi
					.editDepartment(fileData)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			} else {
				const fileData = new FormData()
				if (formData.value.departmentImgUrl) {
					fileData.append('departmentImgUrl', formData.value.departmentImgUrl)
				}
				if (formData.value.departmentManager) {
					fileData.append('departmentManager', formData.value.departmentManager)
				}
				if (formData.value.departmentPhone) {
					fileData.append('departmentPhone', formData.value.departmentPhone)
				}
				if (formData.value.remarks) {
					fileData.append('departmentPhone', formData.value.remarks)
				}
				if (formData.value.sortCode) {
					fileData.append('sortCode', formData.value.sortCode)
				}
				fileData.append('departmentName', formData.value.departmentName)
				fileData.append('departmentRegion', formData.value.departmentRegion)
				fileData.append('detailLocation', formData.value.detailLocation)
				fileData.append('parentId', formData.value.parentId)
				bizDepartmentApi
					.addDepartment(fileData)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			}
		})

	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>

<style scoped>

</style>
