<script setup>
	// 消息

	import {
		ref,
		reactive,
		computed,
	} from 'vue'
	import {
		useStore,
	} from 'vuex'
	import {
		onLoad,
		onUnload,
	} from '@dcloudio/uni-app'
	// 腾讯云聊天
	import TencentCloudChat from '@tencentcloud/chat';

	// 头部导航栏
	import apex from '@/components/header/apex'
	// 未登录
	import noLogin from '@/components/login/noLogin.vue'
	// 未实名认证
	import noAuth from '@/components/mine/noAuth.vue'
	// 通讯录
	import book from '@/components/news/book'
	// 群聊列表
	import groupList from '@/components/news/groupList'
	// 消息列表
	import msgList from '@/components/news/msgList'
	// 底部菜单
	import footerMneu from '@/components/footerMenu/footerMenu'
	// 工具库
	import util from '@/common/js/util.js'
	// vuex
	const store = useStore()
	// 菜单列表
	const menuList = reactive([{
			key: 'group',
			name: '即时消息',
			load: false,
		}, {
			key: 'friend',
			name: '通讯录',
			load: false,
		},
		{
			key: 'video',
			name: '视讯消息',
			load: false,
		},
		{
			key: 'store',
			name: '商城消息',
			load: false,
		},
	])
	// 菜单下标
	const menuIndex = ref('')
	// 是否显示搜索菜单
	const showSearch = ref(false)
	// im加载
	const imLoad = ref(false)
	// 用户信息
	const userinfo = computed(() => {
		return store.state.userinfo
	})
	// 当前选中的菜单
	const menuCurrent = computed(() => {
		return menuList[menuIndex.value]
	})
	const footerMneuRef = ref()

	onLoad(() => {
		// 初始化菜单
		handleMenuIndex(0)

		// 开启监听
		addListener()

		// 更新未读数量
		uni.$on('updateNum', (val) => {
			getNoRead()
		})
	})

	onUnload(() => {
		// 移除监听
		removeListener()
	})

	function getNoRead() {
		setTimeout(() => {
			footerMneuRef.value.getReadNum();
		}, 200)
	}

	// 开启监听
	function addListener() {
		uni.$chat.on(TencentCloudChat.EVENT.SDK_READY, imLoading);
	}

	// im加载完成
	function imLoading() {
		imLoad.value = true
	}

	// 移除监听
	function removeListener() {
		// #ifdef APP
		uni.$chat.off(TencentCloudChat.EVENT.SDK_READY, () => {});
		// #endif
	}

	/**
	 * 切换下标
	 * @param {Number} index
	 */
	function handleMenuIndex(index) {
		if (menuIndex.value === index) return
		menuList.forEach(item => {
			item.load = false;
		});
		menuList[index].load = true;
		menuIndex.value = index
	}

	// 打开菜单
	function showActionSheet() {
		uni.showActionSheet({
			itemList: ['扫一扫', '添加好友', '发起群聊', '我的二维码'],
			success: rs => {
				switch (rs.tapIndex) {
					case 0:
						util.scan()
						break;
					case 1:
						uni.navigateTo({
							// url: '/pages/news/addFriend'
							url: '/pages/news/newFriend'
						});
						break;
					case 2:
						uni.navigateTo({
							url: '/pages/news/group/create'
						});
						break;
					case 3:
						uni.navigateTo({
							url: '/pages/news/myQr'
						});
						break;
					default:
						break;
				}
			}
		})
	}

	function getUserInfos(userRecommend) {
		videoApi.getUserInfo({
			query: {
				userRecommend: userRecommend,
			}
		}).then(rs => {
			if (rs.data !== null) {
				uni.navigateTo({
					url: '/pages/index/beInvited?header=' + rs.data.userPortrait + '&userId=' + rs.data
						.userId + '&userNickname=' +
						rs.data.userNickname
				});
			}
		})
	}
</script>

<template>
	<view class="page">
		<!-- 未登录 -->
		<template v-if="!userinfo.id">
			<noLogin class="f1" />
		</template>

		<!-- 未实名 -->
		<template class="page" v-else-if="!userinfo.isRealName">
			<noAuth class="f1" />
		</template>

		<template class="page" v-else>
			<apex>
				<template #left>
					<view></view>
				</template>
				<template #content>
					<!-- <view class="ver">
					<image class="wh30" src="/static/read.png" mode="aspectFit" />
					<text class="f20">一键已读</text>
				</view> -->
				</template>
				<template #right>
					<uni-icons type="bars" size="40rpx" @click="showActionSheet" />
				</template>
			</apex>

			<view class="searchBox" :class="{ 'active': showSearch }">
				<view class="search rows mt20 mlr20 ptb10 plr30 bfff bar">
					<uni-icons type="search" />
					<input type="text" placeholder="搜索" class="f1 ml10" confirm-type="search" />
				</view>
			</view>

			<!-- 顶部菜单 -->
			<view class="headMenu f24">
				<view class="option oh df" v-for="(item, index) in menuList" :key="item.key"
					:class="[item.key, { 'active': index === menuIndex }]" @click="handleMenuIndex(index)">
					<view class="f1 fmid">{{ item.name }}</view>
				</view>
			</view>

			<!-- 轮播图 -->
			<swiper class="swiper" :current="menuIndex" disable-touch="true">
				<!-- 群组 -->
				<swiper-item>
					<msgList v-if="menuList[0].load" :type="menuIndex" />
					<view class="loading" v-else>正在加载</view>
				</swiper-item>

				<!-- 通讯录 -->
				<swiper-item>
					<book v-if="menuList[1].load" />
					<view class="loading" v-else>正在加载</view>
				</swiper-item>

				<!-- 视频消息 -->
				<swiper-item>
					<msgList v-if="menuList[2].load" :type="menuIndex" />
					<view class="loading" v-else>正在加载</view>
				</swiper-item>

				<!-- 商城消息 -->
				<swiper-item>
					<msgList v-if="menuList[3].load" :type="menuIndex" />
				</swiper-item>
			</swiper>

			<!-- 底部导航 -->
			<footerMenu ref="footerMneuRef" page="news" />
		</template>
	</view>
</template>

<style lang="scss" scoped>
	//
	.headMenu {
		display: grid;
		grid-template-columns: repeat(4, 1fr);
		grid-gap: 25rpx;
		margin: 20rpx;

		// 单项
		.option {
			height: 100rpx;
			background-color: #E2E2E2;
			border-radius: 15rpx;
			transition: .3s;

			// 激活的
			&.active {
				color: #fff;

				// 朋友
				&.friend,
				&.group,
				&.video {
					background-image: linear-gradient(126deg, #27EFE2 0%, #A45EFF 43%, #FF004F 100%);
				}

				// 商城消息
				&.store {
					background-image: linear-gradient(270deg, #FF9B27 20%, #FDC123 103%);
				}
			}
		}

		// 卡通
		.cartoon {
			margin-top: -15rpx;
			margin-right: -25rpx;
			width: 194rpx;
			height: 175rpx;
			transform: rotate(-5deg);
		}
	}

	// 搜索条
	.searchBox {
		overflow: hidden;
		height: 0;
		transition: .3s;

		&.active {
			height: 80rpx;
		}
	}

	// 容器
	.swiper {
		flex: 1;
		width: 100%;
	}

	// 正在加载
	.loading {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100%;
	}
</style>