<!--
 * @Author: ch
 * @Date: 2022-03-26 14:32:03
 * @LastEditors: ch
 * @LastEditTime: 2022-05-21 19:10:58
 * @Description: file content
-->
<template>
	<view class="main">
		<view class="msg-item" v-for="item in msgData" :key="item.id">
			<view class="msg-item--title">
				<text>{{item.payload.title }} {{item.id}}</text>
				<text class="msg-item--time">{{FormatDate(item.createTimeStamp, 'mm-dd hh:ii:ss')}}</text>
			</view>
			<view class="msg-item--con">
				<image class="msg-item--img" src="@/static/message/xt.png" mode="widthFix"/>
				<view class="msg-item--desc">{{item.payload.content}}</view>
			</view>
		</view>
		<u-loadmore status="loading" v-if="!msgData || !msgData.length"/>
	</view>
</template>
<script>
import {Im, CreateUUID, ToAsyncAwait, FormatDate} from '@/common/utils';
export default {
	data(){
		return {}
	},
	computed:{
		curSessionData(){
			const data = this.$store.state.imData.find(i =>i.id == this.$Route.query.sessionId) || {}
			if(data.id){
				Im.setCurSessionId(data.id);
			}
			return data;
		},
		msgData (){
			return this.curSessionData ? this.curSessionData.messageList : [];
		}
	},
	watch:{
		curSessionData(){
			this.setTitle();
		}
	},
	
	onReachBottom(){
		this.getHistoryMsg();
	},
	onShow(){
		if(!this.$route.query.sessionId){
			this.$Router.back();
			return false
		}
		this.getHistoryMsg();
		this.readMsg();
		this.setTitle();
	},
	methods:{
		FormatDate : FormatDate,
		/**
		 * 获取历史消息
		 */
		setTitle(){
			uni.setNavigationBarTitle({
				title : this.curSessionData?.fromNickname || '系统消息'
			});
		},
		async getHistoryMsg(){
			this.loading = true;
			const lastMsg = this.msgData?.length ? this.msgData[0] : {};
			await ToAsyncAwait(Im.getHistoryMsg({
				content : {
					sessionId : this.$route.query.sessionId,
					topMessageId : lastMsg.id || null
				}
			}));
		},
		/**
		 * 把当前会话消息置为已读
		 */
		async readMsg(){
			Im.setRead({
				content: { 
					sessionId : this.$route.query.sessionId
				}
			});
		}
	}
}
</script>
<style lang="scss" scoped>
.msg-item{
	padding: 40rpx 0 0 40rpx;
	&--title{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-right: 40rpx;
		font-size: $font-size-lg;
		color: $color-grey6;
	}
	&--time{
		font-size: $font-size-sm;
		color: $color-grey4;
	}
	&--con{
		display: flex;
		border-bottom: 1px solid $color-grey2;
		padding: 40rpx 0;
	}
	&--img{
		width: 140rpx;
		height: 140rpx;
		border-radius: 12rpx;
		margin-right: 40rpx;
	}
	&--desc{
		width: 455rpx;
		font-size: $font-size-base;
		line-height: 39rpx;
		color: $color-grey5;
	}
}
</style>