<template>
	<view class="uni-page ai-chat">
		<u-navbar :autoBack="true" bgColor="#fff0e7" placeholder title="寄信助手" right-icon=""
			@rightClick="clearMessageList">

			<template slot="right">
				<image @click="clearMessageList" class="img-icon" src="/static/icons/清除_clear.svg" alt=""
					style="height: 47rpx; width: 47rpx;">
				</image>
			</template>
		</u-navbar>
		<view class="ai-chat-hint" v-if="!messageList.length">
			<view class="message-item-chat message-item">
				<view class="message-item-avatar">
					<image src="/static/03头像@2x.png" alt="" style="width: 100%;height: 100%;">
				</view>
				<view class="message-item-content">
					<view class="message-hint-temp-title">
						Hi,我是你的寄信助手小艾
					</view>
					<view class="message-hint-temp-explain">
						有什么问题可以直接问我...
					</view>
				</view>
			</view>
			<view class="hint-message-box">
				<view @click="sendMessage(item)" class="hint-message-item" v-for="item in hintMessage" :key="item">
					{{item}}
				</view>
			</view>
		</view>
		<scroll-view :scroll-into-view="scrollView" class="chat-messageList" id="chat-messageList" v-else
			:scroll-y="true">
			<template v-for="(mess,index) in messageList">
				<view v-if="mess.role==='user'" :key="index" class="message-item-user message-item">
					<view class="message-item-content" style="margin:0 10rpx 0 100rpx;word-wrap: break-word;">
						{{mess.message}}
					</view>
					<view class="message-item-avatar">
						<image style="width: 100%;height: 100%;" :src="userInfo.avatar" alt="">
					</view>
				</view>
				<view class="message-item-chat message-item" v-if="mess.role==='assistant'">
					<view class="message-item-avatar">
						<image style="width: 100%;height: 100%;" src="/static/03头像@2x.png" alt="">
					</view>
					<view class="message-item-content" style="margin:0  100rpx 0 10rpx ;">
						<u-loading-icon v-if="!mess.message"></u-loading-icon>
						<text :id="!mess.message?'loading-message':''">{{mess.message}}</text>
						<view v-if="mess.message" class="message-item-content-toolbar">
							<view class="message-toolbar-left">
								<!-- <view>
								确定内容	
							</view> -->
								<view @click="toWrite(mess.message)">
									去编辑
								</view>
							</view>
							<view class="message-toolbar-right">
								<view @click="copy(mess.message)">
									复制
								</view>
							</view>
						</view> 
					</view>
				</view>

			</template>
			<view :id="'scroll_base'+messageList.length">

			</view>
		</scroll-view>

		<view class="chat-floor">
			<!-- <view class="floor-toolbar">
				<view class="floor-toolbar-item">
					发送照片
				</view>
				<view class="floor-toolbar-item">
					发送订单
				</view>
			</view> -->
			<view class="textarea-box">
				<u--textarea :disabled="isSnedMsg" :autoHeight="true" :maxlength="99999" :placeholder="placeholder"
					v-model="mesText">
				</u--textarea>
				<view class="button-box" @click="sendMessage(mesText)">
					<image :class="{img:true,
					'button-disable':isSnedMsg}" src="/static/25发送@2x.png" alt="发送"></image>
				</view>
			</view>
			<view class="limit-of-times">
				<!-- 	智能寄信 套餐剩余次数 <text style="color: #fe9262;margin: 0 10rpx;">
					2
				</text> 次 -->
			</view>
		</view>
	</view>
</template>

