<template>
	<view class="page_body">
		<!-- 头部 -->
		<nav-bar height="96" title="视频详情" :onlyTitle="true" :showHeaderborder="true" @GoBack="GoBack"></nav-bar>
		<view class="page_content">
			<view class="video_content">
				<!-- duration：指定视频时长，单位为秒（s） initial-time：指定视频初始播放位置，单位为秒（s） -->
				<video 
					id="myVideo"
					:title="state?.videoInfo?.courseName || ''"
					:initial-time="state?.videoInfo?.initialTime"
					:duration="state?.videoInfo?.duration"
					:show-mute-btn="true"
					:poster="state?.videoInfo?.videoCoverUrl"
					:src="state?.videoInfo?.attachmentUrl"
					style="width: 100%; height: 100%; object-fit: contain;"
					@timeupdate="videoTimeupdate"
				>
				</video>
			</view>
			<view class="video_title">{{state?.videoInfo?.courseName}}</view>
			<view class="video_progress">
				<text class="video_progress_text" v-if="state?.videoInfo?.courseType===0">必修任务：{{state.videoInfo.courseStatus===0?`剩余${state.videoInfo.remainingTime}`:'已结束'}}</text>
				<text class="video_progress_text">观看进度：{{state?.videoInfo?.progress}}</text>
			</view>
			<view class="video_class_name">
				<text class="class_name_item">{{state?.videoInfo?.teachCourseTypeName}}</text>
				<text class="class_name_item">{{state?.videoInfo?.teachCourseTypeClassName}}</text>
				<text class="class_name_item">{{state?.videoInfo?.createdBy}}</text>
			</view>
			<view class="video_time">
				<text class="time_item">{{state?.videoInfo?.courseType===0 ?'开始':'上传'}}时间：{{state?.videoInfo?.courseStartDatetime}}</text>
				<text class="time_item" v-if="state?.videoInfo?.courseType===0">结束时间：{{state.videoInfo.courseEndDatetime}}</text>
			</view>
			<view class="video_desc">
				课程简介：{{state?.videoInfo?.courseContent}}
			</view>
		</view>
	</view>
	<!-- 提示框 -->
	<popup-dialog ref="popupDialogRef" popType="custom" title="试题弹出" :showDialogClose="false" :showCloseBtn="false" :showFootBorder="true" dialogWidth="702" @DialogConfirm="DialogConfirm">
		<view class="dialog_questions_input">
			<view class="questions_title">{{state?.question?.codeName}}：{{state?.question?.questionsName}}</view>
			<uni-easyinput type="textarea" v-model="value" :style="state.easyinputStyle" placeholder="请输入你的回答" placeholderStyle="font-weight: 400;font-size: 28rpx;color: #999999;"></uni-easyinput>
		</view>
	</popup-dialog>	
</template>

