<template>
	<a-modal v-model:visible="openAddModel" :title="title" ok-text="提交" cancel-text="取消" @ok="sumbitForm"
		@cancel="cancelModal" width="50%">
		<a-form :model="createForm" :label-col="{span: 4}" labelAlign="right">
			<a-row gutter="20">
				<a-col span="24">
					<a-form-item required label="手机号" name="mobile">
						<a-input v-model:value="createForm.mobile" placeholder="请输入手机号" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="密码" name="password">
						<a-input-password placeholder="密码"
							v-model:value="createForm.password"></a-input-password>
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="邮箱" name="email">
						<a-input v-model:value="createForm.email" placeholder="请输入邮箱" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
	</a-modal>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed, defineProps, watch, defineEmits } from 'vue';
	import { PostCustomerAdd, PostCustomerUpdate,GetCustomerDetail } from '@/apis/models';
	import { dataForm, otherDataForm, reset } from '@/views/customer/list/add/data.ts';
	import { useCommon } from '@/hooks/useCommon';
	let { store, openAddModel, hideModal, message, richOption } = useCommon();
	const emit = defineEmits();
	let props = defineProps(['edit_record']);
	let title = ref<String>('新增求职者账号');

	let createForm = ref<CustomerListType.addFormType>(dataForm)


	const sumbitForm = () => {
		if (!createForm.value.id) {
			PostCustomerAdd(createForm.value).then(res => {
				message.success('创建账号成功');
				hideModal();
				resetForm();
				emit('successAdd');
			}).catch(err => {
			})
		} else {
			PostCustomerUpdate(createForm.value).then(res => {
				message.success('修改账号成功');
				hideModal();
				resetForm();
				emit('successAdd');
			}).catch(err => {
			})
		}
	}

	const resetForm = () => {
		createForm.value = reset().dataForm;
	}
	const cancelModal = () => {
		emit('closeAdd');
		resetForm();
		hideModal();
	}


	openAddModel = computed(() => {
		return store.state.openAddModel;
	})

	watch(() => props.edit_record, (newVal) => {
		if (newVal) {
			title.value = '编辑求职者账号';
			createForm.value = {
				id: newVal.id,
				role: 1,
				mobile: newVal.mobile,
				username: newVal.username,
				password: newVal.password,
				email: newVal.email,
			}
		} else {
			title.value = '新增求职者账号';
		}
	})
</script>

<style>

</style>