<template>
	<div class="system-dataAuth-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<!-- 正常 -->
			<template v-if="state.roleId">
				<div class="flex-center-between mb20">
					<p class="table-title">信息列表</p>
					<div>
						<el-button type="primary" @click="onAddAuth" v-waves> <SvgIcon name="ele-Plus" class="mr5" v-auth="'100206'" />新增 </el-button>
						<el-button type="primary" v-waves @click="onImportTable" :disabled="!state.multipleSelection.length">
							<SvgIcon name="iconfont icon-daochu" class="mr5" />导出
						</el-button>
					</div>
				</div>
				<el-table :data="state.tableData" v-loading="state.loading" row-key="id" @selection-change="handleSelectionChange">
					<el-table-column type="selection" width="55" :reserve-selection="true" />
					<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="scope">
							<el-button link type="primary" @click="onEditAuth(scope.row)" v-auth="'100207'" title="修改">
								<SvgIcon name="ele-Edit" size="var(--hotline-table-icon-font-size)" />
							</el-button>
							<el-button link type="danger" @click="onRowDel(scope.row)" v-auth="'100208'" title="删除">
								<SvgIcon name="ele-Delete" size="var(--hotline-table-icon-font-size)" />
							</el-button>
						</template>
					</el-table-column>
					<template #empty>
						<Empty />
					</template>
				</el-table>
			</template>
			<template v-else>
				<Empty description="参数不正确，请关闭重新打开页面" />
			</template>
		</div>

		<AddAuth ref="addAuthRef" @updateList="handleQuery" />
		<EditAuth ref="editAuthRef" @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 table2excel from 'js-table2excel';
import { formatDate } from '/@/utils/formatTime';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
import { auth } from '/@/utils/authFunction';
import { throttle } from '/@/utils/tools';
import { getdataauthoritybyrole, deleteDataAuth, baseData } from '/@/api/system/roles';

// 引入组件
const AddAuth = defineAsyncComponent(() => import('/@/views/system/dataAuth/component/AddAuth.vue'));
const EditAuth = defineAsyncComponent(() => import('/@/views/system/dataAuth/component/EditAuth.vue'));

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

// 定义变量内容
const state = reactive<QueryState>({
	roleId: '',
	tableData: [],
	loading: false,
	multipleSelection: [],
	accessLevelOptions: <any>[], //可用组织
});
const addAuthRef = ref(); //新增数据权限
const editAuthRef = ref(); //修改数据权限
const route = useRoute();
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
/** 搜索按钮操作 节流操作 */
const handleQuery = throttle(() => {
	getList();
}, 1000);
/** 获取用户列表 */
const getList = () => {
	if (!auth('100209')) ElMessage.error('抱歉，您没有获取数据权限的权限！');
	else {
		state.loading = true;
		getdataauthoritybyrole(state.roleId)
			.then((response: any) => {
				state.tableData = response?.result ?? [];
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	}
};

// 新增数据权限
const onAddAuth = () => {
	addAuthRef.value.openDialog(state.accessLevelOptions);
};
// 表格多选
const handleSelectionChange = (val: any) => {
	state.multipleSelection = val;
};

// 打开修改数据权限弹窗
const onEditAuth = (row: any) => {
	editAuthRef.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 onImportTable = () => {
	const tabeHeader = [
		{ key: 'tableName', colWidth: '', title: '表编码', type: 'text', isCheck: true },
		{ key: 'tableDisplay', colWidth: '', title: '表名称', type: 'text', isCheck: true },
		{ key: 'roleDisplay', colWidth: '', title: '角色名称', type: 'text', isCheck: true },
		{ key: 'roleCode', colWidth: '', title: '角色编码', type: 'text', isCheck: true },
		{ key: 'accessLevelText', colWidth: '', title: '可见等级', type: 'text', isCheck: true },
	];
	table2excel(
		tabeHeader,
		state.multipleSelection,
		`${themeConfig.value.globalTitle}-${route.meta.title} ${formatDate(new Date(), 'YYYY-mm-dd HH-MM')}`
	);
};
const getBaseData = async () => {
	try {
		const res: any = await baseData();
		state.accessLevelOptions = res.result.accessLevelOptions ?? [];
	} catch (error) {
		console.log(error);
	}
};
onMounted(() => {
	if (route.query.id) {
		state.roleId = route.query.id;
		getList();
		getBaseData();
	}
});
</script>
<style lang="scss" scoped>
.system-dataAuth-container {
	.el-table {
		flex: 1;
	}
}
</style>
