<template>
	<xn-form-container
		:title="formData.id ? '编辑设备' : '增加设备'"
		:width="800"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ 'padding-top': '20px' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="设备编号：" name="deviceCode">
						<a-input v-model:value="formData.deviceCode" placeholder="请输入设备编号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="设备类型：" name="deviceType">
						<a-select v-model:value="formData.deviceType" placeholder="请选择设备类别">
							<a-select-option v-for="item in typeList" :key="item.id" :value="item.id">{{item.configName}}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="设备名称：" name="deviceName">
						<a-input v-model:value="formData.deviceName" placeholder="请输入设备名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="安装位置：" name="unitId">
						<a-tree-select
							v-model:value="formData.unitId"
							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"
						/>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="关联母猪耳标号：" name="eartagNum">
						<a-input v-model:value="formData.eartagNum" placeholder="请输入母猪耳标号" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
		</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="FeedDeviceForm">
	import bizHouseApi from "@/api/biz/bizHouseApi"
	import bizFeedDeviceApi from "@/api/biz/feed/bizFeedDeviceApi"
	import {required, rules} from "@/utils/formRules"
	import {cloneDeep} from "lodash-es"

	const visible = ref(false)
	const formRef = ref()
	const emit = defineEmits({ successful: null })
	const submitLoading = ref(false)
	const formData = ref({})
	const formRules = ref({
		deviceCode: [required('请输入设备编号')],
		deviceType: [required('请选择设备类型')],
		deviceName: [required('请输入设备名称')],
		installPosition: [required('请选择安装位置')]
	})

	const treeData = ref([])
	const typeList = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		bizHouseApi.basePigpenTree().then((res) => {
			treeData.value = res
		})
		bizFeedDeviceApi.getDeviceType().then((res) => {
			typeList.value = res
		})
	}

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

	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			if (formData.value.id) {
				bizFeedDeviceApi.editDevice(formDataParam).then(() => {
					onClose()
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			} else {
				bizFeedDeviceApi.addDevice(formDataParam).then(() => {
					onClose()
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			}
		})
	}

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

<style scoped>

</style>
