<template>
	<xn-form-container
		:title="formData.id ? (disabled ? '查看价格监测' : '编辑价格监测') : '增加价格监测'"
		: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="name">
				<a-input v-model:value="formData.name" :disabled="disabled" placeholder="请输入监测点名称" allow-clear />
			</a-form-item>
			<a-form-item label="所属市州：" name="sysOrgId">
				<a-input v-model:value="formData.sysOrgId" :disabled="disabled" placeholder="请输入所属市州" allow-clear />
			</a-form-item>
			<a-form-item label="所属行政管理部门" name="cityAddress">
				<a-select
					class="select-box"
					:disabled="disabled"
					v-model:value="formData.cityAddress"
					:size="size"
					style="width: 100%"
					placeholder="请选择"
				>
					<a-select-option v-for="item in auditStatus" :key="item.id" :value="item.name"
						>{{ item.name }}
					</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="详细位置：" name="address">
				<a-input v-model:value="formData.address" :disabled="disabled" placeholder="请输入详细位置" allow-clear />
			</a-form-item>
			<a-form-item label="定位开关" name="locationswitch">
				<a-select
					class="select-box"
					:disabled="disabled"
					:size="size"
					style="width: 100%"
					v-model:value="formData.locationswitch"
					placeholder="请选择"
				>
					<a-select-option v-for="item in columns" :key="item.id" :value="item.title"
						>{{ item.title }}
					</a-select-option>
				</a-select>
			</a-form-item>
			<a-form-item label="经度：" name="longitude">
				<a-input v-model:value="formData.longitude" :disabled="disabled" placeholder="请输入经度" allow-clear />
			</a-form-item>
			<a-form-item label="纬度：" name="latitude">
				<a-input v-model:value="formData.latitude" :disabled="disabled" placeholder="请输入纬度" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button v-if="disabled !== true" type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="priceMonitorForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import priceMonitorApi from '@/api/basic.info/priceMonitorApi'
	import unReporCityApi from '@/api/basic.info/unReporCityApi.js'
	const auditStatus = ref([])
	onMounted(() => {
		getSearchData()
	})
	// 下拉框数据
	const getSearchData = () => {
		unReporCityApi.unReporCityquery().then((res) => {
			auditStatus.value = res
		})
	}
	const columns = [
		{
			title: '开',
			locationswitch: 1
		},
		{
			title: '关',
			locationswitch: 0
		}
	]
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const disabled = ref(false)

	// 打开抽屉
	const onOpen = (record, flag) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		if (flag) {
			disabled.value = flag
		} else {
			disabled.value = false
		}
		
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			priceMonitorApi
				.priceMonitorSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
