<template>
	<view class="main">
		<cu-custom bgColor="bg-blue">
			<block slot="content">通讯录</block>
		</cu-custom>
		<view class="tree">
			<ly-tree :tree-data="officeList" :props="props" node-key="id" :checkOnClickNode="true" :showRadio="false"
				:show-checkbox="false" :checkOnlyLeaf="false" ref="officeTree" />
		</view>
		<u-overlay :show="loading">
			<view class="warp">
				<view class="rect"><u-button plain loading loadingText="加载中"></u-button></view>
			</view>
		</u-overlay>
	</view>
</template>

<script>
	import userService from '@/api/sys/userService'
	import officeService from "@/api/sys/officeService"
	export default {
		data() {
			return {
				loading:true,
				indexList: [],
				officeList: [],
				userList: [],
				total: 0,
				searchUserName: ''
			}
		},
		props: {
			props: {
				type: Object,
				default: () => {
					return {
						children: 'children',
						label: 'name'
					}
				}
			}
		},
		created() {
			officeService.treeData().then(data => {
				// this.officeList = data
				userService.list({
					current: 1,
					size: 10000
				}).then((res) => {
					this.userList = res.records
					this.officeList = this.forItem(data)
					this.total = res.total
					this.loading=false
				}).catch((e) => {
					throw e
				})

			})
			userService.list({
				current: 1,
				size: 10000
			}).then((data) => {
				this.userList = data.records
				this.total = data.total
			}).catch((e) => {
				throw e
			})
		},
		// computed: {
		// 	list () {
		// 		let resultList = this.userList.filter((item)=>{
		// 			if(item.name.indexOf(this.searchUserName) >= 0){
		// 				return true
		// 			}
		// 		})
		// 		return this.pySegSort(resultList)
		// 	}
		// },
		methods: {
			// 排序
			pySegSort(arr) {
				if (!String.prototype.localeCompare)
					return null;

				var letters = "0abcdefghjklmnopqrstwxyz".split('');
				var zh = "阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀".split('');

				var segs = [];
				var curr;
				letters.forEach((item, i) => {
					curr = {
						letter: item,
						data: []
					};
					arr.forEach((item2) => {
						if ((!zh[i - 1] || zh[i - 1].localeCompare(item2.name) <= 0) && item2.name
							.localeCompare(zh[i]) == -1) {
							curr.data.push(item2);
						}
					});
					if (curr.data.length) {
						segs.push(curr);
						this.indexList.push(curr.letter)
						curr.data.sort((a, b) => {
							return a.name.localeCompare(b.name);
						});
					}
				});
				return segs;
			},
			// 遍历
			forItem(list) {
				for (let i = 0; i < list.length; i++) {
					let b = []
					if(list[i].icon) continue;
					b = this.userList.filter(item => item.officeDTO && item.officeDTO.id == list[i].id)
					if (b.length > 0) {
						b.forEach(item => {
							this.$set(item, "icon", "/static/img/flat-avatar.png");
						})
						if (list[i].children) {
							list[i].children = list[i].children.concat(b);
							this.forItem(list[i].children)
						} else {
							this.$set(list[i], "children", b);
						}
					} else {
						if (list[i].children && list[i].children.length > 0) {
							this.forItem(list[i].children)
						} else {
							let no = [{
								id: "no001",
								name: '暂无人员'
							}]
							this.$set(list[i], "children", no);
						}
					}
				}
				return list;
			},
		}
	}
</script>

<style lang="scss">
	.warp {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;
	}
	.list {

		&__item {
			@include flex;
			padding: 6px 12px;
			align-items: center;

			&__avatar {
				height: 35px;
				width: 35px;
				border-radius: 3px;
			}

			&__user-name {
				font-size: 16px;
				margin-left: 10px;
				color: $u-main-color;
			}
		}

		&__footer {
			color: $u-tips-color;
			font-size: 14px;
			text-align: center;
			margin: 15px 0;
		}
	}
</style>