/** 将15寸和32寸共同用到的 函数内容，封装成一个js文件 */ 
	
	
	import {
		nextTick,
		reactive,
		watch
	} from 'vue';
	import { getDataList } from '../api/ChatApp';
	
	
	// 发送消息
	const handleSend = async (msgList) => {
		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(msgList,data)

			// await sleep(16000);
			getChatContent(msgList,obj.otherContent, msgList.len)


		}

		msgList.otherContent = ''
	}
	
	// 调用数据接口
	const GetNewsList = async (msgList,data) => {
		msgList.chatMsg = ''
		var res = await getDataList(data)
		console.log(res)
		return res
	}
	
	// 逐字显示内容
	const getChatContent = ((msgList,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);
		console.log(msgList.timer)
		msgList.textCount = 0
		msgList.isloading[index - 1] = false
		msgList.isClick = !msgList.isClick
	})
	
	// 滚动条回到最底部
	const scrollToBottom = ((msgList) => {
		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
				}
			})
		})
	})


	// 背景颜色变换
	const styleChange = ((styleData)=>{
		styleData.value = !styleData.value
		if(styleData.value == true){
			styleData.color = "rgba(0,0,0,0.8)"
			styleData.fontColor = "#fff"
			styleData.btnColor = "#ccc"
			styleData.contentColor = "rgba(0,0,0,0.5)"
			styleData.labelColor = "rgb(105,105,105)"
			styleData.borderColor = "rgb(105,105,105)"
			
		}else{
			styleData.color = "#f5f5f5"
			styleData.fontColor = "black"
			styleData.btnColor = "rgb(30,144,255)"
			styleData.contentColor = "#fff"
			styleData.labelColor = "#fff"
			styleData.borderColor = "rgb(30,144,255)"
		}	
		
	})
	
	//快捷键输入
	const shortcuts = ((msgList,item)=>{
		msgList.chatMsg = item
		handleSend(msgList)
	})
	
	export {
		handleSend,
		styleChange,
		scrollToBottom,
		shortcuts
	}