<template>
	<view class="video-wrapper" id="video-container">
		<view style="position: relative;width: 100%; height: 300px;">
			<video :src="videoObj.videoUrl" @ended="videoEnded()" :autoplay="auto" style="width: 100%;height: 300px;"></video>
			<view class="question"></view>

			<view class="popup-layer-back" v-if="!start" v-show="question">
				<!-- <view class="popup-layer-close" @click="question=false">关闭</view> -->
				<view class="popup-layer">
					<h2>答案填充</h2>
					<p @click="nextQuestion(item.outs[0])" v-for="(item, index) in answerList">答题{{item.outs[0]}}后跳转下一视频</p>
				</view>
			</view>
			<view class="popup-layer-back" v-if="start">
				<view class="popup-layer" @click="nextQuestion(2)">
					<p>开始</p>
				</view>
			</view>
			<view class="popup-layer-back" v-if="end">
				<view class="popup-layer" @click="getBindJson()">
					<p>重新开始</p>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "s-video",
		data() {
			return {
				videoObjList: [],
				videoObj:{},
				answerList: [],
				start: true,
				end: false,
				auto: true,
				question: false,
			};
		},
		props:{
			jsonId: {
				type: String,
				default: "",
			}
		},
		created() {
			this.getBindJson();
		},
		methods: {
			videoEnded() {
				this.auto = false;
				this.question = true;
				// 出答案
				if(this.videoObj.outs.length>0) {
					for(var i=0; i<this.videoObj.outs.length; i++) {
						this.getAnswer(this.videoObj.outs[i])
					}
				} else {
					this.end = true;
					this.answerList = [];
					this.question = false;
				}
			},
			nextQuestion(outId) {
				this.hasNodeId(outId);
				this.videoObj.videoUrl = 'http://localhost:85/'+this.videoObj.videoUrl
				this.start = false;
				this.question = false;
				this.auto = true;
			},
			getBindJson() {
				this.start = true;
				this.end = false;
				this.videoObjList = [];
				this.$api.getBindJson(this.jsonId)
					.then(res => {
						this.videoObjList.push(...res.data);
						this.hasNodeId(1);
					}).catch((e) => console.log(e)); //捕获异常
			},
			hasNodeId(nodeId) {
				for(var i=0; i<this.videoObjList.length; i++) {
					if(nodeId == this.videoObjList[i].nodeId) {
						this.videoObj = this.videoObjList[i];
					}
				}
			},
			getAnswer(outId) {
				for(var i=0; i<this.videoObjList.length; i++) {
					if(outId == this.videoObjList[i].nodeId) {
						this.answerList.push(this.videoObjList[i]);
					}
				}
			}
		}
	}
</script>

<style>
	/* 首先设置背景层 */
	.popup-layer-back {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		z-index: 9998;
	}

	/* 然后设置弹出层 */
	.popup-layer {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		background-color: #fff;
		padding: 20px;
		z-index: 9999;
	}

	/* 设置关闭按钮 */
	.popup-layer-close {
		position: absolute;
		top: 5px;
		right: 5px;
		cursor: pointer;
	}
</style>