<template>
	<view>
		<view style="border-top: 2rpx solid #D4D4D4;position: fixed;top: 0;left: 0;right: 0;"></view>
		<chat id="chat" :username="username" ref="chat" chatType="singleChat" @onClickInviteMsg="onClickMsg"></chat>
	</view>
</template>

<script>
	let disp = require("../../hxChatSDK/utils/broadcast");
	let req = require("../../utils/request.js");
	import chat from "../../chat/components/chat/chat.vue";
	var WebIM = require("../../hxChatSDK/utils/WebIM")["default"];

	export default {
		data() {
			return {
				username: {
					your: ""
				}
			};
		},

		components: {
			chat
		},
		props: {},
		// options = 系统传入的 url 参数
		onLoad(options) {
			console.log('options>>>', options)
			let username = JSON.parse(options.username);
			this.setData({
				username: username
			});
			// 生成的支付宝小程序在onLoad里获取不到，这里放到全局变量下
			uni.username = username;

			var userNameText = username.your;
			const imUserInfo = uni.getStorageSync("imUserInfo_" + username.your);
			if (imUserInfo && imUserInfo.nickName) {
				userNameText = imUserInfo.nickName;
			}
			uni.setNavigationBarTitle({
				title: userNameText
			});
			// this.subscribePresence();
		},
		onShow() {
			req.onNetworkStatusChangeIM()
		},
		onUnload(options) {
			disp.fire("em.chatroom.leave");
		},

		onPullDownRefresh: function() {
			uni.showNavigationBarLoading();
			this.$refs.chat.getMore();
			// 停止下拉动作
			uni.hideNavigationBarLoading();
			uni.stopPullDownRefresh();
		},
		methods: {

			/**
			 * 需要到环信开通功能才能生效（增值功能）
			 */
			subscribePresence() {
				let option = {
					usernames: [this.username.your],
					expiry: 7 * 24 * 3600 // 单位为秒
				}
				WebIM.conn.subscribePresence(option).then(res => {
					console.log('subscribePresence>>>>>', res)
				})

				WebIM.conn.addEventHandler('MESSAGES', {
					onPresenceStatusChange: function(msg) {
						// 这里可以处理订阅用户状态更新后的逻辑。
						console.log('状态更新', msg);
					},
				})
			},


			onClickMsg(msg) {
				msg.action = 'join'
				uni.navigateTo({
					url: "../emedia/index?srcData=" + JSON.stringify(msg)
				});
			},
			onNavigationBarButtonTap(e) {
				uni.navigateTo({
					url: `/pages/moreMenu/moreMenu?username=${this.username.your}&type=singleChat`
				})
			},
		}
	};
</script>
<style>
	@import "./chatroom.css";
</style>