<template>
	<view class="app">
		<view class="" style="height: 20rpx;width: 100%;background-color: #f6f6f6;">
		</view>
		<view class="title">
			<!-- 家庭医生迎来重大版本升级 -->
			{{currentItem.title?currentItem.title:title}}
		</view>
		<view class="time">
			{{currentItem.sendTime}}
		</view>
		<view class="" style="height: 2rpx;width: 100%;background-color: #f6f6f6;">
		</view>
		<video v-if="videoId" id="myVideo" @timeupdate="videoFun" :src="messageNo" :initial-time="initial_time"
			@ended='ended' />
		<view class="text">
			<text>{{currentItem.content}}</text>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		videoadd
	} from '@/api/pagesC/noticedetails/index.js'
	import baseurl from '@/api/baseurl.js'
	export default {
		data() {
			return {
				currentItem: '',
				title: '',
				messageNo: null,
				video_real_time: 0, //实时播放进度
				nitial_time: '', //视频跳转进度 秒
				actId: '',
				cardNo: uni.getStorageSync('userinfo').cardNo,
				videoId: null,
			};
		},
		onReady() { //更改导航栏文字
			uni.setNavigationBarTitle({
				title: this.title,
			});
		},
		onLoad(options) {
			this.initial_time = '0' //视频进度
			this.title = options.title
			this.currentItem = JSON.parse(options.item)
			if (options.messageNo) {
				this.messageNo = baseurl + `/applet/message/preview/${options.messageNo}`
			}
			if (options.actId) {
				this.actId = options.actId
				this.videoId = options.videoId
				if (!this.videoId) {
					setTimeout(() => {
						this.ended();
					}, 5000)
				}
			}
		},
		methods: {
			ended() {
				videoadd(this.cardNo, this.actId).then(res => {
					if (res.code == 200) {
						this.$refs.uToast.show({
							title: '已为您增加积分',
							type: 'success',
						})
					}
				})
			},
			videoFun(e) {
				var isReady = 1; // 是否开启可以视频快进 1 禁止开启
				//跳转到指定播放位置 initial-time 时间为秒
				let that = this;
				//播放的总时长
				var duration = e.detail.duration
				//实时播放进度 秒数
				var currentTime = parseInt(e.detail.currentTime)
				//当前视频进度
				// console.log("视频播放到第" + currentTime + "秒")//查看正在播放时间，以秒为单位
				if (that.video_real_time == 0) {
					var jump_time = parseInt(that.initial_time) + parseInt(that.video_real_time)
				} else {
					var jump_time = parseInt(that.video_real_time)
				}
				if (isReady == 1) {
					if (currentTime > jump_time && currentTime - jump_time > 3) {
						let videoContext = wx.createVideoContext('myVideo')
						videoContext.seek(that.video_real_time)
						wx.showToast({
							title: '未完整看完该视频，不能快进',
							icon: 'none',
							duration: 2000,
						})
					}
				}
				that.video_real_time = currentTime //实时播放进度
			}
		}
	}
</script>

<style lang="scss">
	.app {
		#myVideo {
			width: 100%;
		}

		.text {
			font-size: 24rpx;
			line-height: 40rpx;
			letter-spacing: 1rpx;
			color: #333333;
			margin-left: 30rpx;
			margin-top: 20rpx;

			text {
				display: block;
			}
		}

		.time {
			margin-left: 30rpx;
			font-size: 22rpx;
			line-height: 50rpx;
			color: #999999;
		}

		.title {
			margin-left: 30rpx;
			font-size: 32rpx;
			margin-top: 20rpx;
			line-height: 80rpx;
			color: #333333;
		}
	}
</style>