<template>
	<div class="system-dataAuth-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
			>
				<template #buttons>
					<el-button type="primary" @click="onAddAuth"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
				</template>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30, useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="systemDataAuth"
					:custom-config="{ storage: true }"
				>
					<vxe-column field="tableName" title="表编码"></vxe-column>
					<vxe-column field="tableDisplay" title="表名称"></vxe-column>
					<vxe-column field="roleDisplay" title="角色名称"></vxe-column>
					<vxe-column field="roleCode" title="角色编码"></vxe-column>
					<vxe-column field="accessLevelText" title="可见等级"></vxe-column>
					<vxe-column title="操作" fixed="right" width="130" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onEditAuth(row)" title="修改"> 修改 </el-button>
							<el-button link type="danger" @click="onRowDel(row)" title="删除"> 删除 </el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
		</div>
		<data-auth-add ref="dataAuthAddRef" @updateList="queryList" />
		<data-auth-edit ref="dataAuthEditRef" @updateList="queryList" />
	</div>
</template>

<script lang="tsx" setup name="systemDataAuth">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage} from 'element-plus';
import { getDataAuthorityByRole, deleteDataAuth, baseData } from '@/api/system/roles';
import { useRoute } from 'vue-router';
import { formatDate } from '@/utils/formatTime';

// 引入组件
const DataAuthAdd = defineAsyncComponent(() => import('@/views/system/dataAuth/components/Data-auth-add.vue')); //新增数据权限
const DataAuthEdit = defineAsyncComponent(() => import('@/views/system/dataAuth/components/Data-auth-edit.vue')); //修改数据权限

// 定义变量内容
const state = reactive({
	roleId: '', //角色id
	tableData: [], //表格数据
	loading: false, //加载状态
	accessLevelOptions: [], //可用组织
});
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	queryList();
}
/** 获取用户列表 */
const route = useRoute();
state.roleId = route.query.id as string;
const queryList = () => {
	state.loading = true;
	getDataAuthorityByRole(state.roleId)
		.then((response: any) => {
			state.tableData = response?.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
}

// 新增数据权限
const dataAuthAddRef = ref<RefType>(); //新增数据权限
const onAddAuth = () => {
	dataAuthAddRef.value.openDialog(state.accessLevelOptions);
};
// 打开修改数据权限弹窗
const dataAuthEditRef = ref<RefType>(); //修改数据权限
const onEditAuth = (row: any) => {
	dataAuthEditRef.value.openDialog(row, state.accessLevelOptions);
};
// 删除数据权限
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`确定要删除本条数据，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		draggable: true,
		cancelButtonClass: 'default-button',
		type: 'warning',
	})
		.then(() => {
			deleteDataAuth(row.id).then(() => {
				ElMessage.success('删除成功');
				handleQuery();
			});
		})
		.catch(() => {});
};
// 页面基础数据
const getBaseData = async () => {
	try {
		const res: any = await baseData();
		state.accessLevelOptions = res.result.accessLevelOptions ?? [];
	} catch (error) {
		console.log(error);
	}
};
const tableRef = ref<RefType>();
const toolbarRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
	getBaseData();
});
</script>