<template>
	<xn-form-container
		:title="formData.id ? '编辑点位' : '新增点位'"
		:width="'800px'"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form
			ref="formRef"
			:model="formData"
			:rules="formRules"
			layout="inline"
			:label-col="{ style: { width: '110px', justifyContent: 'end' } }"
		>
			<a-row :gutter="10">
				<a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<a-form-item label="点位名称" name="name">
						<a-input v-model:value="formData.name" placeholder="请输入点位名称" allow-clear />
					</a-form-item>
				</a-col>
				<!-- <a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<a-form-item label="上级区域：" name="parentId">
						<a-tree-select
							v-model:value="formData.parentId"
							v-model:treeExpandedKeys="defaultExpandedKeys"
							class="xn-wd"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择区域"
							allow-clear
							:tree-data="treeData"
							:field-names="{
								children: 'children',
								label: 'name',
								value: 'id'
							}"
							selectable="false"
							treeLine
						/>
					</a-form-item>
				</a-col> -->

				<a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<a-form-item label="监控对象" name="monitorTargetId">
						<a-select
							ref="select"
							v-model:value="formData.monitorTargetId"
							:options="monitorTargetOptions"
							placeholder="请选择监控对象"
							@change="monitorTargetIdChange"
						/>
					</a-form-item>
				</a-col>

				<a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<a-form-item label="监控设备" name="monitorDeviceId">
						<a-select
							ref="select"
							v-model:value="formData.monitorDeviceId"
							:options="memListOptions"
							placeholder="请选择监控设备"
							@change="monitorDeviceIdChange"
						/>
					</a-form-item>
				</a-col>

				<a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<a-form-item label="传感器编号" name="sensorCode">
						<a-input v-model:value="formData.sensorCode" disabled placeholder="请输入传感器编号" allow-clear />
					</a-form-item>
				</a-col>

				<a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<a-form-item label="传感器类型" name="sensorType">
						<a-select
							ref="select"
							v-model:value="formData.sensorType"
							:options="sensorOptions"
							placeholder="请选择传感器类型"
						/>
					</a-form-item>
				</a-col>

				<a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<a-form-item label="传感器路数" name="sensorRoute">
						<a-input-number
							v-model:value="formData.sensorRoute"
							style="width: 100%"
							:min="1"
							:max="sensorRouteMax"
							placeholder="请输入传感器路数"
						/>
					</a-form-item>
				</a-col>

				<a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<a-form-item label="报警上限" name="limitUp">
						<a-input-number
							id="inputNumber"
							v-model:value="formData.limitUp"
							placeholder="请输入报警上限"
							allow-clear
							style="width: 100%"
						/>
					</a-form-item>
				</a-col>
				<a-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
					<a-form-item label="报警下限" name="limitDown">
						<a-input-number
							id="inputNumber"
							v-model:value="formData.limitDown"
							placeholder="请输入报警下限"
							allow-clear
							style="width: 100%"
						/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>

		<template #footer>
			<a-button class="xn-mr8" @click="onClose">退出</a-button>
			<a-button type="primary" @click="onSubmit">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup>
	import tool from '@/utils/tool'
	import { required } from '@/utils/formRules'
	import locationApi from '@/api/basicset/locationApi.js'
	import setupApi from '@/api/basicset/setupApi'
	import memApi from '@/api/basicset/memApi'
	const emit = defineEmits({ successful: null })
	const visible = ref(false) // 默认是关闭状态
	const formRef = ref()
	const formData = ref() // 表单数据
	const sensorOptions = tool.dictList('SENSORTYPE') // 传感器类型
	const monitorTargetOptions = ref([]) // 监控对象
	const memListOptions = ref([]) // 监控设备数据

	const sensorRouteMax = ref(1) //传感器最大值默认1

	// 默认要校验的
	const formRules = {
		name: [required('请输入区域名称')],
		// parentId: [required('请选择上级区域')],
		monitorTargetId: [required('请选择监控对象')],
		monitorDeviceId: [required('请选择监控设备')],
		sensorRoute: [required('请输入传感器路数')],
		limitUp: [required('请选择报警上限')],
		limitDown: [required('请选择报警下限')]
	}
	// 默认展开的节点(顶级)
	const defaultExpandedKeys = ref([0])
	// 定义树元素
	const treeData = ref([])
	// 打开抽屉
	const onOpen = (record, parentId) => {
		loadData()
		visible.value = true
		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {}
		}
	}

	const loadData = () => {
		// 获取监控对象
		setupApi.setupList({ isAll: false }).then((res) => {
			monitorTargetOptions.value = (res || []).map((item) => {
				return {
					...item,
					value: item['id'],
					label: item['name']
				}
			})
		})
		// 获取监控设备
		memApi.memList({ isAll: false }).then((res) => {
			console.log(res)
			memListOptions.value = (res || []).map((item) => {
				return {
					...item,
					value: item['id'],
					label: item['deviceName'],
					modelName: item['modelName'],
					deviceCode: item['deviceCode'],
					sensorCount: item['sensorCount']
				}
			})
		})
	}

	// 选中监控设备
	const monitorDeviceIdChange = (value) => {
		if (value) {
			const data = memListOptions.value.find((item) => item.value === value)
			// console.log(data, '1')
			formData.value.sensorCode = data.deviceCode //设备编号
			formData.value.modelName = data.modelName //监控设备型号
			formData.value.deviceCode = data.deviceCode //冷链编号
			sensorRouteMax.value = data.sensorCount //传感器路数的最大值
		}
	}

	// 选中监控对象
	const monitorTargetIdChange = (value) => {
		if (value) {
			const item = monitorTargetOptions.value.find((item) => item.value === value)
			formData.value.limitUp = item.limitUp //监控设备型号
			formData.value.limitDown = item.limitDown //冷链编号
		}
	}

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

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				locationApi.submitForm(formData.value, formData.value.id).then(() => {
					onClose()
					emit('successful')
				})
			})
			.catch((error) => {
				console.log(error)
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style lang="less" scoped>
	:deep(.ant-form-item) {
		width: 100%;
		margin-bottom: 10px;
	}
</style>
