<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<!-- 聊天内容 -->
				<scroll-view class="chat" scroll-y="true" scroll-with-animation="true" :scroll-into-view="scrollToView">
					    <view class="chat-main" :style="{paddingBottom:inputh+'px'}">
						<view class="chat-ls" v-for="(item,index) in unshiftmsg" :key="index" :id="'msg'+ index">
							<view class="chat-time" v-if="item.time != ''">
								{{item.time}}
							</view>
							<view class="msg-m msg-left" v-if="item.speaker !=  friendName">
								<image class="user-img" :src="item.file&&item.file.length>0?item.file[0].url:''">
								</image>
								<!-- 文字 -->
								<view class="message" v-if="item.TextType == 0">                           
									<view class="msg-text">{{item.content}}</view>
								</view>
								<!-- 图像 -->
								<view class="message img" v-else @tap="previewImg(item.content)">
									<image :src="item.content&&item.content.length>0?item.content[0].url:''"
										class="msg-img" mode="widthFix"></image>
								</view>
							</view>
							<view class="msg-m msg-right" v-if="item.speaker == friendName">
								<image class="user-img" :src="item.file&&item.file.length>0?item.file[0].url:''">
								</image>
								<!-- 文字 -->
								<view class="message" v-if="item.TextType == 0">
									<view class="msg-text">{{item.content}}</view>
								</view>
								<!-- 图像 -->
								<view class="message img" v-else @tap="previewImg(item.content)">
									<image :src="item.content&&item.content.length>0?item.content[0].url:''"
										class="msg-img" mode="widthFix"></image>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
				<submit_1 @inputs="inputs" @heights="heights"></submit_1>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	import moment from 'moment';
	import submit_1 from './components/submit_1.vue';
	export default {
		components: {
			submit_1,
		},
		data() {
			return {
				friendName: "zs",
				info: {},
				id: '',
				name: '',
				msg: [],
				// 反转数据接收
				unshiftmsg: [{
						"speaker": "zs",
						"content": "这是第一条未读消息",
						"time": "2023-01-12 12:21:03",
						"TextType": 0
					},
					{
						"speaker": "yy",
						"content": "这是第二条未读消息",
						"time": "2023-01-12 12:22:58",
						"TextType": 0
					},
					{
						"speaker": "zs",
						"content": "这是第三条未读消息",
						"time": "2023-01-12 12:22:03",
						"TextType": 0
					},
					{
						"speaker": "yy",
						"content": "这是第四条未读消息",
						"time": "2023-01-12 12:21:58",
						"TextType": 0
					},
					{
						"speaker": "zs",
						"content": "这是第五条未读消息",
						"time": "2023-01-12 12:22:03",
						"TextType": 0
					},
					{
						"speaker": "yy",
						"content": "这是第六条未读消息",
						"time": "2023-01-12 12:21:58",
						"TextType": 0
					},
					{
						"speaker": "zs",
						"content": "这是第七条未读消息",
						"time": "2023-01-12 12:22:03",
						"TextType": 0
					},
					{
						"speaker": "yy",
						"content": "这是第八条未读消息",
						"time": "2023-01-12 12:21:58",
						"TextType": 0
					},
					{
						"speaker": "zs",
						"content": "这是第九条未读消息",
						"time": "2023-01-12 12:22:03",
						"TextType": 0
					},
					{
						"speaker": "zs",
						"content": "这是第十条未读消息",
						"time": "2023-01-12 12:21:58",
						"TextType": 0
					},
				],
				imgMsg: [],
				inputh: '60',
				scrollToView: '',
			};
		},
		onLoad: async function(e) {
			const that = this;
			that.$set(that, `id`, e.id);
			that.$set(that, `name`, e.name);
			// await that.searchOther();
			if (e.name) {
				uni.setNavigationBarTitle({
					title: e.name
				});
			}
			if (e.id) await that.search()
		},
		methods: {
			async search() {
				const that = this;
				let res;
				res = await that.$api(`/chatRecord`, `GET`, {
					room: that.id
				}, 'chat');
				if (res.errcode == '0') {
					console.log(res);
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
				// 跳转到最后一条数据 与前面的:id进行对照
				that.$nextTick(function() {
					that.scrollToView = 'msg' + (that.unshiftmsg.length - 1)
				})
			},
			// 进行图片的预览
			previewImg(e) {
				const that = this;
				let urls = e && e[0] && e[0].url;
				// 预览图片
				uni.previewImage({
					current: 0,
					urls: [urls],
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			//接受输入内容
			inputs(e) {
				const that = this;
				//时间间隔处理
				let data = {
					"room": "1001",
					"speaker": "zs",
					"content": e.message,
					"time": moment().format('YYYY-MM-DD HH:mm:ss'),
					"TextType": e.type
				};
				// 发送给服务器消息
				// onSendWS(JSON.stringify(data));

				that.unshiftmsg.push(data);
				// 跳转到最后一条数据 与前面的:id进行对照
				that.$nextTick(function() {
					that.scrollToView = 'msg' + (that.unshiftmsg.length - 1)
				})
				if (e.type == 1) {
					that.imgMsg.push(e.message);
				}
				console.log(e)
			},
			//输入框高度
			heights(e) {
				const that = this;
				that.inputh = e;
				that.goBottom();
			},
			// 滚动到底部
			goBottom() {
				const that = this;
				that.scrollToView = '';
				that.$nextTick(function() {
					that.scrollToView = 'msg' + (that.unshiftmsg.length - 1)
				})
			}
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			.chat {
				height: 100%;

				.chat-main {
					padding-left: 32rpx;
					padding-right: 32rpx;
					padding-top: 20rpx;
					display: flex;
					flex-direction: column;
				}

				.chat-ls {
					.chat-time {
						font-size: 24rpx;
						color: rgba(39, 40, 50, 0.3);
						line-height: 34rpx;
						padding: 10rpx 0rpx;
						text-align: center;
					}

					.msg-m {
						display: flex;
						padding: 20rpx 0;

						.user-img {
							flex: none;
							width: 80rpx;
							height: 80rpx;
							border-radius: 40rpx;
							border: 1px solid #c0c0c0;
						}

						.message {
							flex: none;
							max-width: 480rpx;
						}

						.img {
							margin: 0 20rpx 0 0;
						}

						.msg-text {
							font-size: 32rpx;
							color: rgba(39, 40, 50, 1);
							line-height: 44rpx;
							padding: 18rpx 24rpx;
						}

						.msg-img {
							max-width: 400rpx;
							border-radius: 20rpx;
						}
					}

					.msg-left {
						flex-direction: row;

						.msg-text {
							margin-left: 16rpx;
							background-color: #f1f1f1;
							border-radius: 0rpx 20rpx 20rpx 20rpx;
						}

						.ms-img {
							margin-left: 16rpx;
						}

					}

					.msg-right {
						flex-direction: row-reverse;

						.msg-text {
							margin-right: 16rpx;
							background-color: rgba(255, 228, 49, 0.8);
							border-radius: 20rpx 0rpx 20rpx 20rpx;
						}

						.ms-img {
							margin-right: 16rpx;
						}
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>
