<template>
	<!-- 聊天消息 -->
	<view class="back">
		<view class="input u-flex" @click="toserve">
			<u-icon name="search" color="rgba(153, 153, 153, 1)" size="40"></u-icon>
			<input disabled type="text" :placeholder="i18n.Searchchat" style="width: 600rpx;margin-left: 16rpx;" />
		</view>
		<!-- 客服 -->
		<view class="" v-for="(item,idx) in list" :key="idx">
			<view v-if="user_no !='19550568052' && item.conversationId == '19550568052'" class="kefu u-flex"
				@click="tokefu">
				<image src="/pageA/static/images/kefu.png" style="width: 88rpx;height: 88rpx;" mode=""></image>
				<view class="u-row-between"
					style="height: 88rpx;display: flex;flex-direction: column;margin-left: 20rpx;width: 85%;">
					<view class="u-flex u-row-between">
						<view class="name">{{i18n.customerService}}</view>
						<view class="time">
							{{new Date(item.lastMessage.time).getFullYear()}}-{{new Date(item.lastMessage.time).getMonth() + 1}}-{{new Date(item.lastMessage.time).getDate()}}
							{{new Date(item.lastMessage.time).toTimeString().substr(0, 8)}}
						</view>
					</view>
					<view class="u-flex u-row-between">
						<text v-if="item.lastMessage.msg" class="news">{{item.lastMessage.msg}}</text>
						<text v-if="item.lastMessage.type == 'img'" class="news">{{i18n.picture}}</text>
						<text v-if="item.lastMessage.type == 'audio'" class="news">{{i18n.voiceMessage}}</text>
						<u-badge :type="type" max="99" :value="item.unReadCount"></u-badge>
					</view>
				</view>
			</view>
		</view>
		<view class="box ">
			<view style="margin-bottom: 26rpx;" v-for="(item,idx) in  list" :key="idx" @click="chat(item)">
				<!-- 			<u-swipe-action>
					<u-swipe-action-item :options="options1" @click.stop="dele">
						<view class="">
							<view class=""> -->
				<view class="u-flex " v-if="item.conversationId != '19550568052'">
					<image v-if="item.lastMessage.ext.user.easemob_username == user_no"
						:src="item.lastMessage.ext.user_other.image || item.lastMessage.ext.user_other.avatar"
						style="width: 88rpx;height: 88rpx;" mode="">
						<image v-else :src="item.lastMessage.ext.user.avatar" style="width: 88rpx;height: 88rpx;"
							mode="">
						</image>
						<view class="u-row-between"
							style="display: flex;flex-direction: column;margin-left: 20rpx;width: 85%;">
							<view class="u-flex u-row-between">
								<view class="name" v-if="item.lastMessage.ext.user.easemob_username == user_no">
									{{ item.lastMessage.ext.user_other.merchant_name ||item.lastMessage.ext.user_other.nickname || item.lastMessage.ext.user_other.member.easemob_nickname}}
								</view>
								<view class="name" v-else>
									{{ item.lastMessage.ext.user.nickname}}
								</view>
								<!-- <view class="name">{{item.lastMessage.ext.user}}</view> -->
								<view class="time">
									{{new Date(item.lastMessage.time).getFullYear()}}-{{new Date(item.lastMessage.time).getMonth() + 1}}-{{new Date(item.lastMessage.time).getDate()}}
									{{new Date(item.lastMessage.time).toTimeString().substr(0, 8)}}
								</view>
							</view>
							<view class="u-flex u-row-between"
								style="border-bottom: 2rpx solid #eae7e7;padding-bottom: 26rpx;margin-top: 10rpx;">
								<text v-if="item.lastMessage.msg" class="news">{{item.lastMessage.msg}}</text>
								<text v-if="item.lastMessage.type == 'img'" class="news">{{i18n.picture}}</text>
								<text v-if="item.lastMessage.type == 'audio'" class="news">{{i18n.voiceMessage}}</text>
								<u-badge :type="type" max="99" :value="item.unReadCount"></u-badge>
							</view>
						</view>
				</view>
				<!-- 							</view>
						</view>
					</u-swipe-action-item>
				</u-swipe-action> -->
			</view>
			<!-- 			<view style="margin-bottom: 26rpx;" v-for="(item,idx) in  list" :key="idx" @click="chat(item)">
				<view class="u-flex " v-if="item.conversationId != '19550568052'">
					<image :src=" item.lastMessage.ext.user_other.avatar" style="width: 88rpx;height: 88rpx;" mode="">
					</image>
					<view class="u-row-between"
						style="display: flex;flex-direction: column;margin-left: 20rpx;width: 85%;">
						<view class="u-flex u-row-between">
							<view class="name" v-if="item.lastMessage.from == user_no">
								{{ item.lastMessage.ext.user_other.merchant_name ||item.lastMessage.ext.user_other.nickname}}
							</view>
							<view class="name" v-else>
								{{ item.lastMessage.ext.user.nickname}}
							</view>
							<view class="time">
								{{new Date(item.lastMessage.time).getFullYear()}}-{{new Date(item.lastMessage.time).getMonth() + 1}}-{{new Date(item.lastMessage.time).getDate()}}
								{{new Date(item.lastMessage.time).toTimeString().substr(0, 8)}}
							</view>
						</view>
						<view class="u-flex u-row-between"
							style="border-bottom: 2rpx solid #979797;padding-bottom: 26rpx;margin-top: 10rpx;">
							<text v-if="item.lastMessage.msg" class="news">{{item.lastMessage.msg}}</text>
							<text v-if="item.lastMessage.type == 'img'" class="news">图片</text>
							<u-badge :type="type" max="99" :value="item.unReadCount"></u-badge>
						</view>
					</view>
				</view>
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		conn,
	} from '@/utils/WebIM';
	export default {
		data() {
			return {
				cursor:'',
				type: "error",
				value: 1,
				list: [],
				user_no: '',
				options1: [{
					text: '删除',
					style: {
						backgroundColor: 'red',
					}
				}]
			};
		},
		onReachBottom() {
			this.getlist()
		},
		onLoad() {
			this.user_no = uni.getStorageSync('user_no')
			// this.getlist()
		},
		onShow() {
			this.getlist()
		},
		computed: {
			i18n() {
				return this.$t('index')
			}
		},
		methods: {
			toserve(){
				uni.navigateTo({
					url:'/pageA/chat/search'
				})
			},
			dele() {
				let options = {
					// 会话 ID：单聊为对方的用户 ID，群聊为群组 ID。
					channel: "channel",
					// 会话类型：（默认） `singleChat`：单聊；`groupChat`：群聊。
					chatType: "singleChat",
					// 删除会话时是否同时删除服务端漫游消息。
					deleteRoam: true,
				};
				conn
					.deleteConversation(options)
					.then((res) => {
						console.log(res);
					})
					.catch((e) => {
						// 删除失败。
					});
			},
			getlist() {
				conn.getServerConversations({
					pageSize: 50,
					cursor: this.cursor
				}).then((res) => {
					console.log(res);
					// this.list.concat(res.data.conversations)
					if(this.cursor==''){
						this.list = res.data.conversations
					}
					if(this.cursor){
						this.list.concat(res.data.conversations)
					}
					this.cursor = res.data.cursor
				})
			},
			tokefu() {
				uni.navigateTo({
					url: '/pageA/service/service'
				})
			},
			chat(item) {
				if (item.lastMessage.ext.user.easemob_username == this.user_no) {
					uni.navigateTo({
						url: '/pageA/chat/chat?user_other_no=' + item.conversationId + '&user_other=' +
							encodeURIComponent(JSON
								.stringify(item.lastMessage.ext.user_other))
					})
				} else {
					uni.navigateTo({
						url: '/pageA/chat/chat?user_other_no=' + item.conversationId + '&user_other=' +
							encodeURIComponent(
								JSON.stringify(item.lastMessage.ext.user))
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.back {
		width: 750rpx;
		// height: 1624rpx;
		background: #F4F4F4;
		padding: 20rpx 24rpx;
		box-sizing: border-box;

		.input {
			width: 702rpx;
			height: 72rpx;
			background: #FFFFFF;
			border-radius: 36rpx;
			padding: 0 24rpx;
			box-sizing: border-box;
		}

		.kefu {
			width: 702rpx;
			height: 156rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 36rpx 24rpx 32rpx;
			box-sizing: border-box;
			margin-top: 24rpx;
		}

		.name {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 32rpx;
			color: #222222;
			line-height: 44rpx;
			text-align: left;
			font-style: normal;
		}

		.time {
			font-family: SFPro, SFPro;
			font-weight: 400;
			font-size: 20rpx;
			color: #666666;
			line-height: 24rpx;
			text-align: left;
			font-style: normal;
		}

		.news {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #666666;
			line-height: 34rpx;
			text-align: left;
			font-style: normal;
		}

		.box {
			width: 702rpx;
			min-height: 1156rpx;
			background: #FFFFFF;
			border-radius: 16rpx;
			padding: 40rpx 24rpx 20rpx;
			box-sizing: border-box;
			margin-top: 20rpx;
		}
	}
</style>