<template>
	<view class="popupBox ">
		<view class="es-bc-white popupBox-content" v-if="userList.length>0">
			<view class="u-f es-mt-20 popupBox-content-list" v-for="(item,index) in userList" :key="index"
				@tap="loginByUserIdFun(item)">
				<view class="es-mr-20">
					<image class="es-icon-90 es-br-ban" :src="item.avatar" mode=""></image>
				</view>
				<view class="y-b" style="flex: 1;">
					<view class="es-fs-36 es-fw u-f-ac" style="display: flex; justify-content: space-between;">
						<text>{{item.nickName}}</text>
						<view class="last-login-tag" v-if="item.userId === lastLoginUserId">上次登录</view>
					</view>
					<view class="es-fs-30">{{item.userId}}</view>
				</view>
			</view>
		</view>
		<view v-else>
			<u-empty></u-empty>
		</view>
	</view>
</template>

<script>
	import {
		loginByUserId
	} from '@/api/user'
	export default {
		data() {
			return {
				userList: [],
				lastLoginUserId: ''
			}
		},
		mounted() {
			this.getRegistrationID()
			const wechatList = uni.getStorageSync('wechatList')
			if (wechatList) {
				this.userList = wechatList
				// 找出updateTime最大的用户作为上次登录用户
				if (this.userList.length > 0) {
					// 假设updateTime是可比较的字符串或时间戳
					// 如果是字符串，建议统一格式，或者转换为时间戳比较
					// 这里先简单直接比较，如果需要更复杂的转换可以在这里处理
					let maxTimeItem = this.userList[0];
					for (let i = 1; i < this.userList.length; i++) {
						const current = this.userList[i];
						// 尝试转换为时间戳比较，兼容性更好
						const maxTime = new Date(maxTimeItem.updateTime).getTime();
						const curTime = new Date(current.updateTime).getTime();
						
						if (!isNaN(curTime) && (isNaN(maxTime) || curTime > maxTime)) {
							maxTimeItem = current;
						} else if (!isNaN(curTime) && curTime === maxTime) {
							// 如果时间相同，可以根据其他逻辑处理，这里暂且保持原样
						}
					}
					if (maxTimeItem && maxTimeItem.updateTime) {
						this.lastLoginUserId = maxTimeItem.userId;
					}
				}
				uni.removeStorageSync('wechatList')
			}
		},
		methods: {
			getRegistrationID() {
				const registrationID = uni.getStorageSync("registrationID");
				if (!registrationID) {
					uni.getPushClientId({
						success: res => {
							uni.setStorageSync("registrationID", res.cid);
						}
					});
				}
			},
			async loginByUserIdFun(e) {
				const requestParam = uni.getStorageSync('requestParam')
				uni.removeStorageSync('requestParam')
				let params = {
					...requestParam,
					userId: e.userId,
					jpushId: uni.getStorageSync("registrationID")
				}
				const res = await loginByUserId(params)
				if (res.code == 200) {
					uni.setStorageSync('AppToken', res.token);
					uni.setStorageSync('liveToken', res.liveToken);
					uni.setStorageSync('userInfo', JSON.stringify(res.user));
					uni.setStorageSync('userData',JSON.stringify(res.user));
					uni.$emit('refreshIM');
					uni.$emit('showHealthButler');
					this.goPage();
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
				}
			},
			goPage() {
				let pages = getCurrentPages();
				let url = pages[pages.length - 1];
				let openUrl = uni.getStorageSync("openUrl");
				if (openUrl) {
					uni.navigateTo({
						url: openUrl,
						success: function(res) {
							uni.removeStorageSync("openUrl")
						}
					})
				} else {
					this.$updateMsgDot();
					this.$setSource();
					uni.reLaunch({
						url: '/pages_im/pages/conversation/conversationList/index',
						animationType: 'none',
						animationDuration: 2000
					});
					return;
					if (pages.length == 1) {
						uni.reLaunch({
							url: '/pages_im/pages/conversation/conversationList/index',
							//url: '../course/video/living-app',
							animationType: 'none',
							animationDuration: 2000
						});
					} else {
						//this.$navBack();
					}
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.popupBox {
		padding: 48rpx 24rpx;
		padding-top: 0rpx;
		margin-top: 24rpx;
		width: 100vw;
		box-sizing: border-box;
		flex-direction: column;

		.popupBox-content {
			padding: 24rpx;
			padding-bottom: 0;
			border-radius: 16rpx;

			.popupBox-content-list {
				padding-bottom: 24rpx;
				border-bottom: 1rpx solid #F7F8FA;

				&:last-child {
					border-bottom: none;
				}
				
				.last-login-tag {
					font-size: 24rpx;
					color: #FFFFFF;
					background-color: #2583EB;
					padding: 2rpx 10rpx;
					border-radius: 6rpx;
					margin-left: 16rpx;
					line-height: 1.5;
				}
			}
		}
	}
</style>