<template>

	<view class="container">
		<view class="shareheader" style="">
			<view class="iconfont icon-youjiantou"  @click="goBack()">
			</view>
			<view class="share-entrance">
				<view style="z-index: 99999;">
					<uni-popup ref="share" type="top" safeArea backgroundColor="#fff" :maskClick='true' @change="maskClick">
						<uni-popup-share @select="sharechange" ></uni-popup-share>
					</uni-popup>
				</view>
				<!-- 	<img @click="reinvite"  src="../../static/fastentry/Slice122.png" alt="">
					<img @click="shareToggle" src="../../static/fastentry/Slice12.png" alt=""> -->
					<view class=""  style="width: 50rpx;height: 50rpx;padding-right: 20rpx;">
						<image class="image" @click="reinvite"  src="../../static/fastentry/Slice122.png" mode="" ></image>
						
					</view>
					<view class=""  style="width: 50rpx;height: 50rpx; ">
						<image class="image" @click="shareToggle"  src="../../static/fastentry/Slice12.png" mode=""></image>
					</view>
			</view>
		</view>
		<!-- 课程banner图 -->
		 <!-- v-show="!sliceshare" -->
		<view class="courseBannerBox" v-show="!sliceshare">
			<video id="myVideo" :src="courseInfo.filePathOss" :initial-time="videoPlaybackInfo.maxViewTime"
				object-fit="contain" class="videoBox" :poster="courseInfo.fileFirstImage" :title="courseInfo.fileTitle"
				@loadedmetadata="loadedmetadata" @play="playVideo" @pause="pause" @ended="ended"
				@timeupdate="timeupdate"></video>
		</view>
		<!-- 课程详情图 -->
		<view class="courseTitleContent">
			<view class="courseTitle">
				<h4>{{courseInfo.fileTitle}}</h4>
				<view class="shareF">
					<view></view>
					<!-- <view>送好友</view> -->
				</view>
			</view>
			<view class="dataBox">
				<strong v-if="courseInfo.status==1">￥{{courseInfo.coursePrice}}</strong>
				<text v-if="courseInfo.status==2" style="color: #F15A1F;margin-right: 20rpx;"><i
						class="iconfont icon-yifukuan"></i>已购</text>
				<text>{{courseInfo.salesNumber}}人购买</text>
			</view>
		</view>
		<!-- 课程介绍，相关课程，讲师 -->
		<view class="courseProBox">
			<!-- tab区域 -->
			<view class="tab">
				<text :class="{'actived': tabType===1}" @click="switchTab(1)">详情</text>
				<text :class="{'actived': tabType===2}" @click="switchTab(2)">相关课程</text>
				<text :class="{'actived': tabType===3}" @click="switchTab(3)">讲师</text>
			</view>
			<!-- tabContent区域 -->
			<view class="tabContent">
				<!-- 课程介绍 -->
				<view class="courseIntroContent" v-if="tabType===1">
					<view v-html="courseInfo.fileIntroduce" class="richTextContent"></view>
				</view>
				<!-- 相关课程列表 -->
				<view class="relationCourseListsContent" v-show="tabType===2">
					<view class="totalCourseCount">
						<text v-if="requiredCount">{{requiredCount}}节必修</text>
						<text v-if="nonRequiredCount"> + {{nonRequiredCount}}节必修</text>
					</view>
					<view class="courseItemContent" v-for="(item,index) in relatedCoursesLists" :key="item.fileId">
						<view class="courseInfoContent">
							<h4>{{index + 1}}.{{item.fileTitle}}<text
									class="courseType">{{item.fileType=='1' ? '必修' : '选修'}}</text></h4>
							<view class="timeContent">
								<view class="">
									{{secondsTransferPipe(Number(item.maxViewTime))}}/{{secondsTransferPipe(Number(item.courseTotalTime))}}
								</view>
							</view>
						</view>
						<view class="statusBtnContent" @click="play(item)">
							<i class="iconfont icon-24gf-playCircle"
								:style="{'opacity':courseInfo.status == 1 ? '0.5' : '1'}"></i>
							<text>{{courseInfo.status == 1 ? '不可播放' : '播放'}}</text>
						</view>
					</view>
				</view>
				<!-- 讲师介绍 -->
				<view class="lecturerContent" v-if="tabType===3">
					<view class="lecturerIntro">
						<view class="lecturerUrl">
							<image :src="lecturerInfo.lecturerUrl" mode="widthFix"></image>
						</view>
						<view>
							<p><text class="lecturerName">{{lecturerInfo.lecturerName}}</text></p>
							<template v-if="lecturerInfo?.lecturerRankNames">
								<p v-for="item in lecturerInfo.lecturerRankNames.split(',')" class="lecturerTitle">
									<text>{{item}}</text></p>
							</template>
						</view>
					</view>
					<h4>
						<text>讲师介绍</text>
					</h4>
					<view v-html="lecturerInfo?.lecturerIntroduce" class="lecturerText richTextContent"></view>
				</view>
				<!-- 购买按钮 -->
				<view class="buyBox" @click="saveOrder()" v-if="courseInfo.status == 1">
					<text>{{loginType=='visitor' ? '登录/注册' : '购买'}}</text>
				</view>
			</view>
		</view>
		<!-- 分享信息 -->
		<view>
			<uni-popup ref="sharewx" type="bottom" backgroundColor="#fff">
				<view class="popup-content">
					<view class="shareBox">
						<view class="shareOptionItem" @click="uniShare(1)">
							<view class="imgBox">
								<image src="../../static/app-plus/sharemenu/wechatfriend.png" mode="widthFix"></image>
							</view>
							<text>微信好友</text>
						</view>
						<view class="shareOptionItem" @click="uniShare(2)">
							<view class="imgBox">
								<image src="../../static/app-plus/sharemenu/wechatmoments.png" mode="widthFix"></image>
							</view>
							<text>分享朋友圈</text>
						</view>
					</view>
				</view>
				<view class="popup-footer" @click="closeShare()">
					<view>取消</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import api from "../../api/api";
	import dataHandling from "@/util/dataHandling";
	export default {
		data() {
			return {
				fileId: null,
				lecturerId: null,
				tabType: 3,
				lecturerInfo: {},
				courseInfo: {},
				relatedCoursesLists: [],
				orderId: '',
				userId: uni.getStorageSync('cffp_userId'),
				shareUserId: '',
				shareCode: '',
				shareReadId: '',
				dataSource: '1',
				requiredCount: 0,
				nonRequiredCount: 0,
				videoPlaybackInfo: {},
				playbackId: '',
				totalTime: 0,
				auditionTime: 0,
				viewTime: 0,
				timer: null,
				loginType: '',
				sliceshare: false,
				nonRequiredCount: 0,
				requiredCount: 0,
				realName: ''
			};
		},
		onLoad(){
			let dataForm = uni.getStorageSync('userinfodataForm')
			this.realName = dataForm.realName
		},
		methods: {
			goBack() {
				console.log(12121232)
				uni.navigateBack(1)
			},
			// 监听空白关闭
			maskClick(val){
				this.sliceshare = val.show
			},
			// 打开微信分享啊
			reinvite() {
				this.$refs.sharewx.open()
			},
			// 关闭微信分享啊
			closeShare() {
				this.$refs.sharewx.close()
			},
			shareToggle() {
				this.sliceshare = true
				this.$refs.share.open()
			},
			sharechange(val){
				console.log(val, 423)
				uni.navigateTo({
					url:val.item.link
				})
			},
			uniShare(type) {
				if (type === 1) {
					uni.share({
						provider: "weixin",
						scene: "WXSceneSession",
						type: 0,
						href: "https://mdev.zuihuibi.cn/cffp/pages/invitationRegister/invitationRegister?shareId=" +
							this.shareId,
						title: "CFFP家庭财务策划师联盟邀您加入",
						summary: `我是家庭财务策划师${this.realName}正在使用CFFP财富中心，点击下载即刻加入！`,
						imageUrl: "https://mdev.zuihuibi.cn/cffp/static/cffp_logo.jpg",
						success: function(res) {
							console.log("success:" + JSON.stringify(res));
						},
						fail: function(err) {
							console.log("fail:" + JSON.stringify(err));
						}
					});
				} else if (type === 2) {
					uni.share({
						provider: "weixin",
						scene: "WXSceneTimeline",
						type: 0,
						href: "https://mdev.zuihuibi.cn/cffp/pages/invitationRegister/invitationRegister?shareId=" +
							this.shareId,
						title: "CFFP家庭财务策划师联盟邀您加入",
						summary: `我是家庭财务策划师${this.realName}正在使用CFFP财富中心，点击下载即刻加入！`,
						imageUrl: "https://mdev.zuihuibi.cn/cffp/static/cffp_logo.jpg",
						success: function(res) {
							console.log("success:" + JSON.stringify(res));
						},
						fail: function(err) {
							console.log("fail:" + JSON.stringify(err));
						}
					});
				}
			},
			// 订单保存
			saveOrder() {
				if (this.loginType === 'visitor') {
					uni.navigateTo({
						url: '/components/login/login'
					})
				} else {
					const param = {
						productType: '1',
						productId: this.courseInfo.fileId,
						userId: this.userId,
						shareUserId: this.shareUserId,
						shareReadId: this.shareReadId,
						shareCode: this.shareCode,
						dataSource: this.dataSource
					}
					api.saveOrder(param).then(res => {
						if (res['success']) {
							this.orderId = res['data']['id'];
							uni.navigateTo({
								url: `/pages/orderConfirm/orderConfirm?fileId=${this.fileId}&orderId=${this.orderId}`
							})
						} else {
							uni.showToast({
								title: res['message'],
								duration: 2000
							});
							return;
						}
					})
				}

			},
			// 切换tab
			switchTab(type) {
				this.tabType = type;
				if (type === 3) {
					this.lecturerQuery();
				} else if (type == 2) {
					this.relatedCoursesList()
				} else {
					this.courseDetail()
				}
			},
			// 讲师信息查询
			lecturerQuery() {
				api.lecturerQuery({
					id: this.lecturerId
				}).then(res => {
					console.log('讲师信息', res);
					if (res['success']) {
						this.lecturerInfo = res['data']['lecturerInfos'][0];
					} else {
						uni.showToast({
							title: res['message'],
							duration: 2000,
							icon: 'none'
						})
					}
				})
			},
			// 课程详情页面
			courseDetail() {
				api.courseDetail({
					fileId: this.fileId,
					userId: this.userId,
					packFileId: this.courseInfo.packFileId
				}).then(res => {
					if (res['success']) {
						this.courseInfo = res['data']['data'];
						this.lecturerId = res['data']['data']['fileLecturerId'];
						this.lecturerQuery();
						if (this.courseInfo.status === 2) {
							this.findVideoPlayback()
						}
					}
				})
			},
			// 相关课程查询
			relatedCoursesList() {
				api.relatedCoursesList({
					fileId: this.fileId,
					packFileId: this.courseInfo.packFileId
				}).then(res => {
					console.log('相关课程详情', res);
					if (res['success']) {
						this.relatedCoursesLists = res['data']['data']['relatedCourseList'];
						this.nonRequiredCount = res['data']['data']['nonRequiredCount'];
						this.requiredCount = res['data']['data']['requiredCount'];
					}
				})
			},
			// 秒转时分秒格式
			secondsTransferPipe(value) {
				return dataHandling.secondsTransferPipe(value)
			},
			play(item) {
				if (this.courseInfo.status == 1) {
					// 不可播放
					uni.showToast({
						title: '购买才可观看哦~',
						icon: 'none',
						duration: 2000
					});
				} else {
					this.playbackId = null;
					this.fileId = item.fileId;
					this.courseInfo.packFileId = item.packFileId;
					this.courseDetail()
				}
			},
			saveVideoPlayback() {
				// 视频播放轨迹保存
				const param = {
					id: this.playbackId ? this.playbackId : null,
					systemType: 1,
					userId: this.userId,
					fileId: this.fileId,
					packFileId: this.courseInfo.packFileId,
					totalTime: Math.floor(this.totalTime * 100) / 100,
					viewTime: Math.floor(this.viewTime * 100) / 100,
					playbackStatus: this.viewTime >= this.totalTime ? '2' : '1'
				}
				api.saveVideoPlayback(param).then(res => {
					if (res['success']) {
						this.playbackId = res['data']['id'];
					}
				})
			},
			findVideoPlayback() {
				// 查询视频播放最新记录
				const param = {
					systemType: 1,
					userId: this.userId,
					fileId: this.fileId,
					packFileId: this.courseInfo.packFileId
				}
				api.findVideoPlayback(param).then(res => {
					if (res['success']) {
						this.videoPlaybackInfo = res['data']
					}
				})
			},
			loadedmetadata(e) {
				this.totalTime = e.detail.duration;
			},
			playVideo(e) {
				// 开始/继续播放
				if (this.courseInfo.status === 2) {
					if (this.timer) {
						clearInterval(this.timer)
					}
					this.saveVideoPlayback();
					this.timer = setInterval(() => {
						this.saveVideoPlayback()
					}, 20 * 1000)
				}
			},
			pause() {
				// 暂停播放
				if (this.timer) {
					clearInterval(this.timer)
				}
				if (this.viewTime >= this.totalTime) {
					// 当播放完成的时候这里不触发
					return;
				} else {
					this.saveVideoPlayback();
				}
			},
			ended() {
				// 播放到末尾
				if (this.timer) {
					clearInterval(this.timer)
				}
				this.saveVideoPlayback();
			},
			timeupdate(e) {
				// 播放进度变化
				this.viewTime = e.detail.currentTime > this.totalTime ? this.totalTime : e.detail.currentTime;
				if (this.courseInfo.status === 1) {
					// 如果是试听，那么当观看时间大于等于试听时间的时候，强制暂停，并出弹窗
					if (this.viewTime >= this.courseInfo.auditionTime) {
						this.videoContext.pause();
						// // 不可播放
						uni.showModal({
							content: '购买之后才可继续播放哦~',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
						this.videoContext.seek(0);
					}
				}
			}
		},
		mounted() {
			this.switchTab(1);
			this.loginType = uni.getStorageSync('loginType')
			console.log( uni.getStorageSync('loginType'), 555)
		},
		onLoad(option) {
			this.fileId = option.fileId;
		},
		onReady() {
			this.videoContext = uni.createVideoContext('myVideo');
		},
		onUnload() {
			if (this.timer) {
				clearInterval(this.timer)
			}
		}
	}
</script>

<style lang="scss">
	.popup-content {
		font-size: 32rpx;
		color: #666;
		padding-bottom: 40rpx;
	
		.shareBox {
			display: flex;
			justify-content: space-around;
			align-items: center;
	
			.shareOptionItem {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
	
				.imgBox {
					width: 100rpx;
				}
			}
	
		}
	}
	.popup-footer {
		color: #666;
		font-size: 36rpx;
		border-top: 2rpx solid #f0f0f0;
		display: flex;
		justify-content: center;
		align-items: center;
		padding-top: 36rpx;
	}
	
	.courseBannerBox {
		height: 300rpx;
		background-color: #ebebeb;
		display: flex;
		justify-content: center;
		z-index: 1;
		.videoBox {
			max-width: 100%;
			height: 100%;
		}
	}

	.courseTitleContent {
		height: 150rpx;
		background-color: #fff;
		margin-bottom: 10rpx;
		padding: 20rpx 30rpx;
		box-sizing: border-box;

		.courseTitle {
			display: flex;
			justify-content: space-between;
			margin-bottom: 16rpx;

			h4 {
				font-size: 40rpx;
			}
		}

		.dataBox {
			strong {
				font-size: 30rpx;
				color: #F15A1F;
				margin-right: 30rpx;
			}

			text {
				font-size: 28rpx;
				color: #333;
			}
		}
	}

	.shareheader {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 60rpx;
		.icon-youjiantou{
			display: inline-block;
			transform: rotate(180deg);
			font-size: 30rpx;
			z-index: 1;
		}
		.share-entrance{
			width: 120rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.image{
				width: 50rpx;
				height: 50rpx;
				margin-top: 10rpx;
				margin-right: 25rpx;
			}
		}

	}

	.image {
		width: 30rpx;
		height: 30rpx;
	}

	.courseProBox {
		.tab {
			display: flex;
			justify-content: space-between;
			padding: 40rpx 30rpx 0;
			background-color: #fff;
			color: #666;
			font-size: 32rpx;

			text.actived {
				border-bottom: 2px solid #20269B;
				color: #333;
			}
		}

		.tabContent {
			padding-bottom: 200rpx;
			background-color: #fff;

			.statusBtnContent {
				display: flex;
				flex-direction: column;
				align-items: center;
				color: #999;
				font-size: 20rpx;

				.iconfont {
					font-size: 60rpx;
					color: #5359CD;
				}
			}

			.lecturerContent {
				.lecturerIntro {
					display: flex;
					padding: 40rpx 50rpx;
					border-bottom: 2rpx solid #fbfbfb;

					.lecturerUrl {
						width: 200rpx;
						height: 200rpx;
						border-radius: 50%;
						background-color: #ccc;
						overflow: hidden;
						margin-right: 40rpx;
					}

					.lecturerName {
						display: inline-block;
						background: #5359CD;
						border-radius: 10rpx;
						color: #fff;
						font-size: 28rpx;
						padding: 4rpx 20rpx;
					}

					.lecturerTitle {
						font-size: 28rpx;
						color: #5359CD;
						margin-top: 16rpx;
					}
				}

				h4 {
					padding-left: 30rpx;
					padding-top: 10rpx;
					padding-bottom: 20rpx;
					background-color: #fff;

					text {
						border-bottom: 1px solid #333;
						font-size: 30rpx;
					}
				}

				.lecturerText {
					padding: 0 28rpx;
					background-color: #fff;
					white-space: pre-wrap;
					width: 100%;
					box-sizing: border-box;
				}

			}

			.courseIntroContent,
			.relationCourseListsContent {
				padding: 0 28rpx;
				background-color: #fff;
				white-space: pre-wrap;
				width: 100%;
				box-sizing: border-box;
			}

			.relationCourseListsContent {
				.courseType {
					background-color: #5359CD;
					color: #fff;
					border-radius: 2rpx;
					padding: 2rpx 10rpx;
					font-size: 16rpx;
					margin-left: 10rpx;
				}

				.totalCourseCount {
					color: #20269B;
					font-size: 24rpx;
					padding: 8rpx 0;
					border: 1px solid #fbfbfb;
					border-left: none;
					border-right: none;
				}

				.courseItemContent {
					padding: 16rpx 0;
					border-bottom: 1px solid #fbfbfb;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.courseInfoContent {
						h4 {
							color: #666;
						}

						.timeContent {
							color: #999;
							padding-left: 28rpx;

						}
					}
				}

			}

			.buyBox {
				position: fixed;
				bottom: 0;
				left: 0;
				right: 0;
				border-radius: 80rpx;
				background-color: #20269B;
				color: #fff;
				font-size: 36rpx;
				padding: 20rpx 90rpx;
				text-align: center;
				max-width: 60%;
				margin: 0 auto;
			}
		}
	}
</style>
