<template>
	<department-search @searchData="searchData" @clearData="clearData"
		:search_params="commomParams.search"></department-search>
	<a-c-operation @refresh="clearData"  @add="add"></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="edit(record)">编辑</a-button></a-col>
			</a-row>
		</template>
	</a-c-table>
	<department-add  v-if="openForm"  :edit_record="edit_record" @successAdd="successAdd" @closeAdd="closeAdd"></department-add>
</template>

<script lang="ts" setup>
	import { ref, onMounted, watch, computed, onBeforeUnmount } from 'vue';
	import DepartmentSearch from '@/views/company/department/search/index.vue';
	import DepartmentAdd from '@/views/company/department/add/add.vue';
	import { GetCompanyDepartmentList } from '@/apis/models';
	import { useCommon } from '@/hooks/useCommon';
	import { cols } from '@/views/company/department/columns';
	import { message } from 'ant-design-vue';
	let { store, commomParams, showModal, showOtherModal1 } = useCommon();
	let loading = ref<Boolean>(true);
	let openForm = ref<Boolean>(false);
	onMounted(() => {
		if(sessionStorage.getItem('company_id')) {
			commomParams.value.search.company_id = sessionStorage.getItem('company_id')
		}
		getData(commomParams.value.search);
	})
	
	onBeforeUnmount(() => {
		sessionStorage.removeItem('company_id')
		sessionStorage.removeItem('company_name')
		openForm.value = false
	})
	
	const add = (data : object) => {
		openForm.value = true
	}

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

	const clearData = (data : object) => {
		sessionStorage.removeItem('company_id')
		sessionStorage.removeItem('company_name')
		openForm.value = false
		if (data) {
			commomParams.value.search = data
		} else {
			commomParams.value.search = {
				page: 1,
				pagesize: 10,
				sort: 'id',
				sortby: 'desc',
				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 GetCompanyDepartmentList(commomParams.value.search);

			loading.value = false;
			commomParams.value.table.data = res.data.rows;
			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) => {
		openForm.value = true
		edit_record.value = record;
		showModal()
	}
	const successAdd = () => {
		getData();
	}
	const closeAdd = () => {
		openForm.value = false
		edit_record.value = null;
	}

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

<style lang="less" scoped>

</style>