<template>
	<view :style="{paddingTop:`${setingMenuData.top}px`}" class="page_body">
		<view class="page_container">
			<home ref="home" v-show="tabBarIndex==0"></home>
			<charging-station ref='ChargingStation' v-show="tabBarIndex==1"></charging-station>
			<order ref="order" v-if="tabBarIndex==3"></order>
			<personal @switchTabBar='switchTabBar' v-if="tabBarIndex==4"></personal>
		</view>
		<tab-bar v-model="tabBarIndex"></tab-bar>
	</view>
</template>

<script>
	import tool from '../../utils/tool.js'
	import tabBar from "@/components/tabBar/tabBar.vue"
	import home from "../home/index.vue"
	import ChargingStation from "../ChargingStation/index.vue"
	import order from "../order/index.vue"
	import personal from "../personal/index.vue"
	  import x from "./index.js"
	export default {
		components: {
			tabBar,
			home,
			ChargingStation,
			order,
			personal
		},
		data() {
			return {
				user: uni.getStorageSync('user'),
				location: null,
				setingMenuData: null,
				tabBarIndex: 0,
				Anticorrosive: null
			}
		},
		watch: {
			tabBarIndex(n,o) {
				let that = this
				if(n==1){
					this.$refs.ChargingStation.onshow()
				}else if(n==2){ 
					this.readyScanCode({
						success(){
							that.scanCode();
						},
						fail(){
							if(n!==o) that.tabBarIndex = o;
							if(n==o) that.tabBarIndex=0; 
						}
					})
					
                } 
			}
		},
		created() {
			this.init()
		},
		onLoad() {
			this.onOrderUpdate(); 
		},
		onShow() { 
			this.showInit()
		},
		methods: {
			readyScanCode({success,fail}){ 
				if(!this.user){
					uni.showModal({
						title: '提示',
						content: '登录后才可扫码下单哦',
						confirmText:'去登陆',
						success: function (res) {
							if (res.confirm) { 
								uni.reLaunch({
									url: '/pages/login/index'
								})
							} else if (res.cancel) {   
								fail()
							}
						}
					});  
					return
				}
				try{
					success()
				}catch(e){
					fail() 
				}
			},
			async scanCode() {
				
				let res = await this.tool.scanCode().catch(err=>{
				  console.log('取消');
				  this.tabBarIndex = 0
				});
				if (res?.result) {  
					let routingParameter =  this.tool.routingParameterGeneration({
						type:'qrCode',
						qrCode:encodeURIComponent(res.result),
						pointTo:'activeScanCode'
					})
					uni.navigateTo({
						url: `/ChargingStation/pages/placeOrder/index${routingParameter}`
					})
				}
			},
			switchTabBar(e) {
				this.tabBarIndex = e
			},
			onOrderUpdate() {
				uni.$on('orderUpdate', () => {
					this.$refs.order.seach()
				})
			},
			showInit() {
				this.tool.getCards(); 
				if(this.tabBarIndex==2){
					this.tabBarIndex = 0
				}
			},
			async getPosition(callback) {
				await this.tool.getLocation().then(res => {
					this.location = uni.getStorageSync('location')
				}).catch(err => {});
			},
			async init() {
				this.Anticorrosive = this.tool.Anticorrosive;
				this.getSetingMenu(); //获取胶囊数据  
			},
			getSetingMenu() {
				this.setingMenuData = wx.getMenuButtonBoundingClientRect();
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import 'index.scss';
</style>