<script setup>
	import uniEasyinput from '@/uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue';
	import navBar from '@/components/nav-bar.vue';
	import popupDialog from '@/components/popup-dialog.vue';
	import { selectCourseDetail } from '@/reqApi/teacherStudy.js';
	import {
		onLoad,
		onUnload
	} from '@dcloudio/uni-app';
	import {
		reactive,
		ref
	} from 'vue';
	const state = reactive({
		id: '', //视频id
		videoInfo:{},
		currentTime:'',//播放进度时分秒
		question:null,//试题弹出信息
		easyinputStyle:{
			color: '#303133',
			borderColor: '#E9E9E9'
		}
	});
	//提示框
	const popupDialogRef = ref(null);
	const videoCtx = ref(null);//播放器
	onLoad((option) => {
		state.id = option.id;
		videoCtx.value = uni.createVideoContext('myVideo')
		GetCourseDetail();
	})
	//视频详情
	const GetCourseDetail = () => {
		selectCourseDetail(state.id).then(res => {
			if(res.code == 200){
				state.videoInfo = res.data;
				state.videoInfo.initialTime = timeToSecond(state.videoInfo.courseLearnTime || '');
				state.videoInfo.duration = timeToSecond(state.videoInfo.videoTime || '')
			}
		})
	}
	//计算秒数
	const timeToSecond = (timeStr) => {
		const timeArr = timeStr.split(':');
		const [h, m, s] = timeArr.map(item => Number(item));
		return h * 3600 + m * 60 + s;
    }
	//播放进度变化时触发，event.detail = {currentTime, duration} 。触发频率 250ms 一次
	const videoTimeupdate = (event) => {
		const currentTime = event.detail.currentTime;
		state.currentTime = formatSecondsToHms(currentTime);
		const questionsList = state?.videoInfo?.questionsList || [];
		const question = questionsList.find(item=>item.showTime == state.currentTime);//视频显示的时间
		state.question = question;//试题弹框信息
		if(question){
			videoCtx.value?.pause();//暂停播放
			popupDialogRef.value.OpenDialog();
		}
		console.log(state.currentTime,323)
	}
	// 数字补零工具
	const addZero = (num) => {
	  return num < 10 ? '0' + num : String(num);
	}
	//转换时分秒
	const formatSecondsToHms = (seconds) => {
	  let total = Math.floor(seconds);
	  const h = Math.floor(total / 3600);
	  const m = Math.floor((total % 3600) / 60);
	  const s = total % 60;
	
	  const hh = addZero(h);
	  const mm = addZero(m);
	  const ss = addZero(s);
	  return `${hh}:${mm}:${ss}`;
	}
	//页面销毁时触发
	onUnload(()=>{
		
	})
	//返回
	const GoBack = () => {
		uni.navigateBack({
			delta: 1
		});
	}
</script>

<style scoped lang="scss">
	.video_content {
		width: 100%;
		border-radius: 8rpx;
		margin-top: 24rpx;
		height: 398rpx;
		overflow: hidden;
	}
	.video_title{
		width: 100%;
		margin-top: 32rpx;
		font-weight: 500;
		font-size: 36rpx;
		color: #333333;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	.video_progress{
		display: flex;
		justify-content: space-between;
		width: 100%;
		margin-top: 16rpx;
		.video_progress_text{
			font-weight: 400;
			font-size: 24rpx;
			color: #2E64FA;
		}
	}
	.video_class_name{
		display: flex;
		width: 100%;
		margin-top: 16rpx;
		gap: 16rpx 24rpx;
		flex-wrap: wrap;
		.class_name_item{
			font-weight: 400;
			font-size: 24rpx;
			color: #666666;
		}
	}
	.video_time{
		display: flex;
		width: 100%;
		margin-top: 16rpx;
		gap: 16rpx;
		flex-wrap: wrap;
		.time_item{
			font-weight: 400;
			font-size: 24rpx;
			color: #666666;
		}
	}
	.video_desc{
		display: flex;
		width: 100%;
		margin-top: 32rpx;
		flex-wrap: wrap;
		word-break: break-all;
		font-weight: 400;
		font-size: 24rpx;
		color: #999999;
		line-height: 44rpx;
	}
	.dialog_questions_input{
		padding: 40rpx 30rpx;
		display: flex;
		width: 100%;
		flex-direction: column;
		.questions_title{
			display: flex;
			width: 100%;
			font-weight: 400;
			font-size: 32rpx;
			color: #303133;
			text-align: left;
			word-break: break-all;
		}
		:deep(.uni-easyinput){
			margin-top: 20rpx;
			font-size: 28rpx;
			.is-input-border{
				border-radius: 8rpx;
				border-color: #E9E9E9 !important;
			}
			.uni-easyinput__content-textarea{
				height: 440rpx;
				min-height: 440rpx;
				font-size: 28rpx;
				margin:20rpx 24rpx 20rpx 14rpx;
			}
			.input-padding{
				padding-left: 10rpx;
			}
		}
	}
</style>