<template>
	<view class="pageBg">
		<!-- <topNavbar title="签约合同"></topNavbar> -->
		<view class="page-inner">
			<web-view :src="webViewUrl" :webview-styles="webviewStyles" :update-title="false"></web-view>
		</view>
	</view>
</template>

<script>
	import {
		getContractStatus,
		getSignContract
	} from '@/config/api.js'
	export default {
		data() {
			return {
				webViewUrl: '',
				schoolId: '',
				webviewStyles: {
					progress: {
						color: '#00a3ff'
					}
				}
			}
		},
		onReady() {
			// let wv;
			// // #ifdef APP-PLUS
			// let boxHeight = 0
			// uni.getSystemInfo({
			// 	success: res => boxHeight = res.windowHeight // 屏幕可用高度
			// })
			// var currentWebview = this.$scope.$getAppWebview() //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效
			// setTimeout(function() {
			// 	wv = currentWebview.children()[0]
			// 	wv.setStyle({top:statusBar,height: boxHeight})
			// }, 2000); //如果是页面初始化调用时，需要延时一下
			// // #endif
			// let statusBar = uni.getWindowInfo().statusBarHeight
			
		},
		onLoad(options) {
			this.schoolId = options.schoolId ? options.schoolId : this.vuex_userInfo.schoolId
			this.trainingApplyId = options.trainingApplyId ? options.trainingApplyId : ''
			// this.timer = setInterval(() => {
			// 	this.getContractStatusFn()
			// }, 3000)
			this.getSignContractFn()
			// #ifdef APP-PLUS
			this.shang()
			// #endif
		},
		methods: {
			shang() {
				let _this = this
				var currentWebview = this.$scope.$getAppWebview();
				setTimeout(() => {
					console.log('currentWebview')
					console.log(currentWebview)
					let wv = currentWebview.children()[0]
					wv.addEventListener('loaded', async function() {
						let webVeiwUrl = wv.getURL()
						console.log(webVeiwUrl)
						var reg = RegExp(/wait/i); //signaturecallback
						if (reg.test(webVeiwUrl)) {
							console.log('匹配成功了————————————————————————')
							_this.getContractStatusFn()
						}
						console.log('监听到了————————————————————')
					}, false);
				}, 500)

			},
			async getSignContractFn() {
				uni.showLoading({
					title: '正在加载合同...'
				})
				let info = this.vuex_userInfo
				let obj = {
					schoolId: this.schoolId,
					studentId: this.studentId,
				}
				if (this.trainingApplyId) obj.trainingApplyId = this.trainingApplyId
				const {
					data: res
				} = await getSignContract(obj)
				uni.hideLoading()
				this.webViewUrl = res.url
			},
			
			// 查询签约状态
			async getContractStatusFn() {
				const {
					data: res
				} = await getContractStatus({
					studentId: this.studentId,
					trainingApplyId: this.trainingApplyId
				})
				console.log(res)
				if (res.flowStatus == 2) {
					
					uni.navigateTo({
						url: '/pages/indexEntry/enroll/payment/payment?trainingApplyId='+this.trainingApplyId
					})
					if (res.flowStatus > 2) {
						
						this.$u.toast('合同已撤销或过期')
					}
				}
			}
		}
	}
</script>

<style>
	.pageBg {
		flex: 1;
		width: 100%;
		flex-direction: column;
		/* background-color: red; */
	}

	.page-inner {
		flex: 1;
	}
</style>