<template>
	<view class="container">
		<view class="search-box">
			<uni-search-bar ref="searchBar" radius="5" placeholder="搜索" clearButton="auto" cancelButton="none" bgColor="#F8F8F8"
			 @confirm="search" class="search-bar" />
		</view>
		<scroll-view class="video-box" scroll-y="true" @scrolltolower="handleLoadMore()">
			<view style="padding-top: 20px;background-color: #FFFFFF;" v-if="!videoList.length">
				<u-divider color="#909399" border-color="#909399">没有更多了</u-divider>
			</view>
			<view class="video-col" v-for="(item, index) in videoList" :key="index" @click="goToVideoDetail(item)">
				<image class="video-img" :src="videoType == 1 ? item.imgUrl : item.coverUrl" mode="aspectFill"></image>
				<view class="video-title">{{videoType == 1 ? item.liveName : item.videoName}}</view>
				<view class="video-date" v-if="videoType == 1">直播时间:{{item.liveStartTime}}-{{item.liveEndTime}}</view>
				<view class="video-mask" v-if="videoType == 1 && item.liveStatus == 2">
					<view class="video-mask-horn"></view>
					<view class="video-mask-text">未开播</view>
				</view>
			</view>
		</scroll-view>
		<u-top-tips ref="uTips"></u-top-tips>
		<view class="uni-popup-dialog" :hidden="show_qx">
			<view class="uni-dialog-title">
				<text class="uni-dialog-title-text">提示</text>
			</view>
			<view class="uni-dialog-content">
				<text class="uni-dialog-content-text">请在设置中开启摄像头权限和麦克风权限</text>
			</view>
			<view class="uni-dialog-button-group">
				<button class="uni-dialog-button uni-border-left" open-type="openSetting" @click="closeSetting">
					设置权限
				</button>
			</view>
		
		</view>
		<view class="qx_bg" :hidden="show_qx">
		</view>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				show_qx:true,
				videoType: 1,
				pageIndex: 1,
				isOver: false,
				videoName: '',
				videoList: [],
			}
		},
		onLoad(options) {
			this.videoType = options.videoType
			if (this.videoType == 2) {
				uni.setNavigationBarTitle({
					title: '短视频'
				});
			}
			this.getVideoList()
		},
		onPullDownRefresh() {
			this.pageIndex = 1
			this.videoList = []
			this.getVideoList('refresh')
		},
		methods: {
			closeSetting:function(){
				this.show_qx=true;
			},
			//  搜索直播
			search(data) {
				this.videoName = data.value
				this.videoList = []
				this.pageIndex = 1
				this.getVideoList()
			},
			//  懒加载
			handleLoadMore() {
				if (!this.isOver) {
					this.pageIndex++
					this.getVideoList()
				}
			},
			//  获取直播列表
			getVideoList(type) {
				NET.request((this.videoType == 1 ? API.getLiveTelecastList : API.getShortVideo) + '/' + this.pageIndex + '/10', {
					liveName: this.videoName,
					videoName: this.videoName,
				}, 'GET').then(res => {
					if (type == 'refresh') {
						uni.stopPullDownRefresh();
					}
					this.isOver = res.data.list.length != 10
					this.videoList = this.videoList.concat(res.data.list)
				}).catch(res => {
					this.$refs.uTips.show({
						title: '获取直播列表失败',
						type: 'warning',
					})
				})
			},
			//  跳转直播详情
			goToVideoDetail(item) {
				if (this.videoType != 1) {
					uni.setStorage({
						key: 'videoUrl',
						data: item.mediaUrl
					})
				}else{
					uni.setStorage({
						key: 'liveImgUrl',
						data: item.imgUrl
					})
					uni.setStorage({
						key: 'liveName',
						data: item.liveName
					})
				}
				if(this.videoType == 1){
					var that=this;
					if(item.liveStatus==2){
						this.$refs.uTips.show({
							title: '直播未开始',
							type: 'warning',
						})
						wx.requestSubscribeMessage({
						  tmplIds: ['y0X1cZfbEMg7HdTGN_bW8v7TKeI3M0CHSVTY1zStIXM'],
						  success(res) {
							if(res.y0X1cZfbEMg7HdTGN_bW8v7TKeI3M0CHSVTY1zStIXM=="accept"){
								var post_data={liveId:item.liveId,mid:uni.getStorageSync("userData").userId}
								post_data.isDelete=0;
								NET.request(API.subscribeLive, post_data, 'POST').then(res => {
									
								//this.goodsList = res.data
								}).catch(res => {
									
								})
							}else{
								console.log('444', res);
							}
						    //console.log('1111', res);
						  },
						  fail(res) {
						    console.log('2222', res);
						  }
						})
						return false;
					}
					if(uni.getStorageSync("firstTimeLiveUser")==""){
						uni.setStorage({
							key: 'firstTimeLiveUser',
							data: 1
						})
						uni.navigateTo({
							url: '/pagesGood/' + (that.videoType == 1 ? ('liveDetail?liveId=' + item.liveId + '&roomId=' + item.roomId) :
								('videoDetail?videoId=' + item.videoId))
						});
					}else{
						// wx.getSetting({
						//   success(res) {
						// 	/*if (!res.authSetting['scope.camera']){
						// 	  that.show_qx=false;
						// 	}else*/ 
						// 	if (!res.authSetting['scope.record']){
						// 	  that.show_qx=false;
						// 	}else{
								uni.navigateTo({
									url: '/pagesGood/' + (that.videoType == 1 ? ('liveDetail?liveId=' + item.liveId + '&roomId=' + item.roomId) :
										('videoDetail?videoId=' + item.videoId))
								});
						// 	}
						//   }
						// })
					}
				}else{
					uni.navigateTo({
						url: '/pagesGood/' + (this.videoType == 1 ? ('liveDetail?liveId=' + item.liveId + '&roomId=' + item.roomId) :
							('videoDetail?videoId=' + item.videoId))
					});
				}
				
			}
		},
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;

		.search-box {
			width: 100%;
			height: 58px;
			box-sizing: border-box;
			padding: 2px 8px 1px 8px;
		}

		.video-box {
			width: 100%;
			height: calc(100% - 58px) !important;
			overflow-y: auto;

			.video-col {
				float: left;
				background: #FFFFFF;
				box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.15);
				border-radius: 5px;
				width: calc(50% - 18px);
				margin-top: 4px;
				margin-bottom: 6px;
				padding-bottom: 10px;
				position: relative;

				.video-img {
					width: 100%;
					height: 170px;
					object-fit: cover;
					float: left;
				}

				.video-title {
					width: 100%;
					height: 18px;
					box-sizing: border-box;
					padding-left: 8px;
					float: left;
					font-size: 15px;
					font-family: PingFang SC;
					color: #333333;
					line-height: 18px;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					margin: 6px 0 2px 0;
				}

				.video-date {
					width: 100%;
					box-sizing: border-box;
					padding-left: 8px;
					height: 16px;
					float: left;
					font-size: 12px;
					font-family: PingFang SC;
					color: #52A63A;
					line-height: 16px;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}

				.video-mask {
					width: 100%;
					height: 100%;
					position: absolute;
					background-color: rgba(0, 0, 0, 0.3);
					border-radius: 5px;

					.video-mask-horn {
						width: 0;
						height: 0;
						border-bottom: 4px solid #52A63A;
						border-right: 4px solid transparent;
						position: absolute;
						right: -4px;
						top: 8px;
					}

					.video-mask-text {
						position: absolute;
						right: -4px;
						top: 12px;
						width: 52px;
						height: 18px;
						border-radius: 9px 0 0 9px;
						background: #52A63A;
						line-height: 18px;
						font-size: 10px;
						font-family: PingFang SC;
						color: #FFFFFF;
						text-align: center;
					}
				}
			}

			.video-col:nth-child(odd) {
				margin-left: 16px;
				margin-right: 2px;
			}

			.video-col:nth-child(even) {
				margin-left: 2px;
				margin-right: 16px;
			}
		}
	}
	.uni-popup-dialog {
		width: 80vw;
		border-radius: 15px;
		background-color: #fff;
		position: fixed;
		margin-left: 10vw;
		top: 34vh;
		z-index: 11;
	}
	
	.uni-dialog-title {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		padding-top: 15px;
		padding-bottom: 5px;
	}
	
	.uni-dialog-title-text {
		font-size: 16px;
		font-weight: 500;
	}
	
	.uni-dialog-content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 5px 15px 15px 15px;
		text-align: center;
	}
	
	.uni-dialog-content-text {
		font-size: 14px;
		color: #6e6e6e;
	}
	
	.uni-dialog-button-group {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		border-top-color: #f5f5f5;
		border-top-style: solid;
		border-top-width: 1px;
	}
	
	.uni-dialog-button {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		    background: rgba(0, 0, 0, 0);
		    border: none;
		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 45px;
	}
	
	.uni-border-left {
		border-left-color: #f0f0f0;
		border-left-style: solid;
		border-left-width: 0px;
	}
	
	.uni-dialog-button-text {
		font-size: 14px;
	}
	
	.uni-button-color {
		color: #007aff;
	}
	
	.uni-dialog-input {
		flex: 1;
		font-size: 14px;
	}
	
	.uni-popup__success {
		color: #4cd964;
	}
	
	.uni-popup__warn {
		color: #f0ad4e;
	}
	
	.uni-popup__error {
		color: #dd524d;
	}
	
	.uni-popup__info {
		color: #909399;
	}
	.qx_bg{
		width: 100vw;
		height: 100vh;
		background: rgba(0, 0, 0, 0.25);
		position: fixed;
		top: 0px;
		left: 0px;
		z-index: 10;
	}
</style>