<template>
	<view class="message">
		<mescroll-empty :option="option" v-if="chatList.length === 0" @emptyclick="$navigateTo('/pages/login/login')"
			:key="reloadMecroll">
		</mescroll-empty>
		<view class="message-list" v-else>
			<!-- 消息提示 -->
			<view class="message-item" v-for="(item,index) in chatList" :key="index" @click="linkTo(item)">
				<uni-badge class="uni-badge-left-margin" :text="item.unread" absolute="rightTop" size="small">
					<image :src="item.userInfo.avatarUrl" class="message-image" mode="aspectFill">
					</image>
				</uni-badge>
				<view class="message-content">
					<view class="message-title">{{item.userInfo.nickname}}</view>
					<view class="message-sub-content">
						{{item.lastMessage.type === 'image'?'[图片]':item.lastMessage.body.text}}
					</view>
				</view>
				<view class="message-date">
					<uni-dateformat class="visitor-time" :date="item.updatedAt" :threshold="[60000,3600000 * 24 * 365]">
					</uni-dateformat>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		YeIMUniSDK,
		YeIMUniSDKDefines
	} from '@/uni_modules/wzJun1-YeIM-Uni-SDK/js_sdk/yeim-uni-sdk.min.js'
	export default {
		data() {
			return {
				chatList: [],
				option: {
					tip: '暂无消息'
				},
				reloadMecroll: false
			}
		},
		onLoad() {
			if (uni.getStorageSync('token')) {
				this.$chat.getConversationList(res => {
					if (res.code == 200) {
						this.chatList = res.data;
					}
				})
			}
		},
		onShow() {
			if (!uni.getStorageSync('token')) {
				this.chatList = [];
				this.option = {
					tip: '暂未登录',
					btnText: '点击登录'
				}
			} else {
				this.option = {
					tip: '暂无消息'
				}
				uni.$on('changeChatList', (res) => {
					this.chatList = res;
				})
			}
			this.reloadMecroll = !this.reloadMecroll;
		},
		methods: {
			linkTo(item) {
				this.$chat.clearConversationUnread(item.conversationId);
				this.$navigateTo('/pages/chat/chat?userId=' + item.conversationId + '&userName=' + item.userInfo.nickname)
			}
		}
	}
</script>

<style lang="scss">
	.message {
		.message-list {
			margin-top: 20rpx;

			.message-item {
				background: #fff;
				display: flex;
				padding: 24rpx 30rpx;
				position: relative;

				&::before {
					content: '';
					position: absolute;
					bottom: 0;
					right: 0;
					left: 150rpx;
					height: 2rpx;
					background: $uni-border-1;
				}
			}

			.message-image {
				width: 100rpx;
				height: 100rpx;
				border-radius: 16rpx;
				overflow: hidden;
			}

			.message-content {
				flex: 1;
				width: 0;
				margin-left: 20rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
			}

			.message-title {
				font-size: 32rpx;
				font-weight: 600;
				margin-bottom: 10rpx;
			}

			.message-sub-content {
				font-weight: 300;
				color: $uni-secondary-color;
			}

			.message-date {
				position: absolute;
				top: 34rpx;
				right: 30rpx;
				font-size: 24rpx;
				font-weight: 300;
				color: $uni-secondary-color;
			}
		}
	}
</style>