<template>
	<view class="contents">
		<login></login>
		<!-- 视频区 -->
		<view class="banner">
			<lives></lives>
		</view>
		<tabs @skipComponent="skip"></tabs>
		<!-- 聊天 -->
		<component :is="currentTabComponent" :specialUrl="special" :msgList="msgList"></component>
		<!-- 底部 -->
		<view class="footer" v-show="currentTabComponent == 'chat'">
			<foot @specialShow="specialShow" @sendmsg="sendmsg"></foot>
		</view>
	</view>
</template>

<script>
	import login from "@/components/login/login.vue"
	import tabs from "@/components/tabs/tabs.vue"
	import foot from "@/components/footer/footer.vue"
	import chat from "@/components/chat/chat.vue"
	import sinatv from "@/components/tabs/sinaTv/sinaTv.vue"
	import lives from "@/components/live/live.vue"
	import courseware from "@/components/tabs/courseware/courseware.vue"
	import face from "@/until/face.js"

	var Live
	export default {
		components: {
			foot,
			chat,
			login,
			tabs,
			sinatv,
			courseware,
			lives
		},
		data() {
			return {
				loading: true,
				value: '',
				currentTabComponent: 'chat',
				special: "",
				msgList: [],
				scrollTop:0
			}
		},
		created() {
			this.getusertoken()
		},
		onshow() {

		},
		methods: {
			//触发消息
			sendmsg(val) {
				this.send(val)
			},
			skip(value) {
				this.currentTabComponent = value
			},
			specialShow(url) {
				this.special = url
				let _this = this
				let timer = setTimeout(() => {
					_this.special = ''
				}, 1000)
			},
			gsnickname(val) {
				var msgname = val
				msgname = msgname.replace(/\d{8,}/g, "*")
				msgname = msgname.length > 8 ? msgname.substring(0, 8) + "..." : msgname
				return msgname
			},
			//获取房间token
			getusertoken() {
				this.$nextTick(() => {
					if (uni.getStorageSync("login_ok") == 'success') {
						this.$api.getusertoken().then(res => {
							var that = this
							var roomdid = res.data.data
							if (uni.getStorageSync('room_token')) {
								roomdid = uni.getStorageSync('room_token')
							}
							Live = new Finezb.Live(roomdid)
							Live.login(function() {
								console.log('登录成功', JSON.stringify(Live.getLoginUser()))
								uni.setStorageSync("user_account", Live.getLoginUser().account)
							})
						})
					}
				})
			},
			//发送消息
			send(val) {
				if (!val.sendText) {
					return;
				}
				let data = {
					text: val.sendText,
					type: val.type,
					nickName: uni.getStorageSync("user_name"),
				};
				if (uni.getStorageSync("watch_type") == 4 && uni.getStorageSync("wxnick") == 0) {
					data.nickName = uni.getStorageSync("user_name1")
				}
				data.extra = {
					headUrl: uni.getStorageSync("user_headimg"),
					role: 4,
					isDirty: 0
				};
				// var sendlist = this.chatsetinfo.sensitiveWord
				// if (this.chatsetinfo.sensitive && sendlist.length > 0) {
				// 	for (var index in sendlist) {
				// 		if (sendlist[index] != "" && sendlist[index].trim() != "" && this.sendText.includes(sendlist[
				// 				index])) {
				// 			data.extra.isDirty = 1
				// 			break;
				// 		}
				// 	}
				// }
				data.extra = JSON.stringify(data.extra);
				let msg = {
					type: val.type,
					sendTime: new Date().getTime(),
					extra: {
						headUrl: uni.getStorageSync("user_headimg"),
						role: 4
					},
					sendName: uni.getStorageSync("user_name"),
					text: face.replaceEm(val.sendText)
				}
				if (uni.getStorageSync("watch_type") == 4 && uni.getStorageSync("wxnick") == 0) {
					msg.sendName = uni.getStorageSync("user_name1")
				}
				msg.sendName = msg.sendName.replace(/\d{8,}/g, "*")
				msg.sendName = msg.sendName.length > 8 ? msg.sendName.substring(0, 8) + "..." : msg.sendName
				this.msgList.push(msg)
				this.scrollTop = 9999999999
				// console.log(that.datalist)
				// let id = 'msg' + msg.sendTime;
				// this.msgBoxToBottom(id);
				Live.sendMsg(data, function(ack) {
					console.log(ack);
					if (ack.code === 200) {
						this.scrollTop = 500
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.contents {
		width: 750rpx;
		display: flex;
		flex-direction: column;

		.banner {
			width: 100%;
			height: 420rpx;
			background-image: url("../../static/images/banner.png");
			background-repeat: no-repeat;
			background-size: contain;
		}

		.title {
			height: 72rpx;
			background-color: #5E6DF0;
			color: white;
			display: flex;
			align-items: center;
			padding-left: 20rpx;
		}

		.footer {
			height: 88rpx;
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			display: flex;
			background-color: white;
			box-shadow: 0 -2rpx 30rpx #dadada;
			;
		}

		#playerback {
			width: 100%;
			height: 420rpx;
			z-index: 0;
		}
	}
</style>
