<template>
	<view>
		<uni-popup ref="popupRef" :animation="false" :is-mask-click="false" type="top"
			mask-background-color="rgba(255, 255, 255, 0.6);" class="ezy-popup-width-all">
<!--      <view class="ezy-dajx-page" :style="{backgroundImage: 'url(' + courseBjFun() + ')'}">-->
        <view class="ezy-dajx-page">
				<view class="icon-title-navBar-box">
					<view @click="handleBack" class="nav-bar-icon"></view>
					<view class="nav-bar-title">单元测试</view>
				</view>
				<view class="shiti-frame-box">
					<view class="dajx-content-box">
						<!-- 思路分析 -->
						<view class="slfx-title"></view>
						<!-- <view class="slfx-content"> {{data.answer}}</view> -->
						<rich-text class="slfx-content" :nodes="data.answer"></rich-text>

						<!-- 视频讲解 -->
						<view class="spjj-title"></view>
						<view class="ezy-video-box">
							<!-- 这里放视频 ↓ -->
							<view class="ezy-video" ref="videoContent" id="wgy-player-test" :playAuth="playAuth"
								:change:playAuth="renderScript.receiveMsg" :videoId="videoId"
								:change:videoId="renderScript.videoIdFun" :hideFlag="hideFlag"
								:change:hideFlag="renderScript.hideFlagFun" :seekTime="seekTime"
								:change:seekTime="renderScript.seekTimeFun">
							</view>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popupRef2" :animation="false" :is-mask-click="false" type="top"
			mask-background-color="rgba(51, 137, 217, 0.65);">
			<view class="course-finish-dialog">
				<view class="text-score">{{credit}}</view>
				<view class="course-btn-box">
					<view @click="goBack" class="return-btn"></view>
					<view @click="goNext" class="continue-btn"></view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
	import {
		onLoad,
		onReady
	} from '@dcloudio/uni-app';
	import {
		getVideoAuth,
	} from "@/api/shipin.js"
	export default {
		props: {
			cardId: {
				type: [String, Number],
				default: 1
			}
		},
		data() {
			return {
				pageData: null, //上个页面获取的视频参数（视频id）
				playAuth: "", //播放凭证
				progressMarkers: [],
				videoId: "", //阿里云视频id
				seekTime: '',
				hideFlag: 'show',
				data: {},
			}
		},
		onHide() {
			console.log('onHideonHideonHideonHide');
			this.hideFlag = 'hide'
		},
		onUnload() {
			console.log('onUnloadonUnloadonUnloadonUnloadonUnload');
			this.hideFlag = 'hide'
		},
		methods: {
			playEnd(data) {
				console.log('data', data);
				console.log('111111');
				plus.screen.lockOrientation('portrait-primary');
				this.$refs.popupRef2.open();
			},
			goBack() {
				plus.screen.unlockOrientation();
				this.$refs.popupRef2.close();
				this.$refs.popupRef.close()
			},
			goNext() {
				plus.screen.unlockOrientation();
				this.$refs.popupRef2.close();
			},
			showPopup(item) {
				this.data = item;
				this.videoId = item.jiangjie
				//this.videoId = 'b997f16cb9cb474cb93526cff77d8801'
				//	this.progressMarkers = item.progressMarkers
				this.getLive(); //获取播放凭证
				this.$refs.popupRef.open()
			},
			handleBack(item) {
				this.$refs.popupRef.close()
			},
			getLive() {
				let req = {
					videoId: this.videoId
				}
				getVideoAuth(req).then(res => {
					this.playAuth = res.data
				})
			},
			markersClick(data) {
				this.seekTime = ""
				this.$nextTick(() => {
					this.seekTime = data.offset
				});
			},
			/*courseBjFun() {
				const cardId = Number(this.cardId);
				switch (cardId) {
					case 1:
						return 'static/images/course/couse-shuxue-bj.png'
						break;
					case 2:
						return 'static/images/course/course-yingyu-bj.png'
						break;
					default:
						break;
				}
			}*/

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

		},

	}
</script>



<script module="renderScript" lang="renderjs">
	export default {
		mounted() {
			console.log("renderScript1")
			// 在适合的生命周期，通过script和link标签引入播放器sdk、css
		},
		data() {
			return {
				player: null,
				playAuth: '',
				videoId: '',
				progressMarkers: [],
				isFullScreen: false,
				seekTime: '',
				isFirst: -1,
			}
		},
		methods: {
			receiveMsg(newValue, oldValue, ownerInstance, instance) {
				if (newValue) {
					this.playAuth = newValue
					this.loadWebPlayerSDK()
				}
			},
			videoIdFun(newValue, oldValue, ownerInstance, instance) {
				if (newValue) {
					this.videoId = newValue
				}
			},
			hideFlagFun(newValue, oldValue, ownerInstance, instance) {
				if (this.player) {
					this.player.pause()
				}

			},
			progressMarkersMsg(newValue, oldValue, ownerInstance, instance) {
				if (newValue) {
					this.progressMarkers = newValue
				}
			},
			seekTimeFun(newValue, oldValue, ownerInstance, instance) {
				if (newValue) {
					console.log(newValue, 'newValuenewValuenewValuenewValuenewValue')
					console.log(this.player.getStatus(), 'newValuenewValuenewValuenewValuenewValue')
					this.player.play()
					this.player.seek(newValue)
					//	this.player.seek(newValue)
				}
			},
			playAli() {
				if (this.isFirst == 0) {
					this.isFirst = -1
					return;
				}
				this.player = null;
				let that = this
				//配置播放器
				var player = new Aliplayer({
					id: "wgy-player-test",
					"vid": this.videoId,
					"playauth": this.playAuth,
					extraInfo: {
						poster: 'noposter'
					},
					fullscreenEvents: {
						fullscreenChange: (isFull) => {
							this.isFullScreen = isFull
						}
					},
					// "playConfig": {
					// 	"EncryptType": 'AliyunVoDEncryption'
					// },
					"skinLayout": [{
							"name": "bigPlayButton",
							"align": "blabs",
							"x": 30,
							"y": 80
						},
						{
							"name": "controlBar",
							"align": "blabs",
							"x": 0,
							"y": 0,
							"children": [{
									"name": "progress",
									"align": "blabs",
									"x": 0,
									"y": 44
								},
								{
									"name": "playButton",
									"align": "tl",
									"x": 15,
									"y": 12
								},
								{
									"name": "fullScreenButton",
									"align": "tr",
									"x": 10,
									"y": 12
								},
								{
									"name": "timeDisplay",
									"align": "tr",
									"x": 10,
									"y": 5
								}
							]
						}
					],
					"qualitySort": "asc",
					"format": "mp4",
					"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();

				});
				player.on('ended', function(data) {
					that.exitFullScreen();
					that.$ownerInstance.callMethod('playEnd', {
						data: 'end'
					})

				});
				this.isFirst++;
			},
			exitFullScreen() {
				if (document.exitFullscreen) {
					document.exitFullscreen(); // 标准方法
				} else if (document.mozCancelFullScreen) { // Firefox
					document.mozCancelFullScreen();
				} else if (document.webkitExitFullscreen) { // Chrome, Safari & Opera
					document.webkitExitFullscreen();
				} else if (document.msExitFullscreen) { // IE/Edge
					document.msExitFullscreen();
				}
			},

			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.playAli()
						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>