<template>
	<view>
		<nav-bar title="模考讲解直播" class="nav"></nav-bar>
		<view class="tabs">
			<view class="tab" :class="{ active: index == 2 }" data-index="2">
				<view class="dateRange">
					<picker mode="date" :value="param.startTime" :end="endDate" @change="bindDateFromChange">
						<view class="uni-input">{{param.startTime || '开始时间'}}</view>
					</picker>
					- 
					<picker mode="date" :value="param.endTime" :end="endDate" @change="bindDateToChange">
						<view class="uni-input">{{param.endTime || '结束时间'}}</view>
					</picker>
				</view>
				<u-icon class="icon" name="calendar"></u-icon>
			</view>
			<view class="tab" :class="{ active: index == 1 }" data-index="1">
				<view class="dateRange">
					<picker class="picker" mode="selector" :range="listApplyName" range-key="applyName" :value="param.applyName" @change="bindTitleChange">
						<view style="width:300rpx;height:40rpx;">{{param.applyName}}</view>
					</picker>
				</view>
				<u-icon class="icon" :class="index ==1? 'animals':''" name="arrow-down"></u-icon>
			</view>
					
			
		</view>
		

		<view class="record">
			<view class="item" v-for="(item,index) in liveList" :key="index" @click="goLive(item)">
				<view class="item__header">
					<view class="note" :class="{blue:item.liveStatus == 1,green:item.liveStatus == 2,gray:item.liveStatus == 3}">
						<image v-if="item.liveStatus == 0" src="../static/living.png" mode="widthFix"></image>
						<image v-if="item.liveStatus == 1" src="../static/noliving.png" mode="widthFix"></image>
						<image v-if="item.liveStatus == 2" src="../static/playback.png" mode="widthFix"></image>
						{{item.liveStatus == 0? '直播中' : ''}}
						
						{{item.liveStatus == 1? '未开播' :''}}
						
						{{item.liveStatus == 2? '回放中' :''}}
						
						{{item.liveStatus == 3? '已结束' :''}}
					</view>
					<image class="img" src="/pages5/static/questionBank.png" ></image>
				</view>
				<view class="item__body">
					<view class="title">
						{{item.sectionName}}
					</view>
					<view class="desc">直播时间</view>
					<view class="desc">{{$method.timestampToTime(item.liveStartTime,false)}} - {{$method.timestampToTime(item.liveEndTime,false)}} </view>
				</view>
			</view>
		</view>
		

		<view class="modal" :style="{top:modalTop+'px'}" v-if="index == 1">
			<view class="content">
				<scroll-view scroll-y="true" style="height:100%">
					<view class="top" :class="activeIndex === 0 ? 'activesty' : ''" @click="testClick(3)">全部</view>
					<view class="list">
						<view class="item" :class="activeIndex == listItem.goodsId ? 'activesty' : ''" v-for="(listItem,listIndex) in list" :key="listIndex" @click="testClick(listItem)">
							{{ listItem.goodsName }}
						</view>
					</view>
				</scroll-view>
				
			</view>
			<view class="modal_wrap" @click="index = 0"></view>
		</view>
		
		<view class="notice_modal" :style="{top:modalTop+'px'}" v-if="index == 2">
			<view class="content">
				<scroll-view scroll-y="true" style="height:100%">
					<view class="top" :class="typeIndex === 0 ? 'activesty' : ''" @click="paperClick(3)">全部试卷类型</view>
					<view class="list">
						<view class="item" :class="typeIndex == listItem.paperId ? 'activesty' : ''" v-for="(listItem,listIndex) in list1" :key="listIndex" @click="paperClick(listItem)">{{ listItem.paperName }}</view>
					</view>
				</scroll-view>
			</view>
			<view class="modal_wrap" @click="index = 0"></view>
		</view>
		
	</view>
</template>

