<template>
	<xn-form-container
		:title="formData.id ? '报警处理' : '报警确定'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="Inline" :label-col="labelCol">
			<a-form-item label="发生时间" name="fssj">
				<a-date-picker show-time placeholder="请选择发生时间" @change="onChange" style="width: 100%" />
			</a-form-item>

			<a-form-item label="设备" name="sb">
				<a-input v-model:value="formData.sb" placeholder="请输入设备" allow-clear />
			</a-form-item>
			<a-form-item label="报警原因" name="bjyy">
				<a-input v-model:value="formData.bjyy" placeholder="请输入报警原因" allow-clear />
			</a-form-item>
			<a-form-item label="故障类型:" name="gzlx">
				<a-select ref="select" v-model:value="formData.gzlx" style="width: 100%">
					<a-select-option value="1">超限报警</a-select-option>
				</a-select>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">退出</a-button>
			<a-button type="primary" @click="onSubmit">报警确定</a-button>
		</template>
		<Icon-selector ref="iconSelectorRef" @iconCallBack="iconCallBack" />
	</xn-form-container>
</template>

<script setup>
	import ColorPicker from '@/components/ColorPicker/index.vue'
	import { required } from '@/utils/formRules'
	import moduleApi from '@/api/sys/resource/moduleApi'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const labelCol = { style: { width: '80px' } }
	const treeData = ref([])
	const iconSelectorRef = ref()
	// 表单数据
	const formData = ref({})

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true

		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {}
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}
	// 报警时间
	const onChange = (value, dateString) => {
		formData.value.bjkssj = dateString
		console.log('报警时间', dateString)
	}

	// 默认要校验的
	const formRules = {
		fssj: [required('请选择发生时间')],
		sb: [required('请输入设备')],
		bjyy: [required('请输入报警原因')],
		gzlx: [required('请选择故障类型')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				console.log(formData.value, '提交数据')

				// moduleApi.submitForm(formData.value, formData.value.id).then(() => {
				// 	onClose()
				// 	emit('successful')
				// })
			})
			.catch(() => {})
	}

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