<template>
	<div style="margin-bottom: 16px;">
		<position-search @searchData="searchData" @clearData="clearData"
			:search_params="commomParams.search"></position-search>
	</div>
	<a-c-operation @refresh="clearData"></a-c-operation>
	<a-c-table :data="commomParams.table.data" :columns="commomParams.table.columns" :pagination="commomParams.page"
		@page="getPage" :loading="loading">
		<template #default="{ record }">
			<a-row :gutter="10">
				<a-col><a-button type="primary" size="small" primary @click="audit(record)">审核</a-button></a-col>
			</a-row>
		</template>
	</a-c-table>
	<position-add :edit_record="edit_record" @successAdd="successAdd" @closeAdd="closeAdd"></position-add>
</template>

<script lang="ts" setup>
	import { ref, onMounted, watch, computed, createVNode } from 'vue';
	import PositionSearch from '@/views/homemake/position/search/index.vue';
	import PositionAdd from '@/views/homemake/position/add/add.vue';
	import { GetHousekeepingapplyList, PostHousekeepingapplyUpdate } from '@/apis/models';
	import { useCommon } from '@/hooks/useCommon';
	import { cols } from '@/views/homemake/position/columns';
	import { message } from 'ant-design-vue';
	import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
	import { Modal } from 'ant-design-vue';
	let { store, commomParams, showModal, showOtherModal1 } = useCommon();
	let loading = ref<Boolean>(true);
	onMounted(() => {
		getData(commomParams.value.search);
	})

	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 GetHousekeepingapplyList(commomParams.value.search);
			loading.value = false;
			commomParams.value.table.data = res.data.housekeepingapplys;
			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 (err) {
			loading.value = false;
		}
	}


	// 编辑
	let edit_record = ref<Object>(null)
	const edit = (record : object) => {
		edit_record.value = record;
		showModal()
	}
	const successAdd = () => {
		getData();
	}
	const closeAdd = () => {
		edit_record.value = null;
	}

	// 审核
	const audit = (record) => {
		const modal = Modal.confirm({
			title: 'Do you want to delete these items?',
			icon: createVNode(ExclamationCircleOutlined),
			content: 'When clicked the OK button, this dialog will be closed after 1 second',
			onOk() {
				PostHousekeepingapplyUpdate({ id: record.id, customer_id: record.customer_id, request_id: record.request_id, status: 2 }).then(res => {
					message.success('通过');
				})
			},
			// eslint-disable-next-line @typescript-eslint/no-empty-function
			onCancel() {
				modal.destroy();
			},
		});

	}

	// 删除
	// const delOneRole = (id: number) => {
	// 	commomParams.value.delRecord = { id: id };
	// 	PostRoleDel(commomParams.value.delRecord).then(res => {
	// 		message.success('删除成功');
	// 		getData();
	// 	})
	// }
</script>

<style lang="less" scoped>

</style>