<template>
	<view class="web-box">
		<navbar ref="navbar" :config="config" backColor="#1A1A1A" ></navbar>
		<web-view id="web-view" :src="path" @message="onMessage" @error="onError()" />
	</view>
</template>

<script>
	import {
		facParamsHandle
	} from '@/api/government'
	let app = getApp();
	export default {
		data() {
			return {
				title: '',
				navKey: 1,
				config: {
					back: true,
					web: true,
					closePath: '',
					title: '宜格服务',
					color: 'black',
					backgroundColor: [1, '#fff'],
					statusBarFontColor: 'black'
				},
			}
		},
		onReady() {
			// #ifdef APP
			const currentWebview = this.$scope.$getAppWebview()
			// this.pageTitle = uni.getAppBaseInfo().appName
			setTimeout(() => {
				const {
					statusBarHeight,
					screenHeight,
					navHeight
				} = app.globalData;
				const top = statusBarHeight + navHeight
				const wv = currentWebview.children()[0]
				wv.setStyle({
					top: top,
					bottom: 0
				})
				wv.addEventListener('titleUpdate', e => {
					if (e.title) {
						this.$set(this.config, 'title', this.title ||e.title ||  '');
						this.$refs.navbar.init()
					}
				})
			}, 10); //如果是页面初始化调用时，需要延时一下
			// #endif
		},
		onLoad(opt) {
			this.path = opt.path;
			this.title = opt.title
			 

		},
		onShow() {

		},
		methods: {
			onError() {
				uni.redirectTo({
					url: '/pages/web-view/web-error'
				});
			},
			onMessage(e) {
				// e.detail.data 是一个数组
				if (e.detail.data) {
					// this.getCheck(e.detail.data)
					const data = e.detail.data[0];
					let {
						action
					} = data;
					switch (action) {
						case "auth":
							this.getCheck(data);
							break;
						case "map":
							this.openMap(data);
							break;
						case "fac":
							this.checkFac(data);
							break
						case "pay":
							this.goPayPage(data.orderNo);
							break
						case "openWechatApplet":
							this.onOpenWechatApplet(data);
							break
					}
				}
			},
			goPayPage(orderNo) {
				uni.redirectTo({
					url: "/pages/payPage/index?orderNo=" + orderNo
				})
			},
			getCheck(data) {
				console.log('e = ', data)
				this.$yghttp.post('/auth/before/check', data).then(res => {
					if (res.code == 200) {
						if (res.data.needConfirmAuth) {
							uni.setStorageSync('authInfo', data);
							uni.setStorageSync('checkInfo', res.data);
							uni.navigateTo({
								url: '/pages/login/service-auth'
							})
						} else {
							// console.log('res.data.redirect = ' , res.data.redirect)
							uni.redirectTo({
								url: `/pages/web-view/Apps?path=${encodeURIComponent(res.data.redirect)}`
							});
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: res
						})
					}
				}).catch(err => {
					console.log('eeeeeee = ', err)
					// uni.showToast({
					// 	icon: '当前应用',
					// 	title: res
					// })
				})
			},
			openMap(data) {
				// data = {
				// 	latitude: 30.480535,
				// 	longitude: 114.42001,
				// 	name: '哗哗哗',
				// 	address: '啦啦啦啦啦'
				// }

				const {
					latitude,
					longitude,
					name,
					detailedAddress
				} = data
				uni.openLocation({
					latitude: parseFloat(latitude), // 要去的地址经度，浮点数
					longitude: parseFloat(longitude), // 要去的地址纬度，浮点数
					name: name, // 位置名
					address: detailedAddress, // 要去的地址详情说明
					scale: 16, // 地图缩放级别,整形值,范围从1~28。默认为最大
				});
			},

			checkFac(data) {
				// 将第三方h5传递的参数交于服务端进行加入处理并返回
				facParamsHandle(data).then(res => {
					if (res.code == 200) {
						this.$openWXminiprogram(res.data, 2)
					}
				})
			},
			onOpenWechatApplet(data = {}) {
				try {
					const {
						appletOriginalId,
						address,
						type
					} = data
					console.log("appletOriginalId= ", appletOriginalId)
					if (!appletOriginalId) {
						throw new Error("小程序ID错误")
					}
					if (!address) {
						throw new Error("小程序地址错误")
					}
					this.$openWXminiprogram(data, 0)
				} catch (e) {
					//TODO handle the exception
					if (e.message) {
						uni.showToast({
							title: e.message,
							icon: 'none'
						})
					}

				}

			}



		}
	}
</script>

<style lang="scss" scoped>
</style>