<template>
	<view class="user-card-box">
		<view class="user-card">
			<view class="title">
				<view class="icon">
					<u-icon name="huo" custom-prefix="iconfont" size="20" color="#aaa"></u-icon>
				</view>
				<text class="text">置顶用户</text>
			</view>
			<scroll-view class="scroll-box" scroll-x>
				<view class="user-swiper">
					
					<view @click="detail(item)" class="user-box" v-for="(item,i) in hotList">
						<view class="avatar-box">
							<u-avatar mode="square" size="140" :src="item.avatar"></u-avatar>
							<view v-if="item.onlineStatus" class="badge"></view>
							<view class="count-down">
								<u-count-down :timestamp="item.timestamp" format="HH时mm分ss秒"></u-count-down>
							</view>
						</view>
						<view class="nickname">{{item.nickname}}</view>
					</view>
					
				</view>
			</scroll-view>
		</view>
	</view>
	
</template>

<script>
	import sheep from '@/sheep';
	import { showAuthModal } from '@/sheep/hooks/useModal';
	import ImConversationApi from '@/sheep/api/im/memberConversation';
	import UserApi from '@/sheep/api/member/user';
	import { WxaSubscribeTemplate } from '@/sheep/util/const';
	export default {
		components: {
			
		},
		data() {
			return {
				hotList: [],
			}
		},
		created() {
			this.getHomeData();
		},
		methods: {
			getHomeData() {
				UserApi.getHomeData().then(res => {
					this.hotList = res.data.memberHotList;
					
					this.hotList.forEach((order) => order.timestamp = order.slashedTime ? sheep.$helper.parseTimeData(order.slashedTime) : 0);
				});
			},
			detail(e) {
				sheep.$router.go('/pages/user/detail/index',{id: e.id});
			},
			chat(e) {
				const isLogin = sheep.$store('user').isLogin;
				if(!isLogin) {
					showAuthModal();
					return;
				}
				
				const userInfo = sheep.$store('user').userInfo;
				// 如果用户已经有头像和昵称,不要每次登录都要重新上传头像。
				if(userInfo.visible) {
					// #ifdef MP-WEIXIN
					this.subscribeMessage();
					// #endif
					ImConversationApi.createMemberConversation({
						userId: e.id,
						groupType: 1,
						lastMessageContentType: 1,
					}).then(res => {
						if(res.data){
							sheep.$router.go('/pages/im/index',{groupId: res.data, receiveUserId: e.id});
						}
					});
					return;
				}
				// 触发小程序授权信息弹框
				// #ifdef MP-WEIXIN
				showAuthModal('mpAuthorization');
				// #endif
				
				// #ifndef MP-WEIXIN
				showAuthModal('h5Authorization');
				// #endif
			},
			subscribeMessage() {
			  const event = [WxaSubscribeTemplate.UNREAD_MESSAGE];
			  sheep.$platform.useProvider('wechat').subscribeMessage(event, () => {
			    // 订阅后记录一下订阅状态
			    uni.removeStorageSync(WxaSubscribeTemplate.UNREAD_MESSAGE);
			    uni.setStorageSync(WxaSubscribeTemplate.UNREAD_MESSAGE, '已订阅');
			  });
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.user-card-box {
		background-color: #fff;
		border-radius: 10px;
	}
	
	.user-card {
		padding: 10px 0;
		
		.title {
			padding: 10px;
			padding-top: 0;
			padding-left: 20px;
			font-size: 28rpx;
			font-weight: bold;
			display: flex;
			align-items: center;
			padding-bottom: 15px;
			
			.icon {
				background-color: #ddd;
				width: 36rpx;
				height: 36rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 100%;
			}
			
			.text {
				margin-left: 4px;
			}
		}
		
		.user-swiper {
			display: flex;
				
			.avatar-box {
				margin-bottom: 5px;
				position: relative;
				
				.badge {
					width: 30rpx;
					height: 30rpx;
					background-color: var(--ui-BG-Main);
					position: absolute;
					right: 0;
					bottom: 0;
					border-radius: 100%;
					border: 2px solid #fff;
				}
				
				.count-down {
					font-size: 20rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
			
			.user-box {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin-left: 10px;
				
				.nickname {
					font-size: 28rpx;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;
					max-width: 140rpx;
					text-align: center;
					line-height: 28rpx;
				}
			}
		}
		
		.user-swiper .user-box:first-child{
			margin-left: 20px;
		}
	}
	
</style>