<template>
	<view id='app'>
		<view class="head">
			<text>睿宝AI</text>
		</view>
		<view class="pageView">
			<view class="sidebar-left"></view>
			<view class="body">
				<view class="list-wrap">
					<scroll-view id="content-box" scroll-y="true" class="scrollview" :scroll-top="msgList.scrollTop">
						<!-- 聊天主体 -->
						<view id="content-overflow">
							<view class="scrollview-item" v-for="(item,index) in msgList.chatList" :key='index'>
								<!-- user方-聊天内容 -->
								<view class="item self" v-if="item.userContent != ''">
									<image class="avtar" src="../../static/user-avatar.png" mode="aspectFit"></image>
									<view class="content right" disabled>{{ item.userContent }}</view>
								</view>
								<!-- ai方-聊天内容 -->
								<view class="item other">
									<view class="">
										<view class="loadingPoint" v-if='msgList.isloading[index]'>
											<loadingPointsVue></loadingPointsVue>
										</view>
										<view class="content left" v-if='!msgList.isloading[index]'>
											{{ msgList.otherChatList[index] }}</view>
									</view>
									<image class="avtar" src="../../static/Ai-avatar.png" mode="aspectFit"></image>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>

				<view class="chat-bottom">
					<view class="send-msg">
						<view class="uni-textarea">
							<textarea id="input-textarea" type="text" v-model="msgList.chatMsg" placeholder="快来聊天吧"
								confirm-type="send" @confirm="handleSend" maxlength="300" />
						</view>
						<view class="uni-button">
							<button class="send-btn" @click="handleSend" type="primary" :disabled="msgList.isClick">发送</button>
						</view>
					</view>
				</view>
			</view>
			<view class="sidebar-right">
				<image src="../../static/logo.png" mode="aspectFit"></image>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		nextTick,
		reactive,
		watch
	} from 'vue';

	import {
		getDataList
	} from '../../api/ChatApp.js'

	import axios from 'axios';
	import loadingPointsVue from '../../components/loadingPoints.vue';

	const msgList = reactive({
		// 输入框
		chatMsg: '',
		// 聊天数据列表
		chatList: [],
		// 滚动距离
		scrollTop: 0,
		// setTimeout的返回值，唯一确定结束对应的setTimeout
		timer: null,
		// 
		textCount: 0,
		otherContent: '',
		// 处理后的ai数据返回值
		otherChatList: [],
		// 发送/返回的次数
		len: 0,
		// 加载符号是否显示
		isloading: [],
		isClick:false
	})

	// 发送消息
	const handleSend = async () => {
		msgList.isClick = !msgList.isClick
		const data = {
			AppId: '0c8ff06b-91d9-43f7-b61e-ca5da1db15d4',
			AId: 'TK1234',
			content: ''
		}
		if (msgList.chatMsg != '') {
			msgList.isloading[msgList.len] = true
			msgList.len++
			let obj = {
				otherContent: '',
				userContent: '',
				image: ""
			}
			data.content = msgList.chatMsg
			obj.userContent = msgList.chatMsg
			msgList.chatList.push(obj)

			obj.otherContent = await GetNewsList(data)
			// await sleep(16000);
			getChatContent(obj.otherContent, msgList.len)
		}
		
	}


	// 滚动条回到最底部
	const scrollToBottom = (() => {
		nextTick(() => {
			const query = uni.createSelectorQuery();
			query.select("#content-box").boundingClientRect()
			query.select("#content-overflow").boundingClientRect()
			query.exec(res => {
				const scrollViewHeight = res[0].height
				const scrollContentHeight = res[1].height
				if (scrollViewHeight < scrollContentHeight) {
					const scrollTop = scrollContentHeight - scrollViewHeight
					msgList.scrollTop = scrollTop
				}
			})
		})
	})
	//滚动至聊天底部
	watch([msgList.otherChatList,msgList.chatList], (newVal) => {
		scrollToBottom()
	})

	// 调用数据接口
	const GetNewsList = async (data) => {
		msgList.chatMsg = ''
		var res = await getDataList(data)
		console.log(res)
		return res
	}

	// ------- 聊天逐字输出 -----------
	// 延时函数
	// const sleep = ((delaytime = 10000) => {
	// 	return new Promise(resolve => setTimeout(resolve, delaytime));
	// })

	// 逐字显示内容
	const getChatContent = ((text, index) => {
		msgList.timer = setInterval(() => {
			msgList.textCount++;
			if (msgList.textCount == text.length + 1) {
				msgList.otherChatList[index - 1] = text;
				clearInterval(msgList.timer);
			} else {
				// 取字符串子串
				let nowStr = text.substring(0, msgList.textCount);
				msgList.otherChatList[index - 1] = nowStr;
			}

		}, 50);
		msgList.textCount = 0
		msgList.isloading[index - 1] = false
		msgList.isClick = !msgList.isClick
	})
</script>

<style>
	@import url("../../theme/chat15.css");
</style>