<script>
	import '@/utils/EncoderDecoderTogether.min.js'
	import {
		getMessageList,
		sendMessageApi
	} from '@/request/wm.js'
	export default {
		data() {
			return {
				placeholder: '输入您的问题...',
				messageList: [],
				hintMessage: ['我要给狱中的朋友寄一封信...', '帮我给好朋友写封信...', '帮我写封信...', '我想给狱中的朋友写封信，有什么注意事项...'],
				mesText: '',
				userInfo: {},
				scrollView: 'scroll_base',
				isSnedMsg: false
			}
		},
		components: {},
		onReady() {
			this.userInfo = this.$store.userInfo
			console.log(this.$store.userInfo, '11111111111111');
			let isNewChat = uni.getStorageSync('NEW_CHAT')
			if (isNewChat) return
			this.initData()
			console.log(this.userInfo);
		},
		mounted() {},
		methods: {
			async sendMessage(mes) {
				if (this.isSnedMsg) return
				this.isSnedMsg = true
				let isNewchat = uni.getStorageSync('NEW_CHAT')
				this.messageList.push({
					role: 'user',
					message: mes
				})
				this.messageList.push({
					role: 'assistant',
					message: ''
				})
				this.mesText = ''
				this.$nextTick(() => {
					this.scrollView = 'loading-message'
				})
				await sendMessageApi({
					message: mes,
					new: +isNewchat || 0
				}, this)
				console.log(this.messageList[this.messageList.length - 1],'aaa');
				// this.messageList[this.messageList.length - 1] = {
				// 	role: 'assistant',
				// 	message: data.msg
				// }
				uni.setStorageSync('NEW_CHAT', '')

				this.$nextTick(() => {
					setTimeout(() => {
						this.scrollView = 'scroll_base' + this.messageList.length
					})
				})
				// this.isSnedMsg = false
			},
			async initData() {
				let {
					data
				} = await getMessageList()
				this.messageList = data.data.data
				this.$nextTick(() => {
					this.scrollView = 'scroll_base' + data.data?.data.length || ''
				})

			},
			clearMessageList() {
				this.messageList = []
				this.isSnedMsg= false
				uni.setStorageSync('NEW_CHAT', JSON.stringify(1))
			},
			copy(text) {
				uni.setClipboardData({
					data: text,
					success: function() {
						uni.showToast({
							title: '复制成功',
							icon: 'success'
						})
					}
				})
			},
			toWrite(text) {
				this.$mutations.setLetterInfo({
					message: text.replace(/\\n/g , '\n')
				})
				console.log(this.$store.letterInfo);
				uni.navigateTo({
					url: 'pages/editor-letter/editor-letter'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.uni-page {
		display: flex;
		flex-direction: column;
		background: radial-gradient(circle, #ffe3d6, #ffe8dd, #fff3ee);

		.chat-messageList {
			width: 100%;
			flex: 1;
			overflow: auto;
		}

		.ai-chat-hint {
			flex: 1;
		}

		.img-icon {
			// #ifdef MP-WEIXIN
			position: absolute;
			right: 200rpx;
			top: 50%;
			transform: translateY(-50%);
			// #endif
		}
	}

	::v-deep .u-textarea--disabled {
		background-color: rgba(245, 247, 250, 0.3) !important;
	}

	.button-disable {
		opacity: 0.3;
	}

	.ai-chat-hint {
		.message-item {
			height: fit-content;

			.message-item-content {
				padding: 12rpx;
				background-color: transparent;
			}
		}

	}


	.ai-chat {
		.message-item {
			padding: 20rpx 0;
			display: flex;
			width: 100%;
			overflow: hidden;

			.message-item-content {
				flex: 1;
				font-size: 30rpx;
				color: #222222;
			}

		}

		.message-item-avatar {
			width: 96rpx;
			height: 96rpx;
			border-radius: 50%;
			overflow: hidden;

			img {
				width: 100%;
				height: 100%;
				// border-radius: 50%;
			}
		}

		.hint-message-box {
			display: flex;
			flex-wrap: wrap;

			.hint-message-item {
				color: #333333;
				width: fit-content;
				border-radius: 40rpx;
				margin-right: 30rpx;
				margin-top: 20rpx;
				background-color: rgba(255, 255, 255, 0.5);
				padding: 22rpx 35rpx;
				font-size: 32rpx;
			}
		}

		.message-item-content {
			padding: 35rpx;
			background-color: rgba(255, 255, 255, 0.5);
			;
			border-radius: 40rpx;
			overflow: auto;

			.message-hint-temp-title {
				font-size: 32rpx;
				// width: 387rpx;
				// height: 42rpx;
				color: #333333;
			}

			.message-hint-temp-explain {
				// width: 340rpx;
				// height: 40rpx;
				color: #E08745;
				font-size: 30rpx;
			}
		}

		.message-item-content-toolbar {
			display: flex;
			margin-top: 20rpx;
			color: #CE7A2C;
			justify-content: space-between;

			.message-toolbar-left {
				view {
					margin-right: 20rpx;
				}

				display: flex;
			}
		}

		.chat-floor {
			padding: 20rpx 0;

			.floor-toolbar {
				display: flex;
				font-size: 26rpx;
				flex-wrap: wrap;
				margin-bottom: 20rpx;
			}

			.floor-toolbar-item {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 123rpx;
				height: 52rpx;
				background-color: #fed2b8;
				color: #FE9161;
				margin-right: 30rpx;
			}

			.textarea-box {
				height: 270rpx;
				border-radius: 20rpx;
				border: 3rpx solid #FE9161 !important;
				display: flex;
				overflow-y: auto;
			}

			::v-deep .u-textarea {
				flex: 1;
				border: unset;
				padding: 0;
				background: transparent;

				.u-textarea__field {}

				textarea {
					padding: 20rpx;
				}
			}


			::v-deep .button-box {
				position: absolute;
				right: 20rpx;
				bottom: 60rpx;
				display: flex;
				align-items: flex-end;
				border: unset;
				padding: 20rpx;

				.img {
					width: 120rpx;
					height: 76rpx;
					object-fit: contain;
				}


			}

			.limit-of-times {
				margin-top: 10rpx;
				text-align: center;
				font-size: 26rpx;
			}

		}


	}
</style>