<template>
	<xn-form-container
		:title="formData.id ? '编辑账号' : '新增账号'"
		: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="8">
					<a-form-item label="员工工号" name="userId">
						<a-select :disabled="formData.userId" v-model:value="formData.userId" placeholder="请选择员工工号" @change="personChange">
							<a-select-option v-for="item in personList" :value="item.userId">{{item.userId}}</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="员工姓名" name="userName">
						<a-input v-model:value="formData.userName" disabled placeholder="自动填充"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="工作岗位" name="job">
						<a-select disabled mode="multiple" v-model:value="formData.job" placeholder="自动填充">
							<a-select-option :value="1">场长</a-select-option>
							<a-select-option :value="3">生物安全负责人</a-select-option>
							<a-select-option :value="4">PCR检测人员</a-select-option>
							<a-select-option :value="5">洗消站点工作人员</a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="工作地点" name="workLocation">
						<a-input disabled v-model:value="formData.workLocation" placeholder="自动填充"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="小程序账号" name="account">
						<a-input v-model:value="formData.account" placeholder="填写小程序账号"></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="8">
					<a-form-item label="小程序密码" name="password">
						<a-input type="password" v-model:value="formData.password" placeholder="填写小程序密码"></a-input>
					</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="washAccountForm">
	import { required, rules } from "@/utils/formRules";
	import washEntranceApi from "@/api/wash/washEntranceApi";
	import {cloneDeep} from "lodash-es";
	const visible = ref(false)
	const formRef = ref()
	const emit = defineEmits({ successful: null })
	const submitLoading = ref(false)
	const locationList = ref([])
	const formData = ref({})
	const formRules = ref({
		id: [required('请选择员工工号')],
		account: [required('请输入小程序账号')],
		password: [required('请输入小程序密码')]
	})
	const personList = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		initPerson()
	}

	const initPerson = () => {
		washEntranceApi.getPersonAll().then(res => {
			personList.value = res
		})
	}

	const personChange = (val) => {
		personList.value.forEach(item => {
			if (item.userId === val) {
				formData.value.userName = item.userName
				formData.value.workLocation = item.workLocation
				formData.value.job = item.job.split(',').map(Number)
			}
		})
	}

	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
		formData.value = {}
		submitLoading.value = false
	}

	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			if(formData.value.id) {
				let params = {
					id: formData.value.id,
					userId: formData.value.userId,
					account: formData.value.account,
					password: formData.value.password
				}
				washEntranceApi.editAccountPage(params).then(() => {
					onClose()
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			} else {
				let params = {
					userId: formData.value.userId,
					account: formData.value.account,
					password: formData.value.password
				}
				washEntranceApi.addAccountPage(params).then(() => {
					onClose()
					emit('successful')
				}).finally(() => {
					submitLoading.value = false
				})
			}
		})
	}

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

<style scoped>

</style>
