<template>
	<a-search @searchData="searchData" @clearData="clearData" :search_params="commomParams.search"></a-search>
	<a-biaoge :data="commomParams.table.data" :columns="commomParams.table.columns" :pagination="commomParams.page"
		@page="getPage" :loading="loading">
		<template #default="{ record }">
			<a-row :gutter="20">
				<a-col>
					<a-dropdown placement="bottomRight">
						<template #overlay>
							<a-menu>
								<a-menu-item key="1">
									<a-button @click="edit(record)" block>修改</a-button>
								</a-menu-item>
							<!-- 	<a-menu-item key="2">
									<a-button @click="edit(record)" block>重新发布</a-button>
								</a-menu-item> -->
								<a-menu-item key="3">
									<a-button v-if="record.status == 1" @click="inviteStatus(record.id, 2)" block>暂停招聘</a-button>
									<a-button v-if="record.status == 2" @click="inviteStatus(record.id, 1)" block>重新发布</a-button>
								</a-menu-item>
								<a-menu-item key="5">
									<a-button @click="putInRecycle(record.id)" block>放入回收站</a-button>
								</a-menu-item>
								<a-menu-item key="4">
									<a-button @click="del(record.id)" block>删除</a-button>
								</a-menu-item>
							</a-menu>
						</template>
						<a-button>
							操作
							<DownOutlined />
						</a-button>
					</a-dropdown>
				</a-col>
			</a-row>
		</template>
	</a-biaoge>
</template>

<script setup lang="ts">
	import { ref, onMounted, computed, defineEmits, createVNode } from 'vue';
	import { PostCompanyJobList, PostCompanyJobRecycle, PostCompanyJobStatusChange, PostCompanyJobDel} from '@/apis/models';
	import ASearch from '@/components/company/invite/manage/search.vue';
	import { useCommon } from '@/hooks/useCommon';
	import { cols } from '@/components/company/invite/manage/columns';
	import { warnToast, successToast } from '@/utils/toastHelper';
	import { DownOutlined } from '@ant-design/icons-vue';
	const emit = defineEmits();
	let { commomParams, ExclamationCircleOutlined, Modal } = useCommon();
	let loading = ref<Boolean>(true);

	onMounted(() => {
		getData();
	})

	const searchData = (data : object) => {
		commomParams.value.search = data
		getData();
	}

	const clearData = (data : object) => {
		if (data) {
			commomParams.value.search = data
		} else {
			commomParams.value.search = {
				page: 1,
				pagesize: 10,
				sort: 'id',
				sortby: 'asc',
				keyword: ''
			}
		}
		getData();
	}


	const getPage = (data : object) => {
		commomParams.value.search.page = data.current;
		commomParams.value.search.pagesize = data.pageSize;
		getData();
	}
	const getData = async () => {
		try {
			loading.value = true;
			let res = await PostCompanyJobList(commomParams.value.search);
			loading.value = false;
			commomParams.value.table.data = res.data.list;
			commomParams.value.table.columns = cols;
			commomParams.value.page = {
				current: commomParams.value.search.page,
				pageSize: commomParams.value.search.pagesize,
				total: res.data.total,
				pageSizeOptions: ['10', '20', '30', '40'],
				hideOnSinglePage: false,
				showSizeChanger: true
			};
		} catch {
			loading.value = false;
		}
	}

	// 修改
	const edit = (record : Object) => {
		emit('toEdit', { record: record })
	}
	
	// 放入回收站
	const inviteStatus = (id, num) => {
		Modal.confirm({
			title: `是否要${num == 2 ? '暂停招聘' : '重新发布'}` ,
			centered: true,
			icon: createVNode(ExclamationCircleOutlined),
			onOk() {
				PostCompanyJobStatusChange({ id: id, status:num  }).then(res => {
					successToast('保存成功')
					getData();
				})
			},
			onCancel() {
	
			},
		});
	}
	

	// 放入回收站
	const putInRecycle = (id) => {
		Modal.confirm({
			title: '是否要放入回收站',
			centered: true,
			icon: createVNode(ExclamationCircleOutlined),
			onOk() {
				PostCompanyJobRecycle({ id: id }).then(res => {
					successToast('已放入回收站')
					getData();
				})
			},
			onCancel() {

			},
		});
	}


	const del = (id : number) => {
		commomParams.value.delParam = { id: id };
		Modal.confirm({
			title: '是否要删除该职位',
			centered: true,
			icon: createVNode(ExclamationCircleOutlined),
			onOk() {
				PostCompanyJobDel(commomParams.value.delParam).then(res => {
					successToast('删除成功');
					getData();
				})
			},
			onCancel() {
		
			},
		});
		
	}
</script>

<style scoped lang="less">

</style>