<template>
	<!-- 聊天消息 -->
	<view class="back">
		<view class="input u-flex">
			<u-icon name="search" color="rgba(153, 153, 153, 1)" size="40"></u-icon>
			<input @change='search' v-model="keywords" type="text" :placeholder="i18n.Searchchat"
				style="width: 600rpx;margin-left: 16rpx;" />
		</view>
		<!-- 客服 -->
<!-- 				<view class="" v-for="(item,idx) in list" :key="idx">
			<view v-for="(child,index) in item.easemob_log" :key="index">
				<view v-if="user_no !='13523652365' && item.form_id == '13523652365'" 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(Number(child.send_time)).getFullYear()}}-{{new Date(Number(child.send_time)).getMonth() + 1}}-{{new Date(Number(child.send_time)).getDate()}}
								{{new Date(Number(child.send_time)).toTimeString().substr(0, 8)}}
							</view>
						</view>
						<view class="u-flex u-row-between">
							<text class="news">{{child.text}}</text>
						</view>
					</view>
				</view>
			</view>
		</view> -->
		<view class="box ">
			<view style="margin-bottom: 26rpx;" v-for="(item,idx) in  list" :key="idx" >
				<view v-for="(child,index) in item.easemob_log" :key="index" @click="chat(item,child)">
					<view v-if="user_no !='13523652365' && item.targetId == '13523652365'" class="kefu u-flex"
						@click="tokefu(child)">
						<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(Number(child.send_time)).getFullYear()}}-{{new Date(Number(child.send_time)).getMonth() + 1}}-{{new Date(Number(child.send_time)).getDate()}}
									{{new Date(Number(child.send_time)).toTimeString().substr(0, 8)}}
								</view>
							</view>
							<view class="u-flex u-row-between">
								<text class="news">{{child.text}}</text>
							</view>
						</view>
					</view>
					<view class="u-flex " v-if="item.targetId != '13523652365'">
						<image v-if="item.form_id == user_no" :src="item.to_merchant.image || item.to_member.avatar"
							style="width: 88rpx;height: 88rpx;" mode=""> </image>
						<image v-else :src="item.form_member.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.form_id == user_no">
									{{item.to_merchant.merchant_name || item.to_member.nickname}}
								</view>
								<view class="name" v-else>
									{{ item.form_member.nickname}}
								</view>
								<view class="time">
									{{new Date(Number(child.send_time)).getFullYear()}}-{{new Date(Number(child.send_time)).getMonth() + 1}}-{{new Date(Number(child.send_time)).getDate()}}
									{{new Date(Number(child.send_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 class="news">{{child.text}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		conn,
	} from '@/utils/WebIM';
	export default {
		data() {
			return {
				keywords: '',
				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: {
			search() {
				uni.$u.http.post('/api/easemob/list', {
					keywords: this.keywords
				}).then((res) => {
					res.forEach((item)=>{
						if(item.form_id==this.user_no){
							item.targetId = item.to_id
						}else{
							item.targetId = item.form_id
						}
					})
					this.list = res
				}).catch(() => {

				})
			},
			dele() {
				let options = {
					// 会话 ID：单聊为对方的用户 ID，群聊为群组 ID。
					channel: "channel",
					// 会话类型：（默认） `singleChat`：单聊；`groupChat`：群聊。
					chatType: "singleChat",
					// 删除会话时是否同时删除服务端漫游消息。
					deleteRoam: true,
				};
				conn
					.deleteConversation(options)
					.then((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(item) {
				uni.navigateTo({
					url: '/pageA/service/service?id='+item.easemob_log_id
				})
			},
			chat(item,child) {
				if (item.form_id == this.user_no) {
					uni.navigateTo({
						url: '/pageA/chat/chat?user_other_no=' + item.targetId + '&user_other=' +
							encodeURIComponent(JSON.stringify(item.to_merchant || item.to_member))+'&id='+child.easemob_log_id
					})
				} else {
					uni.navigateTo({
						url: '/pageA/chat/chat?user_other_no=' + item.targetId + '&user_other=' +
							encodeURIComponent(
								JSON.stringify(item.form_member))+'&id='+child.easemob_log_id
					})
				}
			}
		}
	}
</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>