<template>
	<a-modal v-model:visible="openAddModel" :title="title" ok-text="提交" cancel-text="取消" @ok="sumbitForm"
		@cancel="cancelModal" width="50%">
		<a-form :model="createForm" layout="vertical">
			<a-row gutter="20">
				<a-col span="24">
					<a-form-item required label="招聘会主题" name="title">
						<a-input v-model:value="createForm.title" placeholder="请输入招聘会主题" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item label="招聘会内容" name="content">
						<QuillEditor theme="snow" :options="options" toolbar="full"
							v-model:content="addOtherForm.content" @update:content="onEditorUpdate($event)"
							contentType="html" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="选择日期" name="open_date">
						<a-date-picker v-model:value="addOtherForm.open_date" @Change="openDateChange"
							style="width: 100%;" />
					</a-form-item>
				</a-col>
				<a-col span="24" v-if="addOtherForm.open_date">
					<a-form-item required label="选择具体的时间">
						<a-time-picker v-model:value="addOtherForm.time" valueFormat="HH:mm:ss" @change="timeChange" />
					</a-form-item>
				</a-col>
				<a-col span="24">
					<a-form-item required label="地点" name="address">
						<a-input v-model:value="createForm.address" placeholder="请输入地址" />
					</a-form-item>
				</a-col>
				<a-col span="24" v-if="createForm.id">
					<a-form-item required label="审核结果" name="status">
						<a-radio-group v-model:value="createForm.status" button-style="solid">
							<a-radio-button :value="1">待审</a-radio-button>
							<a-radio-button :value="2">通过</a-radio-button>
							<a-radio-button :value="3">拒绝</a-radio-button>
						</a-radio-group>
					</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 { PostRecruitmentUpdate, PostRecruitmentAdd, GetCompanyInfo, GetRecruitmentDetail } from '@/apis/models';
	import { dataForm, otherDataForm, reset } from '@/views/jobFair/list/add/data.js';
	import he from 'he';
	import { useCommon } from '@/hooks/useCommon';
	let { store, openAddModel, hideModal, dayjs, disabledDate, richOption, message, imageprefix } = useCommon();
	const dayjsRef = ref(dayjs);
	const emit = defineEmits();
	let props = defineProps(['edit_record']);
	let title = ref<String>('新增招聘会');
	const options = ref(richOption)

	let createForm = ref<companyDepartmentType.addFormType>(dataForm)
	let addOtherForm = ref<companyDepartmentType.addOtherFormType>(otherDataForm)


	// 开始日期
	const openDateChange = (val) => {
		addOtherForm.value.open_date = val;
	}

	// 选择时间
	const timeChange = (val) => {
		addOtherForm.value.time = val;
	}


	// 富文本
	const onEditorUpdate = (data) => {
		createForm.value.content = data
	}



	const sumbitForm = () => {
		console.log(addOtherForm.value.open_date)
		createForm.value.open_date = dayjsRef.value(addOtherForm.value.open_date).format('YYYY-MM-DD') + ' ' + addOtherForm.value.time
		console.log(createForm.value)
		if (createForm.value.title && createForm.value.content && createForm.value.open_date && createForm.value.address) {
			if (!createForm.value.id) {
				PostRecruitmentAdd(createForm.value).then(res => {
					message.success('新增招聘会成功');
					hideModal();
					resetForm();
					emit('successAdd');
				}).catch(err => {
				})
			} else {
				PostRecruitmentUpdate(createForm.value).then(res => {
					message.success('修改招聘会信息成功');
					hideModal();
					resetForm();
					emit('successAdd');
				}).catch(err => {
				})
			}

		} else {
			message.warning('请补充完整信息');
		}

	}

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


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

	watch(() => props.edit_record, async (newVal) => {
		if (newVal) {
			title.value = '编辑招聘会';
			addOtherForm.value = { 	
				content: he.decode(newVal.content),
				open_date: dayjsRef.value(newVal.open_date.substring(0, 10)),
				time: newVal.open_date.substring(11, 19)
			}
			createForm.value = {
				id: newVal.id,
				title: newVal.title,
				content: newVal.content,
				open_date: newVal.open_date,
				address: newVal.address,
				status: newVal.status
			}
		} else {
			title.value = '新增招聘会';
		}
	})
</script>

<style>

</style>