<template>
	<a-modal v-model:visible="showAddCompanyModel" title="加入企业" ok-text="提交" cancel-text="取消" @ok="sumbitForm"
		@cancel="cancelModal" width="50%">
		<a-form :model="createForm" :label-col="{span: 4}" labelAlign="right" style="margin-top: 20px;">
			<a-row gutter="20">
				<a-col span="24">
					<a-form-item required label="选择企业">
						<search-select placeholder="请搜索选择企业" :list="company_list" :select_value="addOtherForm.full_name"
							@searchData="companySearch" @getSelectValue="getCompanyValue"
							:select_disabled="false"></search-select>
					</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 { getCompanyList, PostRecruitmentBookAdd } from '@/apis/models';
	import { dataForm, otherDataForm, reset } from '@/views/jobFair/list/add-company/data.ts';
	import { useCommon } from '@/hooks/useCommon';
	let { store, openAddModel, hideModal, dayjs, disabledDate, richOption, message, imageprefix } = useCommon();
	const emit = defineEmits();
	let props = defineProps(['add_company_record']);

	let createForm = ref(dataForm)
	let addOtherForm = ref(otherDataForm)

	onMounted(() => {
		companySearch({ page: 1, pagesize: 10 })
	})


	// 选择企业
	let company_list = ref<Object[]>([])
	const companySearch = (val : Object) => {
		getCompanyList(val).then((res : object) => {
			const data = res.data.list.map((item : object) => ({
				label: item.full_name,
				value: item.id,
			}));
			company_list.value = data;
		})
	}
	const getCompanyValue = (val : Object) => {
		createForm.value.company_id = val.key;
		addOtherForm.value.full_name = val.label;
	}


	const sumbitForm = () => {
		console.log(createForm.value)
		if (createForm.value.company_id) {
			PostRecruitmentBookAdd(createForm.value).then(res => {
				message.success('企业预约成功');
				cancelModal();
			}).catch(err => {
			})
		} else {
			message.warning('请补充完整信息');
		}
	}

	const resetForm = () => {
		addOtherForm.value = reset().otherDataForm;
		createForm.value = {
			recruitment_id: createForm.value.recruitment_id ? createForm.value.recruitment_id : 0,
			company_id: 0,
			status: 1,
		};
	}

	const cancelModal = () => {
		resetForm();
		store.commit('getShowAddCompanyModel', {
			showAddCompanyModel: false
		})
	}


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

	watch(() => props.add_company_record, (newVal) => {
		if (newVal) {
			createForm.value = {
				recruitment_id: newVal.id,
				company_id: 0,
				status: 1,
			}
		}
	})
</script>

<style>

</style>