<script setup>
	/**
	 * 群组
	 */

	import {
		reactive,
		onMounted,
		onUnmounted,
		getCurrentInstance,
	} from 'vue';
	// 工具库
	import util from '@/common/js/util.js'
	// 腾讯云聊天
	import TencentCloudChat from '@tencentcloud/chat';
	// 
	const {
		proxy
	} = getCurrentInstance()
	// 数据列表
	const list = reactive([])

	// 右滑菜单
	const rightOption = [{
		text: '退出群聊',
		style: {
			backgroundColor: '#F85050'
		},
		fn: (item) => quitGroup(item)
	}]

	onMounted(() => {
		// 
		getGroupList()
		// 开启监听
		addListener()
	})

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

	// 开启监听
	function addListener() {
		let onFriendListUpdated = function(event) {
			getGroupList()
		}

		uni.$chat.on(TencentCloudChat.EVENT.FRIEND_LIST_UPDATED, onFriendListUpdated);
	}

	// 移除监听
	function removeListener() {
		uni.$chat.off(TencentCloudChat.EVENT.FRIEND_LIST_UPDATED);
	}

	// 获取群组列表
	function getGroupList() {
		// 验证sdk是否准备完毕
		let isReady = uni.$chat.isReady();

		if (!isReady) {
			setTimeout(function() {
				getGroupList()
			}, 200);
			return
		}

		uni.$chat.getGroupList().then(rs => {
			if (rs.code == 0) {
				list.length = 0
				list.push(...rs.data.groupList)
				console.log('group list', list)
			}
		})
	}

	/**
	 * 获取群组列表
	 * @param {Object} item
	 */
	function handleGroupItem(item) {
		console.log('group item', item)
		util.toChat({
			name: `${item.name}(${item.memberCount})`,
			msgId: item.groupID,
			type: 'GROUP',
		})
	}

	/**
	 * 菜单
	 * @param {Object} ev 默认事件
	 * @param {Object} item 单项
	 */
	function handleMenu(ev, item) {
		ev.content.fn(item)
		proxy.$refs.swipeAction.closeAll()
	}

	/**
	 * 退出群组
	 * @param {Object} item 详情
	 */
	function quitGroup(item) {
		util.alert({
			content: '确认退出群聊？'
		}).then(rs => {
			if (!rs.confirm) return
			uni.$chat.quitGroup({
				groupID: item.groupID,
			}).then(rs => {
				getGroupList()
			})
		})
	}
</script>

<template>
	<view class="group pr">
		<scroll-view scroll-y="true" class="scroll">
			<uni-swipe-action ref="swipeAction">
				<view class="list plr20 pb30">
					<view class="li" v-for="(item, index) in list" :key="index">
						<uni-swipe-action-item :right-options="rightOption" @click="handleMenu($event,item)">
							<view class="item rows ptb20" @click="handleGroupItem(item)">
								<image class="wh80 avatar br10" :src="item.avatar" mode="aspectFill" />

								<view class="f1 mlr20">
									<view class="name thd f1 c333 f32">{{item.name}}</view>
									<!-- <view class="content thd mt10 c666 f24">{{item.lastMessage.messageForShow || ''}}</view> -->
								</view>
								<!-- <view class="time c999 f22" v-if="item.lastMessage.messageForShow || ''">{{util.formatTime('yyyy-MM-dd HH:mm:ss',item.lastMessage.lastTime)}}</view> -->
							</view>
						</uni-swipe-action-item>
					</view>

					<view class="mtb20 nomore">到底啦~</view>
				</view>
			</uni-swipe-action>
		</scroll-view>
	</view>
</template>

<style lang="scss">
	.group {
		height: 100%;

		.scroll {
			height: 100%;
		}
	}
</style>