<template>
	<view class="container">
		<view class="conwarp">
			<view class="gdHead dffs">
				<view class="head flex1">{{ goodInfo.title }}</view>
				<text v-for="(item, index) in goodInfo.tags.split('|')" class="desc">{{ item }}</text>
			</view>
			<view class="subtit textgrey fontssm">{{ goodInfo.desc }}</view>
			<!-- -->
			<view class="cl-one"><video id='myVideo' class="vidBox" @timeupdate="timeupdate" @play="play"
					:initial-time="0" :src="goodInfo.files" controls></video></view>
		</view>
		<view class="conwarp">
			<view class="gdHead">
				<view class="head">课程简介</view>
			</view>
			<view class="cl-one">
				<view class="xqcont">
					<u-parse :content="goodInfo.content"></u-parse>
				</view>
			</view>
		</view>
		<view v-if="goodInfo.is_pay != 0 && goodInfo.my_is_pay == 0" class="fixBox dfsb">
			<view @tap="toPay()" class="li flex1 dffs">
				<image src="/static/img/pro/navlidown.png" mode=""></image>
				普通购买
			</view>
			<block v-if="goodInfo.is_vip == 1">
				<view class="line"></view>
				<view @tap="toPay()" class="li flex1 dffs">
					<image src="/static/img/pro/navlivdown.png" mode=""></image>
					会员购买
				</view>
			</block>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				id: 0, //商品id
				type: '',
				goodInfo: {
					tags: ''
				}
			};
		},

		onLoad(option) {
			this.id = option.id || '';
			this.type = option.type || '';
			this.loadData();
		},

		onReady: function(res) {
			this.videoContext = uni.createVideoContext('myVideo')
		},

		methods: {
			timeupdate(e) {
				console.log(parseInt(e.detail.currentTime));
				// is_pay  0 免费  1 付费  my_is_pay 0 未购买  1 已购买
				if (this.goodInfo.is_pay != 0 && this.goodInfo.my_is_pay != 1) {
					console.log(11);
					// is_vip/是否是会员:  0=否  1=是   see_time 可试看时长:  秒
					if (parseInt(e.detail.currentTime) >= parseInt(this.goodInfo.see_time)) {
						// e.detail.currentTime = 0;
						this.$api.toast('请购买后观看完整视频');
						setTimeout(res => {
							this.videoContext.pause()
							this.videoContext.seek(0);
						}, 800);
					}
				}
			},
			play(e) {
				let url = 'index/setSee';
				this.$api
					.request(url, {
						id: this.id
					})
					.then(res => {
						if (res.code != '200') {
							this.$api.toast(res.msg);
						}
					});
			},
			/**
			 * 请求数据只是为了代码不那么乱
			 * 分次请求未作整合
			 */
			async loadData() {
				let url = 'index/getVideoInfo';
				this.$api
					.request(url, {
						id: this.id
					})
					.then(res => {
						if (res.code == '200') {
							let data = res.data;
							this.goodInfo = data;
						} else {
							this.$api.toast(res.msg);
						}
					});
			},

			//去支付购买
			toPay() {
				uni.navigateTo({
					url: '/pages/pay/payVid?item=' + JSON.stringify(this.goodInfo)
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		.conwarp {
			background-color: #fff;
			padding: 26rpx 30rpx 39rpx;
			border-radius: 16rpx;
			margin-bottom: 20rpx;

			.gdHead {
				line-height: 58rpx;
				margin-bottom: 16rpx;

				.head {
					font-size: 34rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 600;
					color: #333333;
					line-height: 48rpx;
				}

				.desc {
					background: #f89638;
					border-radius: 5rpx;
					font-size: 18rpx;
					height: 32rpx;
					line-height: 32rpx;
					padding: 0rpx 8rpx;
					display: inline-block;
					color: #fff;
					margin-left: 10rpx;

					&:nth-of-type(2) {
						background: #7fc06e;
					}

					&:nth-of-type(3) {
						background: #75b2c4;
					}

					&:nth-of-type(4) {
						background: #8875c0;
					}
				}
			}

			.subtit {
				margin-top: 10rpx;
			}

			.cl-one {
				.vidBox {
					margin-top: 30rpx;
					width: 690rpx;
					height: 335rpx;
				}

				.xqcont {
					min-height: 800rpx;
					width: 100%;
					align-items: center;
					position: relative;

					.xqleft {
						position: absolute;
						top: 100rpx;
						right: 0;
						align-items: center;
						justify-content: center;

						.small {
							width: 54rpx;
							height: 54rpx;
						}

						.big {
							width: 108rpx;
							height: 108rpx;
						}

						.word {
							margin: 8rpx 0 48rpx;
							font-size: 20rpx;
							color: #999;
						}
					}

					.btnsBox {
						align-items: center;

						.small {
							width: 90rpx;
							height: 90rpx;
							margin: 60rpx 0 16rpx;
							opacity: 0.8;
						}

						.btns {
							margin: 70rpx 60rpx 0;
							width: 154rpx;
							height: 54rpx;
							background: #f6f6f6;
							border-radius: 31px;
							align-items: center;
							padding: 0 15rpx;
							justify-content: space-around;

							.icon {
								width: 35rpx;
								height: 31rpx;
							}

							.word {
								font-size: 22rpx;
								color: #666;
						}
							}
					}
				}
			}
		}

		.fixBox {
			padding: 34rpx;

			.line {
				width: 1rpx;
				height: 20rpx;
				background: #b7b7bd;
			}

			.li {
				justify-content: center;
				color: #7fc06e;

				image {
					width: 42rpx;
					height: 32rpx;
					margin-right: 20rpx;
				}
			}
		}
	}
</style>
