<template>
	<view>
		<view class="back">上一级</view>
		<view>{{nowRouter}}</view>
		<view class="deptWrap">
			<view v-for="item in subDept" :key="item.id" @click="toSub(item)">
				<image style="width: 50upx;height: 50upx;"></image>
				<text>{{item.name}}</text>
			</view>
			<view v-for="user in userList" :key="user.id">{{user.name}}</view>
		</view>
	</view>
</template>

<script>
	var that;
	export default {
		name: "emplyeeIndex",
		data() {
			return {
				depRange: [],
				subDept:[],//部门
				userList:[],//用户
				nowRouter:''
			}
		},
		
		onShow() {
			this.getdept();
		},
		methods: {
			//获取dept
			getdept() {
				let that = this;
				that.$u.api.deptList({page: 0}).then(res => {
					that.subDept = that.depRange = that.redata(res);
					debugger;
					console.log(that.depRange);
					debugger;
				})
			},
			toSub(item){//部门id
			    this.nowRouter = item.name;
				if(item.children){
					debugger;
					console.log(item)
					this.subDept = item.children;
					this.getUseList(item.id);
				}
			},
			//获取用户
			getUseList(id) {
				this.$u.api.userList({page: 0,belong_dept:id}).then(res => {
					debugger;
					this.userList = res
				})
			},
			

			redata(postList) {
				let posts = [];
				postList.forEach((item) => {
					let obj = new Object();
					obj = {
						...item
					};
					obj.value = item.id;
					obj.text = item.name;
					obj.parentId = item.parent;
					posts.push(obj);
				});
				let obj = posts.reduce((res, v) => ((res[v.id] = v), res), {}); //Object
				let arr = [];
				for (let item of posts) {
					if (item.parentId == null) {
						arr.push(item);
						continue;
					}
					let parent = obj[item.parentId];
					parent.children = parent.children ? parent.children : [];
					parent.children.push(item);
				}
				console.log(arr);
				return arr;
			},
			onnodeclick(e) {
				console.log(e);
			},
			onpopupopened(e) {
				console.log('popupopened');
			},
			onpopupclosed(e) {
				console.log('popupclosed');
			},
			onchange(e) {
				console.log('onchange:', e);
			},
		}
	}
</script>

<style>
</style>