<template>
	<div class="system-dataAuth-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<!-- 正常 -->
			<template v-if="state.roleId">
				<ProTable ref="proTableRef" :columns="columns" :data="state.tableData" @updateTable="queryList" :loading="state.loading" :pagination="false">
					<template #tableHeader="scope">
						<el-button type="primary" @click="onAddAuth"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
					</template>
					<!-- 表格操作 -->
					<template #operation="{ row }">
						<el-button link type="primary" @click="onEditAuth(row)" title="修改"> 修改 </el-button>
						<el-button link type="danger" @click="onRowDel(row)" title="删除"> 删除 </el-button>
					</template>
				</ProTable>
			</template>
			<template v-else>
				<Empty description="参数不正确，请关闭重新打开页面" />
			</template>
		</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, ElButton } from 'element-plus';
import { useRoute } from 'vue-router';
import { throttle } from '@/utils/tools';
import { getDataAuthorityByRole, deleteDataAuth, baseData } from '@/api/system/roles';

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

const proTableRef = ref<RefType>(); // 表格ref
// 表格配置项
const columns = ref<any[]>([
	{ prop: 'tableName', label: '表编码', showOverflowTooltip: true },
	{ prop: 'tableDisplay', label: '表名称', showOverflowTooltip: true },
	{ prop: 'roleDisplay', label: '角色名称', showOverflowTooltip: true },
	{ prop: 'roleCode', label: '角色编码', showOverflowTooltip: true },
	{ prop: 'accessLevelText', label: '可见等级', showOverflowTooltip: true, width: 100 },
	{ prop: 'operation', label: '操作', fixed: 'right', width: 130, align: 'center' },
]);

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

// 新增数据权限
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 route = useRoute(); //路由
onMounted(() => {
	if (route.query.id) {
		state.roleId = route.query.id;
		queryList();
		getBaseData();
	}
});
</script>
<style lang="scss" scoped>
.system-dataAuth-container {
	.el-table {
		flex: 1;
	}
}
</style>
