<template>
	<view class="friend_list_container">
		<view class="header">
			<uni-nav-bar 
				right-icon="plus"
				@clickRight="contactAddClick"
				:border="false"
				title="通讯录"
				statusBar
				fixed
				backgroundColor="transparent"
				>
			</uni-nav-bar>
			<SearchbarPlace @click="toSearch">搜索</SearchbarPlace>
		</view>
		<uni-list class="contact_menus">
			<uni-list-item title="新的好友" :showBadge="storeUnHandleFriendApplicationNum>0" :badgeText="storeUnHandleFriendApplicationNum+''" badgeType="error" thumbSize="lg" to="/pages/contact/applicationList/index?applicationType=NewFriend" thumb="/static/images/contact_new_friend.png"></uni-list-item>
<!-- 			<uni-list-item title="新的群组" thumbSize="lg" to="/pages/contact/applicationList/index?applicationType=NewGroup" thumb="/static/images/contact_new_group.png"></uni-list-item> -->
			<uni-list-item title="群聊" thumbSize="lg" to="/pages/contact/groupList/index" thumb="/static/images/contact_my_group.png"></uni-list-item>
		</uni-list>

		<choose-index-list v-if="getIndexData.dataList.length > 0" @itemClick="userClick" :height="`${listHeight}px`"
			:indexList="getIndexData.indexList" :itemArr="getIndexData.dataList" />
		<u-empty v-else mode="list" />
	</view>
</template>

<script>
	import {mapGetters,mapActions} from "vuex";
	import {formatChooseData} from "@/util/common";
	import CustomNavBar from "@/components/CustomNavBar/index.vue";
	import SearchbarPlace from "@/components/searchbar_place.vue";
	import ChooseIndexList from "@/components/ChooseIndexList/index.vue";
	import ContactMenus from "../index/components/ContactMenus.vue";
	export default {
		components: {
			CustomNavBar,
			ChooseIndexList,
			ContactMenus,
			SearchbarPlace
		},
		data() {
			return {
				keyword: "",
				listHeight: 0,
			};
		},
		computed: {
			...mapGetters(["storeFriendList",'storeUnHandleFriendApplicationNum']),
			getIndexData() {
				return formatChooseData(this.storeFriendList);
			},
		},
		mounted() {
			this.getListHeight();
		},
		async onShow() {
			//await this.getFriendList();
		},
		methods: {
			...mapActions('contact',['getFriendList']),
			toSearch(){
				uni.navigateTo({
					url:"/pages/common/search/index?type=friend"
				})
			},
			contactAddClick() {
				uni.navigateTo({
					url: "/pages/contact/contactAdd/index",
				});
			},
			goto(url){
				uni.navigateTo({
					url: url,
				});
			},
			userClick(friend) {
				uni.navigateTo({
					url: `/pages/common/userCard/index?sourceID=${friend.userID}`,
				});
			},
			async getListHeight() {
				const windowInfo = uni.getWindowInfo();
				//const data = await this.getEl(".search_bar_wrap");
				const contact_menus = await this.getEl(".contact_menus");
				const header = await this.getEl(".header");
				
				//const searchBarHeight = Number(data.height.toFixed());
				const contact_menusHeight = contact_menusHeight ? Number(contact_menus.height.toFixed()) : 0;
				const headerBarHeight = header ? Number(header.height.toFixed()) : 0;
				this.listHeight =
					windowInfo.windowHeight -
					windowInfo.statusBarHeight -
					44 -
					contact_menusHeight -
					headerBarHeight;
			},
			getEl(el) {
				return new Promise((resolve) => {
					const query = uni.createSelectorQuery().in(this);
					query
						.select(el)
						.boundingClientRect((data) => {
							// 存在data，且存在宽和高，视为渲染完毕
							resolve(data);
						})
						.exec();
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.friend_list_container {
		.header{
			background-color: #f4f4f4;
		}
		.search_bar_wrap {
			height: 34px;
			padding: 12px 22px;
		}

		.u-empty {
			margin-top: 25vh !important;
		}
		.contact_menus{
			::v-deep .uni-list-item__container{
				padding: 24rpx 44rpx;
			}
		}
	}
</style>