<template>
	<xn-form-container
		title="人员行为识别纠错"
		: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="24">
					<a-form-item label="纠错类型">
						<a-radio-group v-model:value="type">
							<a-radio :value="1">误触发</a-radio>
							<a-radio :value="2">识别错误</a-radio>
						</a-radio-group>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="16" v-if="type === 2">
				<a-col :span="12">
					<a-form-item label="姓名：" name="name">
						<a-input v-model:value="formData.name" placeholder="请输入姓名" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="类型：" name="personType">
						<a-input v-model:value="formData.personType" placeholder="请输入类型" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-row :gutter="16" v-if="type === 2">
				<a-col :span="12">
					<a-form-item label="尝试进入区域：" name="devName">
						<a-input v-model:value="formData.devName" placeholder="请输入尝试进入区域" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="结果：" name="capResult">
						<a-input v-model:value="formData.capResult" 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="personForm">
	import bizAiPollingApi from "@/api/biz/ai/bizAiPollingApi";
	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({
		name: [required('请输入姓名')],
		personType: [required('请输入类型')],
		devName: [required('请输入尝试进入区域')],
		capResult: [required('请输入结果')]
	})

	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}


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

	const type = ref(1)

	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			// 误触发（删除）
			if (type.value === 1) {
				let params = [
					{
						id: formDataParam.id
					}
				]
				bizAiPollingApi.delPersonCapturePage(params).then(() => {
					onClose()
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			} else if (type.value === 2) {
				bizAiPollingApi.editPersonCapturePage(formDataParam).then(() => {
					onClose()
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			}
		})
	}

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

<style scoped>

</style>