<script>
import { mapGetters } from 'vuex';
export default {
	data() {
		return {
			openAppoint:false,
			appointModal:false,
			noticeModal:false,
			index: 0,
			liveList:[],
			list: [],
			list1: [],
			recordList: [],
			listApplyName:[],
			goodsData: {},
			param: {
				pageNum: 1,
				pageSize: 10,
				startTime:'',
				endTime:'',
				applyName:'',
			},
			isRepeat:false,
			total: 0,
			activeIndex: 0,
			typeIndex:0,
			itemIndex:'',
			modalTop:0,
			endDate:'',
		};
	},
	computed: { ...mapGetters(['userInfo','config']) },
	onLoad(option) {
		this.endDate = this.$method.timestampToTime(new Date().getTime() / 1000).replace(/-/g,'/');
		
		this.mockApplyListMockLive();
		uni.getSystemInfo({
			success:(e) => {
				let info = uni.createSelectorQuery().select(".nav");
				info.boundingClientRect((navData) => { //data - 各种参数
				
					let info = uni.createSelectorQuery().select(".tabs");
					info.boundingClientRect((tabData) => { //data - 各种参数
						this.modalTop = navData.height + tabData.height
						console.log(navData) // 获取元素宽度
						console.log(tabData) // 获取元素宽度
					}).exec()
				}).exec()
			}
		})
	},
	onPullDownRefresh() {
		let that = this;
		this.liveList = [];
		this.param = {
			pageNum: 1,
			pageSize: 10
		};
		this.mockApplyListMockLive();
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 500);
	},
	onReachBottom() {
		if (this.liveList.length < this.total) {
			this.param.pageNum++;
			this.mockApplyListMockLive();
		}
	},
	onShow() {
		
	},
	methods: {
		goLive(item) {
			if(item.liveStatus == 0) { //直播中
				let uuid = new Date().valueOf() + ""
				// buyCourse 是否购买课程：1是 0否
				let encode = encodeURIComponent(this.config.hostLive+'/pages/live/index?token='+uni.getStorageSync('token')+'&userInfo='+JSON.stringify(this.userInfo)+
				'&channelId='+item.liveUrl+'&buyCourse=1'+'&ident='+uuid)
				uni.navigateTo({
					url:`/pages5/webview/index?url=`+encode
				})
			} else if(item.liveStatus == 1) { //未开播
				uni.showToast({
					icon:'none',
					title:'暂未开播，请在开播后查看'
				})
			} else if(item.liveStatus == 2) { //回放中
				uni.navigateTo({
					url:'/pages3/live/playback?vid='+item.recordingUrl
				})
			} else if(item.liveStatus == 3) { //已结束
				uni.showToast({
					icon:'none',
					title:'直播已结束，不能查看回放'
				})
			}
		},
		mockApplyListMockLive() {
			let param = JSON.parse(JSON.stringify(this.param))
			if(param.endTime) {
				param.endTime = this.$method.TimeTotimestamp(param.endTime)
			}
			
			if(param.startTime) {
				param.startTime = this.$method.TimeTotimestamp(param.startTime)
			}
			this.$api.mockApplyListMockLive(param).then(res => {
				this.liveList.push(...res.data.rows)
				this.total = res.data.total;
			})
		},
		refreshByIndex() {
			this.$api.examRecordGroupList({
				pageNum: this.itemIndex+1,
				pageSize: 1
			}).then(res => {
				this.$set(this.recordList,this.itemIndex,res.data.rows[0])
				this.itemIndex = ''
			});
		},
		
		appoint(item) {
			this.appointModal = true;
		},
		tab(index) {
			if(this.index == index) {
				this.index = 0;
				return;
			}
			this.index = index;
		},
		
		mockApplyListApplyName() {
			let param = JSON.parse(JSON.stringify(this.param));
			if(param.endTime) {
				param.endTime = this.$method.TimeTotimestamp(param.endTime)
			}
			
			if(param.startTime) {
				param.startTime = this.$method.TimeTotimestamp(param.startTime)
			}
			this.$api.mockApplyListApplyName(param).then((res) => {
				this.listApplyName = res.data.rows;
				this.param.applyName = "";
				this.mockApplyListMockLive();
			});
		},

		bindDateFromChange(e) {
			this.param.startTime = e.detail.value
			this.param.pageNum = 1;
			this.param.applyName = '';
			this.liveList = []
			this.mockApplyListApplyName();
		},
		
		bindDateToChange(e) {
				this.param.endTime = e.detail.value
				this.param.pageNum = 1;
				this.param.applyName = '';
				this.liveList = []
				this.mockApplyListApplyName();
		},
		
		bindTitleChange(e) {
			if (this.listApplyName.length) {
				let index = e.detail.value;
				this.param.applyName = this.listApplyName[index].applyName
				
			} else {
				this.param.applyName  =''
			}
			this.param.pageNum = 1;
			this.liveList = []
			this.mockApplyListMockLive();
		}
	}
};
</script>
<style>
page {
	background: #eaeef1;
}
</style>
<style lang="scss" scope>
	.animals{
		transition: all 0.3s;
		transform: rotate(180deg);
	}
