<template>
	<xn-form-container :title="'阈值设置'" :width="800" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form
			:model="formState"
			ref="formStateRef"
			:rules="formStateRules"
			name="basic"
			autocomplete="off"
			@finish="onFinish"
			@finishFailed="onFinishFailed"
		>
			<a-row>
				<a-col :span="12">
					<div class="common">
						<a-form-item
							class="form-item selects"
							label="设备类型"
							name="equipmentType"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-select :disabled="disabled" v-model:value="formState.equipmentType">
								<a-select-option :value="1">消防水箱</a-select-option>
								<a-select-option :value="2">消防水池</a-select-option>
							</a-select>
						</a-form-item>
					</div>
				</a-col>
				<a-col :span="12">
					<div class="common">
						<a-form-item
							class="form-item"
							label="预警阈值"
							name="warningThreshold"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-input
								v-model:value="formState.warningThreshold"
								:disabled="disabled"
								placeholder="请输入预警阈值"
								all-clear
							/>
						</a-form-item>
					</div>
				</a-col>
				<a-col :span="12">
					<div class="common">
						<a-form-item
							class="form-item"
							label="维护人"
							name="maintenancePerson"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-input
								v-model:value="formState.maintenancePerson"
								:disabled="disabled"
								placeholder="请输入维护人"
								all-clear
							/>
						</a-form-item>
					</div>
				</a-col>
				<a-col :span="12">
					<div class="common">
						<a-form-item
							class="form-item"
							label="维护时间"
							name="maintenanceTime"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-date-picker
								:disabled="disabled"
								v-model:value="formState.maintenanceTime"
								valueFormat="YYYY-MM-DD HH:mm:ss"
								style="width: 250px"
							/>
						</a-form-item>
					</div>
				</a-col>
			</a-row>

			<div class="btnBox">
				<a-button class="acc" @click="onClose"> 取消 </a-button>
				<a-button class="acc snowy-buttom-left" :disabled="disabled" type="primary" html-type="submit"> 保存 </a-button>
			</div>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
		</template>
	</xn-form-container>
</template>
<script lang="js">
	import business from '@/api/security/index.js'
	import businessDEl from '@/api/business/dictionary.js'
	import { ref } from 'vue'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import global from '@/api/basic.info/global'
	import {
		isSocialCreditOrIdCode,
		isLongitude,
		isLatitude,
		isEmail,
		// isTelephone,
		isChinese,
		isMobileData,
		isZipCode,
		isInternetUrl,
		isSocialCreditCode
	} from '@/utils/validator-zq'
	export default {
		name: 'FromName',
		components: {},
		emits: ['change'],

		setup(props, { emit }) {
			const disabled = ref(false)
			const visible = ref(false)
			const records = ref('')
			const onOpen = (record, flag, view, row) => {
				records.value = record
				visible.value = flag
				disabled.value = view
				if (row) {
					detail(row.id)
					formState.value.id = row.id
				}
			}
			const rest = () => {
				for (let key in formState.value) {
					formState.value[key] = ''
				}
			}
			const detail = async (v) => {
				const res = await business.YZSZdetail({ id: v })
				formState.value = res
			}
			const onClose = () => {
				rest()
				visible.value = false
			}
			watch(
				() => visible.value,
				(newVal) => {
					if (!newVal) {
						setTimeout(() => {
							rest()
						}, 0)
					}
				},
				{ immediate: true, deep: true }
			)

			const formState = ref({
				equipmentType: 0,
				warningThreshold: 0,
				maintenancePerson: '',
				maintenanceTime: ''
			})

			const formStateRef = ref(null)
			const formStateRules = ref({
				dwmc: [
					{ required: true, message: '请填写!', trigger: 'change' }
					// { validator: isChinese, trigger: 'change' }
				],
				dwlx: [{ required: true, message: '请填写!', trigger: 'change' }],
				zcdz: [
					{ required: true, message: '请填写!', trigger: 'blur' }
					// { validator: isChinese, trigger: 'change' }
				],
				companyNature: [{ required: true, message: '请填写!', trigger: 'change' }],
				dwdm: [
					{ required: true, message: '请填写!', trigger: 'change' },
					{ validator: isSocialCreditCode, trigger: 'change' }
				],
				yzbm: [
					{ required: true, message: '请填写!', trigger: 'change' },
					{ validator: isZipCode, trigger: 'change' }
				],
				frlxfs: [
					{ required: true, message: '请填写!', trigger: 'change' },
					{ validator: isMobileData, trigger: 'change' }
				],
				qylxr: [
					{ required: true, message: '请填写!', trigger: 'change' }
					// { validator: isMobileData, trigger: 'change' }
				],
				jd: [
					{ required: true, message: '请填写!', trigger: 'change' },
					{ validator: isLongitude, trigger: 'change' }
				],
				wd: [
					{ required: true, message: '请填写!', trigger: 'change' },
					{ validator: isLatitude, trigger: 'change' }
				],
				xzqhdm: [{ required: true, message: '请填写!', trigger: 'change' }]
			})

			const onFinish = async (values) => {
				await handleAdd(formState.value)
				emit('change')
				onClose()
			}

			const handleAdd = async (val) => {
				if (records.value == 'edit') {
					const res = await business.YZSZDELedit(val)
				} else {
					const res = await business.YZSZadd(val)
					console.log('add')
				}
			}

			onMounted(() => {
				// getEthnicity()
				// getGender()
			})
			// 获取下拉单位类型
			const optionsGender = ref([]) // 单位性质
			const optionsEthnicity = ref([]) //单位类型
			const getEthnicity = async () => {
				const res = await businessDEl.genderDEl({ isQueryFristNode: true, parentId: '1783406822916190209' })
				optionsEthnicity.value = res
			}
			const getGender = async () => {
				const res = await businessDEl.genderDEl({ isQueryFristNode: true, parentId: '1787747408829857793' })
				optionsGender.value = res
			}

			const handleChange = (val) => {
				console.log(val)
			}

			const delADD = () => {
				formStateRef.value.validate().then(() => {})
			}

			const onFinishFailed = (errorInfo) => {
				console.log('Failed:', errorInfo)
			}

			return {
				optionsGender,
				optionsEthnicity,
				handleChange,
				formStateRules,
				delADD,
				formStateRef,
				formState,
				onFinish,
				onFinishFailed,
				disabled,
				onOpen,
				visible,
				onClose
			}
		}
	}
</script>
<style lang="less" scoped>
	.btnBox {
		text-align: center;
		margin-top: 100px;
		.snowy-buttom-left {
			margin-left: 100px;
		}
		.acc {
			width: 80px;
			height: 40px;
		}
	}
	// ::v-deep(.ant-form-item-control-input) {
	// 	// width: 250px;
	// }
	// .selects {
	// 	::v-deep(.ant-select-selector) {
	// 		// width: 20px;
	// 	}
	// }

	// .xingzheng {
	// 	::v-deep(.ant-select-selector) {
	// 		width: 400px;
	// 	}
	// }
</style>
