<template>
	<view class="message">
		<view class="message-system" @click="$navigateTo('/pages/messageSystem/messageSystem')">
			<uni-badge class="uni-badge-left-margin" :text="systemCount" absolute="rightTop" size="small">
				<view class="message-system-icon">
					<uni-icons type="email-filled" size="30" color="#fff"></uni-icons>
				</view>
			</uni-badge>
			<view class="message-content">
				<view class="message-title">系统通知</view>
				<view class="message-sub-content">
					{{systemCount === 0 ?'暂无消息通知' : `您有${systemCount}条未读消息，请点击查看～`}}
				</view>
			</view>
		</view>
		<view class="message-titles">即时会话</view>
		<mescroll-empty :option="option" v-if="chatList.length === 0" @emptyclick="$navigateTo('/pages/login/login')">
		</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 || item.createdAt"
						: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'
	import {
		getMessageTest
	} from '@/request/api/message.js'
	export default {
		data() {
			return {
				chatList: [],
				option: {
					tip: '暂无会话'
				},
				systemCount: 0,
				systemDate: ''
			}
		},
		onLoad() {
			uni.$on('changeChatList', (list) => {
				this.chatList = list.filter(node => node.conversationId !== 'system');
				// this.$store.getters.user.userId
				getMessageTest(3).then(res => {
					if (res.code == 200) {
						this.systemCount = res.data.length;
						this.unread();
					}
				});
			})
		},
		onShow() {
			console.log('on show');
			if (!uni.getStorageSync('token')) {
				this.chatList = [];
				this.option = {
					tip: '暂未登录',
					btnText: '点击登录'
				}
			} else {
				this.option = {
					tip: '暂无会话'
				}
				this.$chat.listenerList();
				this.init();
			}
		},
		methods: {
			init() {
				this.$chat.getConversationList((data) => {
					this.chatList = data.data.filter(node => node.conversationId !== 'system');
					getMessageTest(3).then(res => {
						if (res.code == 200) {
							this.systemCount = res.data.length;
							this.unread();
						}
					});
				});
			},
			linkTo(item) {
				this.$chat.clearConversationUnread(item.conversationId);
				this.$navigateTo('/pages/chat/chat?userId=' + item.conversationId + '&userName=' + item.userInfo.nickname)
			},
			unread() {
				let badge = this.systemCount;
				let list = this.chatList;
				for (let i = 0; i < list.length; i++) {
					badge += list[i].unread
				}
				if (!badge) {
					uni.removeTabBarBadge({
						index: 1
					})
				} else {
					uni.setTabBarBadge({
						index: 1,
						text: String(badge)
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.message {
		.message-system {
			background: #fff;
			display: flex;
			padding: 24rpx 30rpx;
			position: relative;
			margin-top: 30rpx;

			.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-system-icon {
				width: 100rpx;
				height: 100rpx;
				border-radius: 16rpx;
				text-align: center;
				line-height: 100rpx;
				background: $uni-primary;
			}
		}

		.message-titles {
			font-weight: 300;
			padding: 30rpx 30rpx 10rpx 30rpx;
		}

		.message-list {
			.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>