<script>
	import Vue from 'vue'
	import accountApi from '@/api/account.js'
	import oilSiteApi from '@/api/oil-site.js'
	let loginTimer = null
	let onlineTimer = null

	export default {
		data() {
			return {
				menuList: [{
						title: '渠道',
						isMutiple: false,
						key: 'channelCode',
						detailList: [{
							title: "全部",
							value: ""
						}]
					},
					{
						title: '0#轻柴油',
						key: 'oilProductCode',
						isMutiple: false,
						detailList: [{
							title: "全部",
							value: ""
						}]
					},
					{
						title: '品牌',
						key: 'siteBrand',
						isSort: true,
						isMutiple: false,
						detailList: [{
							title: "全部",
							value: ""
						}]
					}
				],
				loginStatus: 'online',
				msg: ''
			}
		},
		onLaunch() {
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif	
								
					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})
			this.onShareAppMessage()

		},

		methods: {
			getFilterData() {
				oilSiteApi.getCheckInfo().then(res => {
					if (res.code == 20000) {
						let channelCodes = []
						res.data.channelCodes.map(item => {
							channelCodes.push({
								title: item.name,
								value: item.id
							})
						})
						let productCodes = []
						res.data.productCodes.map(item => {
							productCodes.push({
								title: item.name,
								value: item.id
							})
						})
						let siteBrands = []
						res.data.siteBrands.map(item => {
							siteBrands.push({
								title: item.name,
								value: item.id
							})
						})
						console.log(channelCodes, productCodes, siteBrands)
						this.menuList[0].detailList = this.menuList[0].detailList.concat(channelCodes)
						this.menuList[1].detailList = this.menuList[1].detailList.concat(productCodes)
						this.menuList[2].detailList = this.menuList[2].detailList.concat(siteBrands)
						uni.setStorageSync('filterData', this.menuList)
						console.log(uni.getStorageSync('filterData'),'pppppppppppppppppppppppp')
						// this.$refs.slFilter.resetMenuList(this.menuList)
					}
				})
			},
			onShareAppMessage() {
				let that = this
				wx.onAppRoute(() => {
					let white = ['BagStation/pages/stationDetail/stationDetail']
					// console.log('当前页面路由发生变化 触发该事件onShareAppMessage')
					const pages = getCurrentPages() //获取加载的页面
					const view = pages[pages.length - 1] //获取当前页面的对象
					if (!view) return false //如果不存在页面对象 则返回
					// 若想给个别页面做特殊处理 可以给特殊页面加isOverShare为true 就不会重写了
					// const data = view.data
					// if (!data.isOverShare) {
					// data.isOverShare = true
					view.onShareAppMessage = () => { //重写分享配置
						return {
							title: '中品加油',
							path: "/pages/index/startPage/startPage", //若无path 默认跳转分享页
							// imageUrl:'/image/onshowMessage.png' //若无imageUrl 截图当前页面
							imageUrl: 'https://publicxingyou.oss-cn-hangzhou.aliyuncs.com/mp-oil/login-bg.png',
						}
					}
					console.log(view.route)
					clearTimeout(loginTimer)
					clearTimeout(onlineTimer)
					
					if (view.route != 'pages/index/startPage/startPage' && view.route.indexOf('BagAuth') == -1) {
						const token = uni.getStorageSync('Authorization')
						const openid = uni.getStorageSync('openid')
						
						let jumpUrl = '/BagAuth/pages/login/login'
						let urlTarget = this.obtainUrlPathParameterTarget(view.$page.fullPath)
						console.log('urlTarget',urlTarget)
						let target = {}
						if(urlTarget.q) {
							let originLink  =  decodeURIComponent(decodeURIComponent(urlTarget.q))
							target = this.obtainUrlPathParameterTarget(originLink)
							target.siteId && (jumpUrl += `?scanningParams=${decodeURIComponent(urlTarget.q)}`)
						}
						 // console.log('jumpUrl',jumpUrl)
						if (token && openid) {
							this.getAmount()
							// if(!white.includes(view.route) ){
							// 	this.getAmount()
							// }
				
							// if(target.siteId || !white.includes(view.route) ){
							// 	this.getAmount()
							// }
								
							onlineTimer = setTimeout(() => {
								if (this.loginStatus == 'offline') {
									uni.clearStorageSync()
									uni.showModal({
										title: '需要您重新登录',
										content: this.msg,
										duration: '3000',
										success: function(res) {
											if (res.confirm) {
												uni.redirectTo({
													url: jumpUrl
												})
											}
										},
										complete: () => {
											clearTimeout(onlineTimer)
										}
									})
								}else{
									// if(!uni.getStorageSync('filterData')){
									// 	that.getFilterData()
									// }
								}
							}, 500)

						} else {
								target.siteId && uni.redirectTo({
									url: jumpUrl
								})
							// loginTimer = setTimeout(() => {
							// 	uni.showModal({
							// 		title: '请您登录',
							// 		content: '您还没有登录,暂时查不到油站哦',
							// 		duration: '3000',
							// 		success: function(res) {
							// 			if (res.confirm) {
							// 				uni.redirectTo({
							// 					url: '/BagAuth/pages/login/login'
							// 				})
							// 			}
							// 		}
							// 	})
							// }, 1000)
						}


					}
					// }
				})
			},
			
			obtainUrlPathParameterTarget(url) {
				let target = {}
				if(url.includes('?')) {
					let paramsText = url.split("?")[1]
					let paramsArr = paramsText.split("&")
					paramsArr.forEach(item => {
						target[item.split('=')[0]] = item.split('=')[1]
					})
				}
				console.log('11target',target)
				return target
			},
			
			// 是否token过期或ip更改
			getAmount() {			
				clearTimeout(loginTimer)
				accountApi.getTotalOilCardInfo().then(res => {
					console.log('res.code',res.code)
					console.log(555)
					if (res.code == 20000) {
						this.loginStatus = 'online'
					} else if (res.code == 42011) {
						this.loginStatus = 'offline'
						this.msg = res.msg
					}
				})
			},
		},

		onReady() {
		
		},

		onShow: function() {
			console.log(12388)
			uni.getLocation({
				type: 'wgs84',
				success: function(res) {
					uni.setStorageSync('location', {
						longitude: res.longitude + '',
						latitude: res.latitude + ''
					})
				}
			});
			console.log('onReady')
			const token = uni.getStorageSync('Authorization')
			const openid = uni.getStorageSync('openid')
			let key = ''
			if (token && openid) {
				this.getAmount()
				key = '真在线'
			} else {
				key = '离线'
			}
			if (key == '在线') {

			}
			setTimeout(() => {
				console.log(key)
				if (key == '在线') {

				} else {
					// uni.redirectTo({
					// 	url: '/BagAuth/pages/login/login'
					// })
				}
			}, 2000)
			// uni.showShareMenu()
			// console.log('App Show')

		},
		onHide: function() {
			// console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import "colorui/animation.css";
	@import "common/common.css";
</style>