.tabs {
	position: fixed;
	padding:18rpx 0;
	left: 0;
	width: 100%;
	display: flex;
	z-index: 10;
	background: #ffffff;
	.tab {
		padding:0 18rpx;
		flex: 1;
		display: flex;
			align-items: center;
		justify-content: space-between;
		background: #ffffff;
		font-size: 24rpx;
		color: #999999;
		
		&:nth-of-type(1) {
			border-right:1px solid #EEEEEE;
		}
		
		.picker {
			width:100%;
			display: flex;
			align-items: center;
		}
		
		.dateRange {
			width:100%;
			display: flex;
			justify-content: space-between;
		}

		&.active {
			color: #333333;
			.icon{
				transform: rotate(180deg);
			}
		}
	}
}


.record {
	margin-top:80rpx;
	padding: 16rpx 16rpx;
	display: flex;
	flex-wrap: wrap;
	.item {
		margin-bottom: 16rpx;
		width:351rpx;
		background: #ffffff;
		border-radius: 16rpx;
		position: relative;
		
		&:nth-of-type(2n) {
			margin-left:16rpx;
			
		}

		&__header {
			width: 100%;
			height: 170rpx;
			position:relative;
			
			.note {
				position:absolute;
				left:0;
				top:0;
				width: 136rpx;
				height: 40rpx;
				background: linear-gradient(90deg, #B37FEB, #722ED1);
				box-shadow: 3rpx 3rpx 6rpx 0rpx rgba(239, 219, 255, 0.6);
				border-radius: 16rpx 0rpx 8rpx 0rpx;
				text-align: center;
				color:#fff;
				
				&.blue {
					background: linear-gradient(90deg, #40A9FF, #096DD9);
				}
				
				&.green {
					background: linear-gradient(90deg, #24F2C1, #00B9A0);
				}
				
				&.gray {
					background: linear-gradient(90deg, #BFBFBF, #8C8C8C);
				}
				
				image {
					width:24rpx;
					vertical-align: middle;
				}
			}
			
			.img {
				width:100%;
				height:100%;
			}
		}
		
		&__body {
			padding:16rpx;
			
			.title {
				font-size: 30rpx;
				color: #333333;
				line-height: 48rpx;
				font-weight: bold;
			}
			
			.desc {
				margin-top:10rpx;
				font-size: 24rpx;
				color: #666666;
				line-height: 36rpx;
			}
			
			.btn {
				margin-top:30rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				height: 64rpx;
				line-height: 62rpx;
				text-align: center;
				font-size: 28rpx;
					border: 1px solid #D9D9D9;
					color:#D9D9D9;
				
				&.active {
					
					border: 1px solid #007AFF;
					color:#007AFF;
				}
			}
		}
	}
}

.notice {
	position:fixed;
	right:0;
	top:50%;
	width: 48rpx;
	height: 200rpx;
	background: #52C41A;
	box-shadow: 0px 3rpx 16rpx 0rpx rgba(44, 121, 7, 0.5);
	border-radius: 24rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding:10rpx 0;
	
	.text {
		flex:1;
		color:#fff;
		text-align: center;
	}
}


.modal {
	bottom:0;
	z-index: 199999999;
	position: fixed;
	left: 0;
	width: 100%;

	.content {
		height:80%;
		overflow: hidden;
		position: relative;
		z-index: 10;
		background: #fff;
		padding: 8rpx 12rpx 20rpx;
		display: flex;
		flex-wrap: wrap;

		.top {
			margin: 0 auto;
			width: 726rpx;
			height: 80rpx;
			background: #f5f5f5;
			color: #666666;
			border-radius: 16rpx;
			text-align: center;
			line-height: 80rpx;
			font-size: 32rpx;
		}

		.list {
			width:100%;
			margin-top: 16rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.item {
				padding: 25rpx 20rpx;
				width: 49%;
				background: #f5f5f5;
				border-radius: 16rpx;
				font-size: 32rpx;
				color: #666666;
				margin: 8rpx 0;
			}
		}
	}

	.modal_wrap {
		position: absolute;
		left: 0;
		width: 100%;
		top: 0;
		height: 100%;
		background: rgba(0, 0, 0, 0.3);
	}
}
.activesty {
	background: #007aff !important;
	color: #fff !important;
}
</style>
