<template>
	<view class="container">
		<web-view :src="pageUrl" v-if="pageUrl" update-title allow="fullscreen" @message="handleMessage"></web-view>
		<image v-if="false" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/app/newImages/empty_icon.png" mode="aspectFill"></image>
		<view>{{msg}}</view>
	</view>
</template>

<script>
	import {
		getRealLink,
		updateSopAppLink
	} from "@/api/courseAnswer.js"
	export default {
		data() {
			return {
				pageUrl:"",
				msg: '加载中...',
				userInfo: {}
			}
		},
		onLoad(option) {
			this.userInfo = uni.getStorageSync("userInfo") ? JSON.parse(uni.getStorageSync("userInfo")) : {}
			if (option.link) {
				this.getLink(option.link)
				this.getCourseRead(option.link)
			} else {
				this.msg = "link is not found"
				uni.showToast({
					title: "link is not found",
					icon: 'error'
				})
			}
		},
		onUnload() {
			// #ifdef APP-PLUS
			plus.navigator.setFullscreen(false);
			plus.screen.lockOrientation('portrait-primary');
			// #endif
		},
		onBackPress() {
			// #ifdef APP-PLUS
			// 返回时退出全屏
			plus.navigator.setFullscreen(false);
			plus.screen.lockOrientation('portrait-primary');
			// #endif
		},
		methods: {
			getCourseRead(id) {
				updateSopAppLink(id).then(res=>{
					if(res.code == 200) {
						this.$updateMsgDot();
					}
				})
			},
			handleMessage(event) {
				const info = event.detail.data[0]
				// #ifdef APP-PLUS
				if(info.login != 1) {
					this.$showLoginPage()
					return
				}
				if (info&&info.isFullscreen) {
					plus.screen.lockOrientation('landscape-primary');
				} else {
					plus.screen.lockOrientation('portrait-primary');
				}
				if(info&&info.productId) {
					uni.navigateTo({
						url: '/pages/user/integral/integralGoodsDetails?goodsId='+info.productId
					})
				}
				if(info&&info.pagesUrl) {
					uni.navigateTo({
						url: info.pagesUrl
					})
				}
				// #endif
			},
			getLink(sortLink) {
				this.msg = '加载中...'
				const userId = this.userInfo.userId || ''
				getRealLink({
					sortLink: sortLink
				}).then(res => {
					if (res.code == 200) {
						// 如果响应中包含真实链接，则跳转到真实链接
						this.pageUrl = res.realLink + "&userId="+userId+"&source=app&time=" + new Date().getTime()
						// console.log("this.pageUrl============",this.pageUrl)
					} else {
						this.msg = '课程已过期或链接无效'
						uni.showModal({
							title: '提示',
							content: '课程已过期或链接无效',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
				}).catch(err => {
					this.msg = '发生错误，请稍后再试'
					uni.showToast({
						title: '发生错误，请稍后再试',
						icon: 'none'
					});
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-bottom: 88rpx;
		justify-content: center;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 32rpx;
		color: #757575;
		line-height: 48rpx;
		text-align: center;

		image {
			width: 428rpx;
			height: 360rpx;
			margin-bottom: 30rpx;
		}
	}
</style>