<template>
	<div>
		<template v-if="state">
			<a-form :model="createForm" layout="vertical"  class="resume-form">
				<a-row :gutter="20">
					<a-col span="24">
						<a-form-item label="个人简介" name="description">
							<QuillEditor theme="snow" :options="richOption" toolbar="full"
								v-model:content="addOtherForm.description" @update:content="onEditorUpdate($event)"
								contentType="html" />
						</a-form-item>
					</a-col>
					<a-col span="24">
						<a-flex justify="flex-end">
							<a-space>
								<a-button  @click="resetForm" size="large">取消</a-button>
								<a-button type="primary" @click="sumbitForm" size="large">保存</a-button>
							</a-space>
						</a-flex>
					</a-col>
				</a-row>
			</a-form>
		</template>
		<template v-else>
			<template v-if="addOtherForm.description">
				<div v-html="addOtherForm.description"></div>
			</template>
			<template v-else>
				<a-row>
					<a-col span="24">
						<a-empty>
							<template #description>
								暂无信息
							</template>
						</a-empty>
					</a-col>
				</a-row>
			</template>
		</template>
	</div>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed, defineProps, watch, defineEmits, createVNode } from 'vue';
	import { PostJobapplicantAddintroduction,PostJobapplicantUpdateintroduction, GetJobapplicantIntroductiondetail } from '@/apis/models';
	import { intersectionAlike } from '@/utils/dataHelper';
	import { warnToast, successToast } from '@/utils/toastHelper';
	import { dataForm, otherDataForm, reset } from '@/components/jobseeker/resume/introduction/data.ts';
	import { useCommon } from '@/hooks/useCommon';
	let { store, dayjs, richOption, ExclamationCircleOutlined, Modal } = useCommon();
	const dayjsRef = ref(dayjs);
	let props = defineProps(['form_state']);
	const emit = defineEmits();
	let state = ref<Boolean>(false)
	let detail = ref({})
	let createForm = ref<JobseekerIntroductionType.JobseekerIntroductionFormType>(dataForm)
	let addOtherForm = ref<JobseekerIntroductionType.OtherFormType>(otherDataForm)

	onMounted(() => {
		getBasic()
	})

	const getBasic = () => {
		GetJobapplicantIntroductiondetail().then(res => {
			detail.value = res.data;
			createForm.value = intersectionAlike(createForm.value, res.data)
			createForm.value.id = res.data.id;
			addOtherForm.value = {
				description: res.data.description,
			}
		})
	}

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


	const sumbitForm = () => {
		if (createForm.value.id) {
			PostJobapplicantUpdateintroduction(createForm.value).then(res => {
				successToast('保存成功');
				resetForm()
				getBasic();
			}).catch(err => {
			})
		} else {
			PostJobapplicantAddintroduction(createForm.value).then(res => {
				successToast('保存成功');
				resetForm()
				getBasic();
			}).catch(err => {
			})
		}
	}

	const resetForm = () => {
		createForm.value = reset().dataForm as JobseekerIntroductionType.JobseekerIntroductionFormType;
		addOtherForm.value = reset().otherDataForm as JobseekerIntroductionType.OtherFormType;
		state.value = false;
		emit("quitEdit")
	}

	watch(() => [props.form_state], (newVal) => {
		state.value = newVal[0];
		getBasic();
	})
</script>

<style>
</style>

