<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="orgId"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-select :disabled="disabled" v-model:value="formState.orgId" @change="handleChange">
								<a-select-option v-for="item in optionsEthnicity" :key="item.orgId" :value="item.orgId">{{
									item.orgName
								}}</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="deviceName"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-input
								v-model:value="formState.deviceName"
								: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="patrolTime"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-input
								v-model:value="formState.deviceName"
								: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="patrolReport"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-input v-model:value="formState.number" :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="repairer"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-input
								v-model:value="formState.repairer"
								: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="deviceManager"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-input
								v-model:value="formState.deviceManager"
								: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="repairSumnumber"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-input
								v-model:value="formState.repairSumnumber"
								: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="repairStatus"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-select :disabled="disabled" v-model:value="formState.repairStatus">
								<a-select-option :value="0">正常</a-select-option>
								<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="repairNumber"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-input
								v-model:value="formState.repairNumber"
								: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="handleStatus"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-select :disabled="disabled" v-model:value="formState.handleStatus">
								<a-select-option :value="0">未申请</a-select-option>
								<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="repairStart"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-date-picker
								:disabled="disabled"
								v-model:value="formState.repairStart"
								valueFormat="YYYY-MM-DD HH:mm:ss"
								style="width: 250px"
							/>
						</a-form-item>
					</div>
				</a-col>
				<a-col :span="12">
					<div class="common">
						<a-form-item
							class="form-item"
							label="保养结束时间"
							name="repairEnd"
							:label-col="{ span: 8 }"
							:wrapper-col="{ span: 20 }"
						>
							<a-date-picker
								:disabled="disabled"
								v-model:value="formState.repairEnd"
								valueFormat="YYYY-MM-DD HH:mm:ss"
								style="width: 250px"
							/>
						</a-form-item>
					</div>
				</a-col>
			</a-row>

			<a-col :span="24">
				<div class="common">
					<a-form-item
						class="form-item"
						label="备注"
						name="remark"
						:label-col="{ span: 4 }"
						:wrapper-col="{ span: 20 }"
					>
						<a-textarea v-model:value="formState.remark" :disabled="disabled" placeholder="请输入备注" all-clear />
					</a-form-item>
				</div>
			</a-col>

			<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'
	import businessDEl from '@/api/security'
	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.WBBAloadDataById({ 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({
				deviceManager: '',
				deviceName: '',
				handleStatus: '',
				id: '',
				model: '',
				number: '',
				orgId: '',
				remark: '',
				repairEnd: '',
				repairNumber: '',
				repairStart: '',
				repairStatus: '',
				repairSumnumber: '',
				repairer: ''
			})

			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) => {
				formState.value.xzqhdm = ''
				if (records.value == 'edit') {
					const res = await business.XJGLedit(val)
				} else {
					const res = await business.XJGLadd(val)
					console.log('add')
				}
			}

			onMounted(() => {
				getEthnicity()
				// getGender()
			})
			// 获取下拉单位类型
			const optionsGender = ref([]) // 单位性质
			const optionsEthnicity = ref([])
			const getEthnicity = async () => {
				const res = await businessDEl.getAllList()
				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>
