<template>
	<a-form :model="createForm" :label-col="{span: 8}" labelAlign="right">
		<a-row :gutter="20">
			<a-col span="12">
				<a-row>
					<a-col span="24">
						<a-form-item required label="联系人" name="contact">
							<a-input v-model:value="createForm.contact" placeholder="请输入联系人" />
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="担任职位" name="possession">
							<a-input v-model:value="createForm.possession" placeholder="请输入联系人职位" />
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item required label="联系电话" name="phone">
							<a-input v-model:value="createForm.phone" placeholder="请输入联系电话" />
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="是否公开联系电话" name="phone_public">
							<a-radio-group v-model:value="createForm.phone_public" button-style="solid">
								<a-radio-button :value="1">是</a-radio-button>
								<a-radio-button :value="2">否</a-radio-button>
							</a-radio-group>
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="手机" name="mobile">
							<a-input v-model:value="createForm.mobile" placeholder="请输入手机" />
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="是否公开手机" name="mobile_public">
							<a-radio-group v-model:value="createForm.mobile_public" button-style="solid">
								<a-radio-button :value="1">是</a-radio-button>
								<a-radio-button :value="2">否</a-radio-button>
							</a-radio-group>
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="邮箱" name="email">
							<a-input v-model:value="createForm.email" placeholder="请输入邮箱" />
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="是否公开邮箱" name="email_public">
							<a-radio-group v-model:value="createForm.email_public" button-style="solid">
								<a-radio-button :value="1">是</a-radio-button>
								<a-radio-button :value="2">否</a-radio-button>
							</a-radio-group>
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="传真" name="fax">
							<a-input v-model:value="createForm.fax" placeholder="请输入传真" />
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="是否公开传真" name="fax_public">
							<a-radio-group v-model:value="createForm.fax_public" button-style="solid">
								<a-radio-button :value="1">是</a-radio-button>
								<a-radio-button :value="2">否</a-radio-button>
							</a-radio-group>
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="其他地址" name="other_address">
							<a-input v-model:value="createForm.other_address" placeholder="请输入其他地址" />
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item label="面试地址" name="interview_address">
							<a-input v-model:value="createForm.interview_address" placeholder="请输入面试地址" />
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-form-item required label="保存">
							<a-button type="primary" block @click="sumbitForm">保存</a-button>
						</a-form-item>
					</a-col>
				</a-row>
			</a-col>
		</a-row>
	</a-form>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed, watch, defineEmits, defineProps } from 'vue';
	import { PostCompanyJobAdd, PostCompanyJobEdit } from '@/apis/models';
	import { warnToast, successToast } from '@/utils/toastHelper';
	import { intersectionAlike } from '@/utils/dataHelper';
	const emit = defineEmits();
	const props = defineProps(['job_detail']);
	
	let createForm : companyListType.addrFormType = ref({
		contact: '',
		possession: '',
		phone: '',
		phone_public: 2,
		mobile: '',
		mobile_public: 2,
		email: '',
		email_public: 2,
		fax: '',
		fax_public: 2,
		other_address: '',
		interview_address: '',
	})
	
	const job_id = ref<number>(null);
	watch(() => [props.job_detail], async (newVal : string[], oldVal) => {
		if (newVal[0]) {
			job_id.value = newVal[0].id;
			createForm.value.id = newVal[0].id;
			createForm.value = intersectionAlike(createForm.value, newVal[0]);
		}
	}, { immediate: true })
	

	const sumbitForm = () => {
		const newObject = { ...JSON.parse(sessionStorage.getItem('first')), ...JSON.parse(sessionStorage.getItem('second')), ...createForm.value }
		console.log(newObject)
		if(!job_id.value) {
			PostCompanyJobAdd(newObject).then(res => {
				successToast('保存成功');
				sessionStorage.removeItem('first')
				sessionStorage.removeItem('firstOther')
				sessionStorage.removeItem('second')
				sessionStorage.removeItem('secondOther')
				resetForm();
			}).catch(err => {
			})
		} else {
			PostCompanyJobEdit(newObject).then(res => {
				successToast('编辑成功');
				sessionStorage.removeItem('first')
				sessionStorage.removeItem('firstOther')
				sessionStorage.removeItem('second')
				sessionStorage.removeItem('secondOther')
				resetForm();
			}).catch(err => {
			})
			
		}
		emit('saveSuccess')
	}

	const resetForm = () => {
		createForm.value = {
			contact: '',
			possession: '',
			phone: '',
			phone_public: 2,
			mobile: '',
			mobile_public: 2,
			email: '',
			email_public: 2,
			fax: '',
			fax_public: 2,
			other_address: '',
			interview_address: '',
		}
	}
</script>

<style>
</style>