<template>
	<view class="ezy-course-page">
		<view class="icon-title-navBar-box">
			<view @click="goUpPage" class="nav-bar-icon"></view>
			<text class="nav-bar-title">视频播放</text>
		</view>
		<view ref="videoContent" id="wgy-player-test" :playAuth="playAuth" :change:playAuth="renderScript.receiveMsg"
			:progressMarkers="progressMarkers" :change:progressMarkers="renderScript.progressMarkersMsg"
			:seekTime="seekTime" :change:seekTime="renderScript.seekTimeFun" class="course-video-box">
		</view>
		<view class="course-content-border">
			<view class="course-content-box">
				<view class="course-title">第一节 时间的计算</view>
				<view v-for="(item,index) in progressMarkers" :key="index" @click="markersClick(item)" class="course-content-item">
					<view class="course-play-btn"></view>
					<view class="course-content">
						<text>{{item.title}}</text>
						<text class="content-kcwz">课程位置：{{item.time}}</text>
					</view>
				</view>
			</view>
		</view>
		<uni-popup ref="popupRef" :animation="false" :is-mask-click="false"
		 mask-background-color="rgba(255, 255, 255, 0.6);">
			<view class="course-score-dialog">
				<view class="text-score">20</view>
				<view class="course-btn-box">
					<view class="return-btn"></view>
					<view class="continue-btn"></view>
				</view>
			</view>
		</uni-popup>
		
		
	</view>
</template>

<script>
	import { ref } from 'vue';
	import {
		onLoad,
		onReady
	} from '@dcloudio/uni-app';
	import {
		videoPolicy,
		getVideoAuth,
		getVideoRefresh
	} from "@/api/shipin.js"
	export default {
		data() {
			return {
				pageData: null, //上个页面获取的视频参数（视频id）
				playAuth: "", //播放凭证
				progressMarkers: [],
				videoId: "", //阿里云视频id
				seekTime: ''
			}
		},
		onLoad(options) {
			let data = JSON.parse(options.studyData)
			console.log(data);
			this.pageData = { ...data }
			console.log(this.pageData);
			this.videoId = data.videoId
			this.progressMarkers = data.progressMarkers
			this.getLive(); //获取播放凭证
		},
		methods: {
			getLive() {
				let req = {
					videoId: this.videoId
				}
				getVideoAuth(req).then(res => {
					this.playAuth = res.data
				})
			},
			markersClick(data) {
				console.log(data)
				this.seekTime = data.offset
			},
			goUpPage(){
				// this.$refs.popupRef.open();
				uni.navigateTo({
				//	url: `/pages/study/index?xueqi=${data.activeXueqi}&nianji=${data.activeNianji}&flag=selectGrades`
					url: `/pages/study/index?xueqi=${this.pageData.xueqi}&nianji=${this.pageData.nianji}&zhangId=${this.pageData.zhangId}`
				
				})
			}

		},
		created() {
			// console.log("getLive")
			// this.getLive(); //获取播放凭证

		},

	}
</script>



<script module="renderScript" lang="renderjs">

	export default {
		mounted() {
			console.log("renderScript1")
			// 在适合的生命周期，通过script和link标签引入播放器sdk、css
			this.loadWebPlayerSDK()
		},
		data() {
			return {
				player: null,
				playAuth: '',
				progressMarkers: [],
				seekTime: ''

			}
		},
		methods: {
			open(){
				
			},
			receiveMsg(newValue, oldValue, ownerInstance, instance) {
				console.log('service层中的options发生变化')
				console.log('新值', newValue)
				console.log('旧值', oldValue)
				// ownerInstance和this.$ownerInstance一样，可用来向service层通信
				// instance和ownerInstance的区别是：
				// instance.$el指向的是触发事件的那个节点；ownerInstance.$el指向当前vue文件中的根节点；
				// instance的作用目前尚不明确，官方没有给出用法
				if (newValue) {
					this.playAuth = newValue
					this.playAli()
				}
			},
			progressMarkersMsg(newValue, oldValue, ownerInstance, instance) {
				console.log('service层中的options发生变化222222222222')
				console.log('新值', newValue)
				console.log('旧值', oldValue)
				// ownerInstance和this.$ownerInstance一样，可用来向service层通信
				// instance和ownerInstance的区别是：
				// instance.$el指向的是触发事件的那个节点；ownerInstance.$el指向当前vue文件中的根节点；
				// instance的作用目前尚不明确，官方没有给出用法
				if (newValue) {
					this.progressMarkers = newValue
				}
			},
			seekTimeFun(newValue, oldValue, ownerInstance, instance) {
				console.log('service层中的options发生变化3333333')
				console.log('新值', newValue)
				console.log('旧值', oldValue)
				// ownerInstance和this.$ownerInstance一样，可用来向service层通信
				// instance和ownerInstance的区别是：
				// instance.$el指向的是触发事件的那个节点；ownerInstance.$el指向当前vue文件中的根节点；
				// instance的作用目前尚不明确，官方没有给出用法
				if (newValue) {
					console.log(newValue, 'newValuenewValuenewValuenewValuenewValue')
					console.log(this.player.getStatus(), 'newValuenewValuenewValuenewValuenewValue')
					this.player.play()
					this.player.seek(newValue)
					// switch (this.player.getStatus()) {
					// 	case 'init':
					// 		break;
					// 	case 'ready':
					// 		break;
					// 	case 'loading':
					// 	this.player.play()
					// 	this.player.seek(newValue)
					// 		break;
					// 	case 'play':
					// 		this.player.seek(newValue)
					// 		break;
					// 	case 'pause':
					// 	this.player.play()
					// 	this.player.seek(newValue)
					// 		break;
					// 	case 'playing':
					// 	this.player.seek(newValue)
					// 		break;
					// 	case 'waiting':
					// 		break;
					// 	case 'error':
					// 		break;
					// 	case 'ended':
					// 		break;
					// 	default:
					// 		break;
					// }
					//	this.player.seek(newValue)
				}
			},
			playAli() {
				//配置播放器
				var player = new Aliplayer({
					id: "wgy-player-test",
					"vid": 'b997f16cb9cb474cb93526cff77d8801',
					"playauth": this.playAuth,
					// "playConfig": {
					// 	"EncryptType": 'AliyunVoDEncryption'
					// },
					"qualitySort": "asc",
					"format": "m3u8",
					"mediaType": "video",
					"encryptType": 1,
					"progressMarkers": this.progressMarkers,
					"width": '100%',
					"height": '500px',
					"autoplay": false,
					"isLive": false,
					"rePlay": false,
					"playsinline": true,
					"preload": false,
					"controlBarVisibility": "hover",
					"useH5Prism": true

				}, function(player) {});
				this.player = player;
				player.one('canplay', function() {
					console.log('canplay', this.player.tag);
					player.tag.play();

				});
			},


			loadWebPlayerSDK() {
				return new Promise((resolve, reject) => {
					const s_tag = document.createElement('script'); // 引入播放器js
					s_tag.type = 'text/javascript';
					s_tag.src = 'https://g.alicdn.com/de/prismplayer/2.9.6/aliplayer-min.js';
					s_tag.charset = 'utf-8';
					s_tag.onload = () => {
						//	this.checkValue();
						resolve();
					}
					document.body.appendChild(s_tag);
					const l_tag = document.createElement('link'); // 引入播放器css
					l_tag.rel = 'stylesheet';
					l_tag.href =
						'https://g.alicdn.com/de/prismplayer/2.9.6/skins/default/aliplayer-min.css';
					document.body.appendChild(l_tag);
				});
			},
		}
	}
</script>