<template>
	<view class="page-content">
		<image :src="yellowURL+'start.png'" width="" mode="widthFix"></image>
		<Advertisement v-if="showAdvertisement" @informNext='goHome' ref='Advertisement'></Advertisement>
	</view>
</template>

<script>
	import oilSiteApi from '@/api/oil-site.js'
	import wxCode from '@/api/wxcode.js'
	import Advertisement from '../../components/Advertisement.vue'
	export default {
		components: {
			Advertisement
		},
		data() {
			return {
				showAdvertisement: false,
				key: '',
				isOnline:false,
				option: null,
				imgURL: this.global.imgURL + 'start/',
				yellowURL: this.global.yellowURL + 'xq-ad/',
				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: ""
						}]
					}
				],

			}
		},
		mounted() {
			// console.log('mounted')
		},
		onLoad(option) {
			this.option = option;
			this.versionUpdate()
		},
		onReady(option) {},
		methods: {
			versionUpdate() {
				// console.log('versionUpdate')
				let that = this
				const updateManager = uni.getUpdateManager();
				updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					// console.log('请求完新版本信息的回调', res.hasUpdate);
					if (!res.hasUpdate) {
						that.initFn(that.option)
					}
				});
				updateManager.onUpdateReady(function(res) {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好,是否重启应用?',
						success(res) {
							if (res.confirm) {
								console.log(res.confirm, 'res.confirm版本信息')
								// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							} else {
								that.initFn(that.option)
							}
						},
					});
				});
				updateManager.onUpdateFailed(function(res) {
					console.log('新的版本下载失败')
					// 新的版本下载失败
				});
			},
			async initFn(option) {
				const token = uni.getStorageSync('Authorization')
				const openid = uni.getStorageSync('openid')
				// this.key = '';
				// this.isOnline = false;
				if (token) {
					await oilSiteApi.isLoginSuccess().then(res => {
						if (res.code == 20000) this.isOnline = true
					}).catch(err => {})
				}
				// console.log(isOnline,'isOnline')
				// if (isOnline) {
				// 	this.key = '在线'
				// } else {
				// 	this.key = '离线'
				// }
				if (this.isOnline) {
					if (!uni.getStorageSync('filterData')) {
						this.getFilterData()
					} else if (uni.getStorageSync('filterData')[0].detailList.length < 2) {
						this.getFilterData()
					}
				}
				if (option.scene) {
					this.Interceptor(option.scene);
				} else {
					setTimeout(() => {
						this.showAdvertisement = true
					}, 2000)
				}
			},
			goHome(type) {
				if (this.isOnline) {
					if (type) {
						uni.setStorageSync('isJump', {
							page: 'Financial/pages/estimate/index',
							disposable: true
						})
					}
					uni.switchTab({
						url: `/pages/tabbar/home/home`
					})
				} else {
					uni.redirectTo({
						url: '/BagAuth/pages/login/login'
					})
				}
			},
			Interceptor(id) {
				// let taht = this
				// console.log(`%c 检测到参数拦截器启动`, 'color:red;font-size:50px');
				// this.tools.overallInterceptor((opentype) => {
				// 	uni.removeInterceptor(opentype);
				// 	wxCode.getQrCodeType({
				// 		qrCode: id
				// 	}).then(res => {
				// 		if (res.code == 20000) {
				// 			uni.redirectTo({
				// 				url: '/AppletCode/pages/index/index?details=' + JSON.stringify({...res.data,qrCode: id}),
				// 				fail(e) {
				// 					console.log(e,'跳转失败')
				// 				}
				// 			})
				// 		}
				// 	})
				// })
				wxCode.getQrCodeType({
					qrCode: id
				}).then(res => {
					if (res.code == 20000) {
						uni.redirectTo({
							url: '/AppletCode/pages/index/index?details=' + JSON.stringify({
								...res.data,
								qrCode: id
							}),
							// fail(e) {
							// 	console.log(e, '跳转失败')
							// }
						})
					}
				})
			},
			getFilterData() {
				// console.log('wodefenzhi')
				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)
						uni.setStorageSync('productCodeList', res.data.productCodeList)
						// console.log(res, '%c 这里是启动页调用的菜单列表', 'font-size:36px;color:red;')
						// this.$refs.slFilter.resetMenuList(this.menuList)
					}
				})
			},
		}
	}
</script>

<style scoped>
	.page-content {
		height: 100%;
		overflow: hidden;
	}

	.page-content image {
		width: 750upx;
	}
</style>