<template>
	<view class="box">
		<view class="main">
			<chat-index v-if="pageCur==='chats'"></chat-index>
			<friend-index v-if="pageCur==='friends'"></friend-index>
			<group-index v-if="pageCur==='groups'"></group-index>
			<dept-index v-if="pageCur==='dept'"></dept-index>
			<mine-index v-if="pageCur==='mine'"></mine-index>
		</view>
		<view class="cu-bar tabbar bg-white shadow foot">
			<view class="action" @click="navChange" data-cur="chats">
				<view class="iconfont icon-liaotian" :class="pageCur==='chats'?'text-cyan':'text-gray'">
					<view class="cu-tag badge" v-if="chatStore.allUnReadCount>0">{{ chatStore.allUnReadCount }}</view>
				</view>
				<view :class="pageCur==='chats'?'text-cyan':'text-gray'">聊天</view>
			</view>
			<!-- <view class="action" @click="navChange" data-cur="friends" v-if="user?.deptId=='103'">
				<view class="iconfont icon-haoyou" :class="pageCur==='friends'?'text-cyan':'text-gray'">
					<view class="cu-tag badge" v-if="friendStore.waitValidateList.length>0">
						{{ friendStore.waitValidateList.length }}
					</view>
				</view>
				<view :class="pageCur==='friends'?'text-cyan':'text-gray'">通讯录</view>
			</view>
			<view class="action" @click="navChange" data-cur="groups" v-if="user?.deptId=='103'">
				<view class="iconfont icon-qunzhong" :class="pageCur==='groups'?'text-cyan':'text-gray'">
					<view class="cu-tag badge" v-if="waitCheckGroupCount>0">{{ waitCheckGroupCount }}</view>
				</view>
				<view :class="pageCur==='groups'?'text-cyan':'text-gray'">群组</view>
			</view>
			<view class="action" @click="navChange" data-cur="dept" v-if="user?.deptId=='103'">
				<view class="iconfont icon-bumen" :class="pageCur==='dept'?'text-cyan':'text-gray'"></view>
				<view :class="pageCur==='dept'?'text-cyan':'text-gray'">组织</view>
			</view>
			<view class="action" @click="navChange" data-cur="mine" v-if="user?.deptId=='103'">
				<view class="iconfont icon-wode" :class="pageCur==='mine'?'text-cyan':'text-gray'"></view>
				<view :class="pageCur==='mine'?'text-cyan':'text-gray'">我的</view>
			</view> -->
		</view>

	</view>
</template>

<script setup lang="ts">
	import ChatType from '@/utils/ChatType';
	import type Message from '@/mode/Message'
	import Auth from "@/api/Auth";
	import { computed, getCurrentInstance, onMounted, ref } from 'vue';
	import ChatIndex from '@/pages/chat/index.vue'
	import FriendIndex from '@/pages/friend/index.vue'
	import GroupIndex from '@/pages/group/index.vue'
	import MineIndex from '@/pages/mine/index.vue'
	import DeptIndex from '@/pages/dept/index.vue'
	import { useChatStore } from '@/store/chatStore';
	import { useFriendStore } from '@/store/friendStore';
	import { useUserStore } from '@/store/userStore';
	import UserApi from '@/api/UserApi';
	import { onBackPress } from '@dcloudio/uni-app';
	import { useWsStore } from "@/store/WsStore";
	import { useImmunityStore } from '@/store/immunityStore'
	import { useSettingStore } from '@/store/settingStore'
	import { useGroupStore } from "@/store/groupStore";
	import MessageUtils from "@/utils/MessageUtils";
	import { onLoad } from '@dcloudio/uni-app';
	//@ts-ignore
	const { proxy } = getCurrentInstance();
	const chatStore = useChatStore();
	const userStore = useUserStore();
	const friendStore = useFriendStore();
	const groupStore = useGroupStore();
	const wsStore = useWsStore();
	const pageCur = ref('chats');
	const backCount = ref(0);
	const navChange = (e : any) => {
		pageCur.value = e.currentTarget.dataset.cur
	}

	const waitCheckGroupCount = computed(() => {
		const arr = [...groupStore.waitCheckMap.values()];
		if (arr && arr.length === 0) {
			return 0;
		}
		return arr.reduce((prev, curr) => {
			return prev + curr;
		})
	})

	onBackPress(() => {
		backCount.value++;
		if (backCount.value > 1) {
			return false;
		} else {
			MessageUtils.message('再按一次返回登录界面')
			return true;
		}
		setTimeout(() => {
			backCount.value = 0;
		}, 2000)
	})
	//重置打开的聊天id,说明没有打开聊天窗口
	chatStore.setOpenChatId(null);
	
	onMounted(() => {
		UserApi.currentUser()
			.then((res) => {
				if (res && res.data) {
					userStore.setUser(res.data);
					//加载免打扰
					useImmunityStore().loadData()
					//加载设置
					useSettingStore().loadData()
					//加载聊天室
					useChatStore().reloadChats()
						.then(() => {
							//初始化websocket
							if (wsStore.wsRequest.socket?.readyState === 1) {
								wsStore.checkStatus()
							} else {
								wsStore.close(true)
								wsStore.init()
							}
						})
				}
			})
	})

	//当前用户
	const user = userStore.getUser()
</script>

<style scoped lang="scss">
	.action {
		.iconfont {
			font-size: 44upx;
			line-height: 1.2;
		}
	}

	.box {
		flex-direction: column;
		height: 100%;
		display: flex;
	}

	.main {
		flex: 1;
		background-color: #f8f8f8;
	}

	.icon-liaotian {
		position: relative;
	}

	.cu-tag.badge {
		right: 20%;
	}
</style>