<template>
	<div class="system-dataAuth-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<!-- 正常 -->
			<template v-if="state.roleId">
				<div class="mb20">
					<el-button type="primary" @click="onAddAuth" v-waves v-auth="'system:role:dataAuth:add'">
						<SvgIcon name="ele-Plus" class="mr5" />新增
					</el-button>
				</div>
				<el-table :data="state.tableData" v-loading="state.loading" row-key="id">
					<el-table-column type="index" width="60" label="序号" />
					<el-table-column prop="tableName" label="表编码" show-overflow-tooltip></el-table-column>
					<el-table-column prop="tableDisplay" label="表名称" show-overflow-tooltip> </el-table-column>
					<el-table-column prop="roleDisplay" label="角色名称" show-overflow-tooltip></el-table-column>
					<el-table-column prop="roleCode" label="角色编码" show-overflow-tooltip></el-table-column>
					<el-table-column prop="accessLevelText" label="可见等级" show-overflow-tooltip width="100"> </el-table-column>
					<el-table-column label="操作" width="130" fixed="right" align="center">
						<template #default="{row}">
							<el-button link type="primary" @click="onEditAuth(row)" v-auth="'system:role:dataAuth:edit'" title="修改"> 修改 </el-button>
							<el-button link type="danger" @click="onRowDel(row)" v-auth="'system:role:dataAuth:delete'" title="删除"> 删除 </el-button>
						</template>
					</el-table-column>
					<template #empty>
						<Empty />
					</template>
				</el-table>
			</template>
			<template v-else>
				<Empty description="参数不正确，请关闭重新打开页面" />
			</template>
		</div>

		<data-auth-add ref="dataAuthAddRef" @updateList="handleQuery" />
		<data-auth-edit ref="dataAuthEditRef" @updateList="handleQuery" />
	</div>
</template>

<script lang="ts" setup name="systemDataAuth">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useRoute } from 'vue-router';
import { auth } from '/@/utils/authFunction';
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')); //修改数据权限

// 定义接口来定义对象的类型
interface QueryState {
	roleId: string | any[];
	tableData: Array<any>;
	loading: boolean;
	accessLevelOptions: Array<any>;
}

// 定义变量内容
const state = reactive<QueryState>({
	roleId: '', //角色id
	tableData: [],  //表格数据
	loading: false, //加载状态
	accessLevelOptions: <EmptyArrayType>[], //可用组织
});
/** 搜索按钮操作 节流操作 */
const handleQuery = throttle(() => {
	queryList();
}, 1000);
/** 获取用户列表 */
const queryList = throttle(() => {
	if (!auth('system:role:dataAuth:query')) ElMessage.error('抱歉，您没有权限获取数据权限列表！');
	else {
		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>
