<template>
	<div class="system-dataAuth-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<!-- 正常 -->
			<template v-if="state.queryParams.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" />新增 </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="role.displayName" label="角色名称" show-overflow-tooltip></el-table-column>
					<el-table-column prop="role.name" label="角色编码" show-overflow-tooltip></el-table-column>
					<el-table-column prop="role.description" label="角色说明" show-overflow-tooltip width="400"></el-table-column>
					<el-table-column prop="table.displayName" label="数据表" show-overflow-tooltip width="100"> </el-table-column>
					<el-table-column prop="authorityType" label="权限方式" show-overflow-tooltip width="100">
						<template #default="scope">
							{{ state.options[scope.row.authorityType].label }}
						</template>
					</el-table-column>
					<el-table-column label="操作" width="130" fixed="right" align="center">
						<template #default="scope">
							<el-button text type="primary" @click="onEditAuth(scope.row)" v-auth="'100203'" title="修改">
								<SvgIcon name="ele-Edit" size="var(--hotline-table-icon-font-size)" />
							</el-button>
							<el-button text type="danger" @click="onRowDel(scope.row)" v-auth="'100202'" 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 { throttle } from '/@/utils/tools';
import { getdataauthoritybyrole, deleteDataAuth } 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 {
	queryParams: {
		roleid: string | any[];
	};
	tableData: Array<any>;
	loading: boolean;
	multipleSelection: Array<any>;
	options: Array<any>;
}

// 定义变量内容
const state = reactive<QueryState>({
	queryParams: {
		roleid: '',
	},
	tableData: [],
	loading: false,
	multipleSelection: [],
	options: [
		{
			label: '全部可见',
			value: 0,
		},
		{
			label: '本部',
			value: 1,
		},
		{
			label: '本部及以下',
			value: 2,
		},
		{
			label: '创建人',
			value: 3,
		},
	], //可用组织
});
const addAuthRef = ref(); //新增数据权限
const editAuthRef = ref(); //修改数据权限
const route = useRoute();
/** 搜索按钮操作 节流操作 */
const handleQuery = throttle(() => {
	getList();
}, 1000);
/** 获取用户列表 */
const getList = () => {
	state.loading = true;
	getdataauthoritybyrole(state.queryParams)
		.then((response: any) => {
			state.tableData = response?.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};

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

// 打开修改数据权限弹窗
const onEditAuth = (row: any) => {
	editAuthRef.value.openDialog(row);
};
// 删除数据权限
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: 'name', colWidth: '', title: '姓名', type: 'text', isCheck: true },
		{ key: 'userName', colWidth: '', title: '账号', type: 'text', isCheck: true },
		{ key: 'orgName', colWidth: '', title: '所属部门', type: 'text', isCheck: true },
		{ key: 'roles', colWidth: '', title: '角色', type: 'text', isCheck: true },
		{ key: 'phoneNo', colWidth: '', title: '电话号码', type: 'text', isCheck: true },
		{ key: 'staffNo', colWidth: '', title: '工号', type: 'text', isCheck: true },
		{ key: 'defaultTelNo', colWidth: '', title: '默认分机', type: 'text', isCheck: true },
		{ key: 'creationTime', colWidth: '', title: '更新时间', type: 'text', isCheck: true },
	];
	table2excel(tabeHeader, state.multipleSelection, `用户信息 ${formatDate(new Date(), 'YYYY-mm-dd HH-MM')}`);
};
onMounted(() => {
	if (route.query.id) {
		state.queryParams.roleid = route.query.id;
		getList();
	}
});
</script>
<style lang="scss" scoped>
.system-dataAuth-container {
	.el-table {
		flex: 1;
	}
}
</style>
