<template>
	<el-container class="mainBox ">
		<el-aside width="200px" v-loading="showGroupLoading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" :size="size" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="id" :data="group" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
				<el-header>
					<div class="left-panel">
						<el-button type="primary" :size="size" icon="el-icon-plus" @click="add">新增用户</el-button>
						<el-button type="danger" plain :size="size" icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
						<el-button type="primary" plain :size="size" :disabled="selection.length==0" @click="role">分配角色</el-button>
						<el-button type="primary" plain :size="size" :disabled="selection.length==0" @click="rePwd">密码重置</el-button>
					</div>
					<div class="right-panel">
						<div class="right-panel-search">
							<el-input v-model="params.name" :size="size" placeholder="关键字" clearable></el-input>
							<el-button type="primary" :size="size" icon="el-icon-search" @click="upSearch"></el-button>
						</div>
					</div>
				</el-header>
				<el-main class="nopadding">
					<scTable ref="table" :apiObj="list.apiObj" :column="list.column" @selection-change="selectionChange" stripe :size="size">
						<el-table-column type="selection" align="center" width="45"></el-table-column>
						<el-table-column label="序号" type="index" align="center" width="50"></el-table-column>
						<template #avatar="scope">
							<div class="userImg">
								<el-avatar :src="scope.row.avatar" size="small"></el-avatar>
							</div>
						</template>
						<template #roles="scope">
							<span v-if="scope.row.user_roles">
								<span v-for="(em,index) in scope.row.user_roles.role_names" :key="index">
									<span>{{em}}</span><span v-if="index+1<scope.row.user_roles.role_names.length">、</span>
								</span>
							</span>
						</template>
						<template #active_status="scope">
							<el-switch :size="size" v-model="scope.row.active_status" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="true" :inactive-value="false"></el-switch>
						</template>
						<el-table-column label="操作" fixed="right" align="center" width="160">
							<template #default="scope">
								<el-dropdown>
									<el-button class="noBorderBtn" icon="el-icon-more" :size="size"></el-button>
									<template #dropdown>
										<el-dropdown-menu>
											<el-dropdown-item @click="table_show(scope.row, 'show')" icon="sc-icon-See">用户详情</el-dropdown-item>
											<el-dropdown-item @click="table_edit(scope.row, 'edit')" icon="sc-icon-Edit">编辑用户</el-dropdown-item>
											<el-dropdown-item @click="table_empower(scope.row)" icon="sc-icon-AbilityAuthorization" divided>查看权限</el-dropdown-item>
											<el-dropdown-item @click="table_del(scope.row, 'delete')" icon="sc-icon-Delete">删除用户</el-dropdown-item>
										</el-dropdown-menu>
									</template>
								</el-dropdown>
							</template>
						</el-table-column>

					</scTable>
				</el-main>
		</el-container>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	<re-pwd-dialog v-if="dialog.rePwdD" ref="rePwdDialog" @success="handleSuccess" @closed="dialog.rePwdD=false"></re-pwd-dialog>
	<dis-role-dialog v-if="dialog.role" ref="diRoleDialog" @success="handleSuccess" @closed="dialog.role=false"></dis-role-dialog>

</template>

<script>
	import saveDialog from './save'
	import rePwdDialog from './rePwd'
	import disRoleDialog from './role'

	export default {
		name: 'user',
		components: {
			saveDialog, rePwdDialog, disRoleDialog
		},
		data() {
			return {
				size:'small',
				dialog: {
					save: false,
					rePwdD: false,
					role: false
				},
				showGroupLoading: false,
				groupFilterText: '',
				group: [],
				list:{
					apiObj: this.$API.system.user.list,
					column:[]
				},
				selection: [],
				params: {
					name: null
				}
			}
		},
		watch: {
			groupFilterText(val) {
				this.$refs.group.filter(val);
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			rePwd() {
				this.dialog.rePwdD = true
				this.$nextTick(() => {
					const id = this.selection[0]['id']
					this.$refs.rePwdDialog.open('add', id)
				})
			},
			role() {
				this.dialog.role = true
				this.$nextTick(() => {
					const data = this.selection;
					const row = {
						id: data[0]['id'],
						login_name: data[0]['login_name'],
						name: data[0]['name'],
						group: data[0]['user_roles'],
						mobile: data[0]['mobile'],
						avatar:data[0]['avatar']
					}
					this.$refs.diRoleDialog.open().setData(row)
				})
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row){
				const reqData = {ids: [row.id]};
				const res = await this.$API.system.user.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh();
					this.$message.success("删除成功")
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(async () => {
					const loading = this.$loading();
					const params = {ids: this.selection.map(em => em.id)};
					const res = await this.$API.system.user.delete.post(params);
					if (res.code == 200) {
						this.$refs.table.refresh();
						this.$message.success("删除成功");
					}
					loading.close();
				}).catch(() => {

				})
			},
			// 查看用户权限
			table_empower(row){
				this.$router.push({
					path: '/setting/user/view-permission',
					query: {
						id: row.id,
						name:row.name,
					}
				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//加载树数据
			async getGroup(){
				this.showGroupLoading = true;
				const res = await this.$API.system.dept.active.post();
				if(res.code == 200){
					const allNode = {id: '', label: '所有'};
					res.data.unshift(allNode);
					this.group = res.data;
				}
				this.showGroupLoading = false;
			},
			//树过滤
			groupFilterNode(value, data){
				if (!value) return true;
				return data.label.indexOf(value) !== -1;
			},
			//树点击事件
			groupClick(data){
				const params = {
					groupId: data.id
				};
				this.$refs.table.reload(params)
			},
			// 状态
			changeSwitch(val, row) {
				row.$switch_yx = true;
				setTimeout(async () => {
					let params = {
						id: row.id,
						status: row.active_status,
					};
					const res = await this.$API.system.user.status.post(params);
					if(res.code !=200){
						row.active_status = !row.active_status;
					}
					delete row.$switch_status;
					delete row.$switch_yx;
				}, 500);
			},
			//搜索
			upSearch(){
				this.$refs.table.upData(this.params)
			},
			//本地更新数据
			handleSuccess(data, mode){
				if(mode=='add'){
					this.$refs.table.refresh();
				}else if(mode=='edit'){
					this.$refs.table.tableData.filter(item => item.id===data.id ).forEach(item => {
						Object.assign(item, data)
					})
				}
				this.$refs.table.refresh();
			}
		}
	}
</script>

<style>
</style>
