<template>
	<view class="message">
		<view class="message-empty" v-if="chatList.length === 0">
			<uv-empty icon="https://file-node.oss-cn-shanghai.aliyuncs.com/youji/fa57e25b38c442ebb0ba023cace796bb"
				:isImg="true" textSize="28" width="360" :text="option.tip">
				<view class="message-button-box">
					<uv-button type="primary" :text="option.btnText" size="small" v-if="option.btnText"
						@tap="$navigateTo('/pages/login/login')">
					</uv-button>
				</view>
			</uv-empty>
		</view>
		<view class="message-list" v-else>
			<!-- 消息提示 -->
			<view class="message-item" v-for="(item,index) in chatList" :key="index" @click="linkTo(item)">
				<view class="message-image-box">
					<uv-badge type="error" max="99" numberType="overflow" :value="item.unread" :absolute="true"
						:offset="[0,0]" :customStyle="{
							zIndex:99
						}">
					</uv-badge>
					<image :src="item.userInfo.avatarUrl" class="message-image" mode="aspectFill">
					</image>
				</view>
				<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>
	export default {
		data() {
			return {
				option: {
					tip: '暂无会话'
				},
				chatList: []
			}
		},
		onReady() {
			uni.$on('changeChatList', this.init);
		},
		onShow() {
			this.$customerServe.listenerList();
			this.init();
		},
		methods: {
			init() {
				this.$customerServe.getConversationList(data => {
					this.chatList = data.data.filter(node => node.conversationId !== 'system');
				});
			},
			linkTo(item) {
				if (item.unread > 0) this.$customerServe.clearConversationUnread(item.conversationId);
				this.$navigateTo('/subPages/myPage/serveChat/serveChat?userId=' + item.conversationId + '&userName=' + item
					.userInfo.nickname)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.message {
		padding: 30rpx 0;

		.message-button-box {
			margin-top: 40rpx;
		}

		.message-empty {
			padding-top: 80rpx;
		}

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

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

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

		.message-image-box {
			width: 100rpx;
			height: 100rpx;
			position: relative;
		}

		.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: $uv-content-color;
		}

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