<template>
	<view class="contacts-v">
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :sticky="true"
			:down="downOption" :up="upOption" :bottombar="false">
			<view class="search-box search-box_sticky">
				<u-search :placeholder="$t('app.apply.pleaseKeyword')" v-model="keyword" height="72"
					:show-action="false" @change="search" bg-color="#f0f2f6" shape="square">
				</u-search>
				<view class="organization u-border-bottom">{{keyword?'联系人':'组织列表'}}</view>
			</view>
			<view class="list-cell u-p-l-20 u-p-r-20" v-if="!keyword">
				<scroll-view :scroll-y="true" style="height: 100%">
					<ly-tree :tree-data="list" :node-key="defaultProps.value" :props="defaultProps" 
						:load="loadNode" lazy :default-expand-all='false' :show-node-icon="true"
						:defaultExpandedKeys="defaultExpandedKeys" child-visible-for-filter-node
						@node-click="handleNodeClick" ref="tree">
					</ly-tree>
				</scroll-view>
			</view>
			<view class="list-cell u-p-l-20 u-p-r-20" v-for="(item, i) in list" :key="i" @click="detail(item)" v-else>
				<view class="u-border-bottom list-item u-font-28 u-flex" v-if="list.length">
					<u-avatar :src="baseURL+item.headIcon"></u-avatar>
					<view class="list-cell-txt">
						<view class="u-font-30 u-m-b-4" style="color: #303133;font-size: 28rpx;">
							{{item.realName}}/{{item.account}}
						</view>
						<view class="u-font-24 department u-m-t-4">{{item.department}}</view>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import {
		getOrgAndPosSelector
	} from '@/api/message.js'
	import {
		getUserList
	} from '@/api/common.js'
	import resources from '@/libs/resources.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				downOption: {
					use: true,
					auto: true
				},
				upOption: {
					page: {
						num: 0,
						size: 20,
						time: null
					},
					empty: {
						use: true,
						icon: resources.message.nodata,
						tip: this.$t('common.noData'),
						fixed: true,
						top: "300rpx",
					},
					textNoMore: this.$t('app.apply.noMoreData'),
				},
				keyword: '',
				list: [],
				defaultProps: {
					label: 'fullName',
					value: 'id',
					icon: 'icon',
					children: 'children',
					isLeaf: 'isLeaf'
				},
				level: 0,
			}
		},
		computed: {
			baseURL() {
				return this.define.baseURL
			},
			defaultExpandedKeys() {
				return this.list.length ? [this.list[0].id] : []
			}
		},
		methods: {
			upCallback(page) {
				let query = this.keyword ? {
					currentPage: page.num,
					pageSize: page.size,
					keyword: this.keyword,
					organizeId: '',
					organizeName: '',
					positionId: ''
				} : {
					id: '0',
					type: 'organize'
				};

				let method = this.keyword ? getUserList(query) : getOrgAndPosSelector(query);
				method.then(res => {
					const list = res.data.list || [];
					if (!this.keyword) {
						this.list = list;
						return this.mescroll.endSuccess(true);
					}
					this.mescroll.endSuccess(list.length);
					if (page.num === 1) this.list = [];
					this.list = this.list.concat(list);

				}).catch(() => {
					this.mescroll.endErr();
				});
			},
			loadNode(node, resolve) {
				let query = {
					id: node.key || 0,
					type: node.data.type || 'organize'
				}
				getOrgAndPosSelector(query).then(res => {
					resolve(res.data.list)
				})
			},
			search() {
				// 节流,避免输入过快多次请求
				this.searchTimer && clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					this.list = [];
					this.mescroll.resetUpScroll();
				}, 300)
			},
			handleNodeClick(data) {
				const type = data.data.type
				const config = {
					organizeId: type === 'organize' ? data.key : data.data?.organizeId,
					organizeName: data.label,
					...(type === 'position' ? {
						positionId: data.key
					} : {})
				};
				uni.navigateTo({
					url: '/pages/my/contacts/userList?config=' + encodeURIComponent(JSON.stringify(
						config)),
				})
			},
			detail(item) {
				uni.navigateTo({
					url: '/pages/my/userDetail/index?data=' + encodeURIComponent(JSON.stringify(
						item))
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #eef0f4;
	}

	.contacts-v {
		::v-deep .ly-tree-node__icon {
			width: 1.5rem;
			height: 1.5rem;
			overflow: hidden;
			margin-right: 0.5rem;
			background: #43a1f3;
			border-radius: 50%;
			text-align: center;
			line-height: 1.5rem;
			color: #fff;
			font-size: 24rpx;
		}

		.organization {
			width: 100%;
			background-color: #fff;
			padding: 20rpx;
			font-size: 32rpx;
		}

		.list-cell {
			width: 100%;
			background-color: #fff;

			.list-item {
				box-sizing: border-box;
				overflow: hidden;
				color: $u-content-color;
				height: 136rpx;

				.list-cell-txt {
					margin-left: 20rpx;

					.department {
						color: #909399;
						font-size: 24rpx;
					}
				}
			}
		}
	}
</style>