<template>
	<view class="">
		<view class="navbar">
			<view class="back">
				<text class="iconfont icon-fanhui " @click="backLast()"></text>
			</view>
			<text>视频学习</text>
		</view>
		<view class="msg" style="border-radius:0 0 10px 10px ;">
			<!-- <video src="" style="width: 100%;"></video> -->
			<video id="myVideo" :src="videoUrl" :initial-time="initialTime" @error="videoErrorCallback"
				@timeupdate="timeupdate" @play="videoPlay" @pause="videoPause" :title="videoTitle" :autoplay="true"
				show-loading @ended="videoEnd" controls style="width: 100%;"></video>

			<view class="title"><text style="margin-right: 7px;">{{msg.title}}</text> <text class="tag"
					v-for="item1 in msg.labels" :key="item1">{{item1}}</text></view>
			<view class="view1">
				<text style="color: darkgrey;">视频数量： <text
						style="color: coral;font-weight:550">{{msg.video_nums}}</text></text>
				<text style="color: darkgrey;" v-if="msg.user_count>0"> <text
						style="color: coral;font-weight:550">{{msg.user_count}}</text>人已学习</text>
				<text style="color: darkgrey;" v-else>暂无学习</text>
			</view>
		</view>
		<view class="msg content">
			<view class="tab">
				<view class="tab-item" :class="currentTab==0 ? 'active-item' :''" @click="changeTab(0)">
					介绍
					<view class="line" v-if="currentTab==0">

					</view>
				</view>

				<view class="tab-item" :class="currentTab==1 ? 'active-item' :''" @click="changeTab(1)">
					目录
					<view class="line" v-if="currentTab==1">

					</view>
				</view>

			</view>
			<view class="desc" v-if="currentTab==0">
				<div v-html="msg.description"></div>
			</view>
			<view class="directory" v-if="currentTab==1">
				<view class="chapt" v-for="(item,index) in directoryList" :key="index">
					<view class="chapt-title">
						{{item.title}}
					</view>
					<view class="dirc-list" v-for="(video,index1) in item.children " :key="index1"
						:class="video.id==videoId ? 'active-video' :''" @click="changeNextVideo(video,index1)">
						<view class="icon"></view>
						<view class="right">
							<view class="title">
								{{video.title}}
							</view>
							<view class="minte">
								<view>{{video.duration_text}}</view>
								<view class="progress">进度：<text
										style="color: #fd6308;font-weight: 550;">{{video.progress}} % </text></view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var app = getApp()
	import navBar from '../../components/navBar/navbar.vue';
	import configData from '../../config.js'
	export default {
		data() {
			return {
				msg: '',
				currentTab: 1,
				directoryList: [],
				videoUrl: '', //当前视频列表
				videoId: '', //当前视频id
				currentVideo: '', //当前视频的序号
				videoList: [], //视频列表
				videoTitle: '', //当前视频的标题
				videoDuration: 0,
				currentMint: 0, //当前播放秒数防止重复发送，观看时长记录
				sendTime: 0, //视频观看进度发送间隔
				initialTime: 0, //当前视频播放秒数
			}
		},
		components: {
			navBar
		},
		onShow() {
			this.getSettings()
		},
		onLoad(options) {
			app.request('/course/show-detail?slug=' + options.slug, 'get').then(res => {
				console.log(res)
				this.msg = res.data.data.course
				this.directoryList = res.data.data.directory
				let file = res.data.data.directory
				let arr = file.filter(item => {
					return item.children.length > 0
				})
				if (arr.length == 0) {
					uni.showModal({
						title: '提示',
						content: '当前暂无可播放视频',
						showCancel: false,
					})
				} else {
					console.log(arr, '-----', arr1)
					let arr1 = []
					this.currentVideo = 0
					let indexProgress = [] //进度不为0 的下表
					arr.map(item => {
						item.children.map((item1, index) => {

							arr1.push(item1)

						})
					})
					arr1.map((item, index) => {
						if (item.progress == 0) {
							console.log(item, '---', index)
							indexProgress.push(index)
						}
					})
					console.log(indexProgress, 'indexProgressindexProgressindexProgress', arr1)
					if (indexProgress.length > 0) {
						this.currentVideo = indexProgress[0]
					} else {
						this.currentVideo = 0
					}
					this.videoList = arr1
					this.videoUrl = this.videoList[this.currentVideo].url_resource.url
					this.videoId = this.videoList[this.currentVideo].id
					this.videoTitle = this.videoList[this.currentVideo].title
					this.videoDuration = this.videoList[this.currentVideo].duration
					if (this.videoList[this.currentVideo].progress !== 100) {
						this.initialTime = this.videoDuration * this.videoList[this.currentVideo].progress
					}

				}
			})
			// app.request('/course/video?id=' + options.id, 'get').then(res => {
			// 	console.log(res)
			// })
			// app.request('/course/chapter?id=' + options.id, 'get').then(res => {
			// 	console.log(res)
			// })
		},
		methods: {
			getSettings() {
				app.request('/setting/keys', {
					keys: ['SENG_VIDEO_MSG'],

				}, 'post').then(res => {
					let data = res.data.data
					this.sendTime = data.SENG_VIDEO_MSG

				})
			},
			backLast: function() {
				uni.navigateBack()
			},
			//tab切换视频底部标签
			changeTab(e) {
				this.currentTab = e
			},

			//播放进度发生变化时
			timeupdate(event) {
				let currentTime = event.detail.currentTime
				if (currentTime) {
					let min = currentTime.toFixed(0)
					let yu = min % Number(this.sendTime)
					if (yu == 0 && min > 1 && this.currentMint !== min) {
						console.log(min, '-----', this.currentMint)
						this.currentMint = min
						this.postProgress(min)
					}
				}

			},
			//发送观看进度
			postProgress(currentTime) {
				let data = {
					course_id: this.msg.id,
					video_id: this.videoId,
					watch_seconds: currentTime
				}
				app.request('/course/video/look-record', data, 'post').then(res => {
					console.log(res, '观看进度')
				})
			},
			//点击切换视频
			changeNextVideo(item) {
				console.log(item, 'ppppp')
				let id = item.id
				this.videoId = item.id
				this.videoUrl = item.url_resource.url
				this.videoTitle = item.title
				this.videoDuration = item.duration
				if (item.progress !== 100) {
					this.initialTime = item.duration * item.progress
				}
				this.videoList.map((item1, index) => {
					if (item1.id == id) {
						this.currentVideo = index
					}
				})
			},
			//视频播放时触发
			videoPlay() {},
			//视频暂停播放
			videoPause() {},
			//视频播放结束
			videoEnd() {
				this.postProgress(this.videoDuration)
				setTimeout(() => {
					//播放结束自动播放下一节
					if (this.currentVideo == this.videoList.length - 1) {
						return
					} else {
						this.currentVideo++
						this.videoUrl = this.videoList[this.currentVideo].url_resource.url
						this.videoId = this.videoList[this.currentVideo].id
						this.videoTitle = this.videoList[this.currentVideo].title
						this.videoDuration = this.videoList[this.currentVideo].duration
					}
				}, 500)


			},
			//加载失败
			videoErrorCallback() {}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #EFF1F6;
	}

	.navbar {
		font-size: 32upx;
		height: 100upx;
		line-height: 100upx;
		color: #888888;
		position: relative;
		position: fixed;
		top: 0;
		z-index: 9999999;
		width: 100%;
		background-color: #FFFFFF;
		text-align: center;
		border-bottom: solid 2upx #EFF1F6;

		.back {
			height: 100upx;
			width: 100upx;
			text-align: center;
			position: absolute;
			float: left;
			left: 0upx;
			font-size: 32upx;
		}
	}

	.msg {
		margin-top: 100upx;
		background: white;
		padding-bottom: 10upx;

		.title {
			padding: 8px 0;
			font-weight: 550;
			color: #3e3e3e;
			font-size: 16px;
			padding: 0 30upx;
		}

		.tag {
			display: inline-block;
			border-radius: 3px;
			border: solid 1px #1c85f1;
			color: #1c85f1;
			font-size: 12px;
			padding: 0px 3px;
			margin-right: 5px;
			font-weight: normal;
		}

		.view1 {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30upx;
			padding-top: 15upx;
			padding-bottom: 10px;
			font-size: 26upx;
			color: #888888;
		}

		.text1 {
			padding-left: 30upx;
			padding-top: 10upx;
			font-size: 32upx;
			color: black;
			font-weight: bold;
		}
	}

	.content {
		margin-top: 30upx;
		border-radius: 10px 10px 0 0;
		min-height: 30vh;

		.desc {
			padding: 5px 10px 10px;
			font-size: 14px;
			color: #3e3e3e;
		}
	}

	.tab {
		display: flex;
		width: 50vw;
		margin: 0 auto;
		padding: 10px 0;

		.tab-item {
			flex: 1;
			color: #6b6b6b;
			text-align: center;
			position: relative;

			.line {
				height: 3px;
				background-color: #1c85f1;
				border-radius: 8px;
				// position: absolute;
				left: 10px;
				width: 25px;
				bottom: 0;
				margin: 0 auto;
				margin-top: 8px;
			}
		}

		.active-item {
			color: #000;
		}
	}

	.directory {
		padding: 0 30upx;

		.chapt-title {
			// background-color: #eff2fc;
			border-left: 4px solid #1c85f1;
			font-size: 26upx;
			font-weight: 500;
			padding: 5px;
			color: #484848;
			border-radius: 2px;
			margin-bottom: 5px;
		}

		.chapt {
			padding-bottom: 10px;
		}

		.dirc-list {
			display: flex;
			padding-left: 10px;


			.right {
				flex: 1;
				padding: 10px;
				border-bottom: solid 1px #EFF1F6;
				font-size: 13px;

				.title {
					padding: 0;
					font-size: 13px;
					font-weight: 400 !important;
					margin-bottom: 5px;
				}
			}

			.minte {
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			.progress {}
		}

		.active-video {
			.right {
				flex: 1;
				padding: 10px;
				border-bottom: solid 1px #EFF1F6;
				font-size: 13px;
				color: #1c85f1;

				.title {
					padding: 0;
					font-size: 13px;
					font-weight: 400 !important;
					margin-bottom: 5px;
					color: #1c85f1;
				}

			}

		}
	}
</style>
