<template>
	<div class="page">
		<BaseTable
			ref="baseTable"
			:action="action"
			:batch-operation="batchOperation"
			:columns="columns"
			:max-operation="3"
			:operation="operation"
			:search="search"
		>
			<template v-slot:operation>
				<a-button v-if="checkPermission('新增')" type="primary" @click="useManageMethod.handleUpdate(undefined)"> 新增 </a-button>
			</template>
		</BaseTable>
		<dc-drawer
			:closable="true"
			:maskClosable="false"
			:showBottom="!update.id"
			:title="update.title"
			:visible="update.visible"
			:width="update.width"
			destroyOnClose
			placement="right"
			@close="update.visible = false"
		>
			<Add :id="update.id" @cancel="update.visible = false" @refresh="useManageMethod.refresh" />
		</dc-drawer>
		<dc-drawer
			:closable="true"
			:maskClosable="false"
			:showBottom="false"
			:title="detailPanel.title"
			:visible="detailPanel.visible"
			:width="detailPanel.width"
			destroyOnClose
			placement="right"
			@close="detailPanel.visible = false"
		>
			<Detail :id="detailPanel.id" />
		</dc-drawer>
	</div>
</template>

<script lang="ts" setup>
import { defineAsyncComponent, ref } from 'vue'
import requestMethod from '../../../api/account/organization'
import BaseTable from '../../../components/table/BaseTable.vue'
import columns from './columns'
import search from './search'
import { BatchOperationProps, OperationProps } from '../../../components/table/table-helper'
import { confirmMethod, messageMethod } from '../../../utils/message-util'
import { ResultModel } from '../../../http/result-model'
import { drawerMethod, manageMethod } from '../../../components/form/method'
import { AccountOrg } from '../../../entity/account/account-org'
import { checkPermission } from '../../../utils/permission-util'

const Add = defineAsyncComponent(() => import('./Add.vue'))
const Detail = defineAsyncComponent(() => import('./Detail.vue'))

const useManageMethod = manageMethod<AccountOrg>()
const useDetailDrawerMethod = drawerMethod<AccountOrg>()

// 修改面板参数
const update = useManageMethod.update
// 详情面板参数
const detailPanel = useDetailDrawerMethod.panel

// 列表查询信息的api
const action = ref<() => void>(requestMethod.page)

// 批量删除
const batchOperation = ref<BatchOperationProps<AccountOrg>[]>([
	{
		title: '批量删除',
		danger: true,
		permission: '删除',
		action: async ({ keys }) => {
			// 刷新页面
			const resultModel: ResultModel<boolean> = await requestMethod.deleteBatch(keys)
			messageMethod.success(resultModel.message)
			useManageMethod.baseTable().pageRequest()
		}
	}
])

// 列表操作信息
const operation = ref<OperationProps<AccountOrg>[]>([
	{
		title: '详情',
		permission: '修改',
		action: useDetailDrawerMethod.open
	},
	{
		title: '修改',
		permission: true,
		action: useManageMethod.handleUpdate
	},
	{
		title: '删除',
		danger: true,
		permission: true,
		action: (vo: AccountOrg) => {
			confirmMethod.info('确认删除？', () => {
				requestMethod.delete(vo.id).then(data => {
					messageMethod.success(data.message)
					useManageMethod.baseTable().pageRequest()
				})
			})
		}
	}
])
</script>

<style scoped>
.page {
	height: 100%;
	display: flex;
	flex-direction: column;
}
</style>
