<!-- 角色管理 -->
<template>
	<div class='roleManage'>
		<div style="border: 1px solid #dedede;height: 100%;">
			<el-row :gutter="0" style="height: 100%;">
				<el-col :span="4">
					<div class="leftTreeTitle">部门列表</div>
					<div class="leftTree">
						<el-tree :data="data" @current-node-key="currentDepartment" :default-expand-all="true" :expand-on-click-node="false" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
					</div>
				</el-col>
				<el-col :span="20" style="height: 100%;">
					<div style="border-left: 1px solid #dedede;height: 100%;">
						<el-table
										:data="tableRoleData"
										border
										align="center"
										style="width: 100%">
							<el-table-column
											align="center"
											prop="department"
											label="部门名称">
							</el-table-column>
							<el-table-column
											align="center"
											prop="name"
											label="角色名称">
							</el-table-column>
							<el-table-column
											align="center"
											prop="description"
											label="角色描述">
							</el-table-column>
							<el-table-column
											align="center"
											prop="authorities"
											label="拥有权限">
								<template slot-scope="scope">
									<span v-for="(item, index) in scope.row.authorities" :key="index">{{item.name}}<span v-show="(index + 1) !== scope.row.authorities.length">、</span></span>
								</template>
							</el-table-column>
							<el-table-column
											label="操作"
											align="center"
											width="150">
								<template slot-scope="scope">
									<el-button @click="editRole(scope.row)"  size="small">修改</el-button>
									<el-button  @click="deleteRole(scope.row)" size="small">删除</el-button>
								</template>
							</el-table-column>
						</el-table>
						<div style="text-align: center;margin-top: 20px;">
							<el-button type="primary" icon="el-icon-plus" @click="addRole">新增</el-button>
						</div>
					</div>
				</el-col>
			</el-row>
		</div>
		<roleDialog :showRoleDialog="showRoleDialog" :authorityLists="authorityLists" :roleDetails="roleDetails" ref="dialogRole" @close="closeRoleDialog"></roleDialog>
	</div>
</template>

<script>
	//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
	//例如：import 《组件名称》 from '《组件路径》';
	import roleDialog from '../components/roleDialog';
	export default {
		name: "RoleManage",
//import引入的组件需要注入到对象中才能使用
		components: {roleDialog},
		data () {
//这里存放数据
			return {
				defaultProps: {
					children: 'departments',
					label: 'name'
				},
				data: [],
				tableRoleData: [],
				showRoleDialog: false,
				roleDetails: {
					name: '',
					isLeader: '',
					description: '',
					authorities: [],
					department: ''
				},
				currentDepartment: '', // 当前
				authorityLists: [],
			};
		},
//监听属性 类似于data概念
		computed: {},
//监控data中的数据变化
		watch: {},
//方法集合
		methods: {
			postDepartmentList() {
				this.$axios.postDepartmentList().then((res) => {
					console.log(res)
					if (res.request.status === 200) {
						this.data = res.data;
					}
				})
			},
			handleNodeClick(data) {
				console.log(data);
				this.currentDepartment = data;
				this.$axios.getRoleFindDepartment(data.id).then((res) => {
					console.log(res)
					if (res.request.status === 200) {
						res.data.forEach((v, i) => {
							v.department = data.name;
						})
						this.tableRoleData = res.data;
					}
				})
			},
			addRole() {
				console.log(this.currentDepartment.id)
				this.roleDetails = {
					name: '',
					isLeader: '',
					description: '',
					authorities: [],
					departmentId: this.currentDepartment.id
				}
				this.showRoleDialog = true;
			},
			editRole(val) {
				console.log(val);
				this.showRoleDialog = true;
				this.roleDetails = val;
			},
			deleteRole(val) {
				console.log(val)
				this.$axios.deleteRole(val.id).then((res) => {
					console.log(res)
					if (res.request.status === 200) {
						this.$message({
							message: '删除角色成功!',
							type: 'success'
						});
						this.handleNodeClick(this.currentDepartment);
					}
				})
			},
			closeRoleDialog() {
				this.showRoleDialog = false;
				this.handleNodeClick(this.currentDepartment);
			},
			getAuthorityList() {
				this.$axios.putAuthority().then((res) => {
					console.log(res)
					if (res.request.status === 200) {
						this.authorityLists = res.data;
					}
				})
			},
		},
//生命周期 - 创建完成（可以访问当前this实例）
		created () {

		},
//生命周期 - 挂载完成（可以访问DOM元素）
		mounted () {
			this.postDepartmentList();
			this.getAuthorityList();
		},
		beforeCreate () {
		}, //生命周期 - 创建之前
		beforeMount () {
		}, //生命周期 - 挂载之前
		beforeUpdate () {
		}, //生命周期 - 更新之前
		updated () {
		}, //生命周期 - 更新之后
		beforeDestroy () {
		}, //生命周期 - 销毁之前
		destroyed () {
		}, //生命周期 - 销毁完成
		activated () {
		}, //如果页面有keep-alive缓存功能，这个函数会触发
	}
</script>
<style scoped>
	.roleManage{
		height: 99%;
	}

	.leftTreeTitle{
		height: 60px;
		text-align: center;
		border-bottom: 1px solid #dedede;
		line-height: 60px;
		font-size: 30px;
	}

</style>
