<template>
	<view class="tab-bar">
		<view class="footer">
			<view class="index" @click="swithTab(1)">
				<view>
					<image
						:src="currentPage === 1?'../../static/tabbar/menu-1.png':'../../static/tabbar/menu-1-active.png'">
					</image>
				</view>
				<view :class="[currentPage === 1?'current':'']">首页</view>
			</view>
			<view class="scan" @click="swithTab(2)">
				<view>
					<image src="../../static/tabbar/menu-2.png"></image>
				</view>
				<view class="hover">扫码换电</view>
			</view>
			<view class="index" @click="swithTab(3)">
				<view>
					<image
						:src="currentPage === 3?'../../static/tabbar/menu-3-active.png':'../../static/tabbar/menu-3.png'">
					</image>
				</view>
				<view :class="[currentPage === 3?'current':'']">我的</view>
			</view>
		</view>
		<view class="bottom" v-if="plat.startsWith('iPhone X')"></view>
	</view>
</template>

<script>
	export default {
		name: "tab-bar",
		data() {
			return {
				currentPage: 0,
				stationCode: null,
				plat:uni.getSystemInfoSync().model
			};
		},
		mounted() {
			this.getRoute()
		},
		methods: {
			swithTab(num) {
				if (num === 1) {
					uni.switchTab({
						url: '/pages/index/index',
					});
				} else if (num === 2) {
					if (this.$storage.getJson("token")) {
						// let stationCode='st001'
						// uni.navigateTo({
						// 	url:'/pages/powerchange/index?stationCode='+stationCode
						// })
						let that = this
						uni.scanCode({
							success: function(res) {
								const url = res.result
								if (url.startsWith('https://zk.li-ai.com.cn/?station_code=')) {
									const parts = url.split("=")
									if (parts.length === 2) {
										that.stationCode = parts[1]
										uni.navigateTo({
											url: '/pages/powerchange/index?stationCode=' +that.stationCode
										})
									} else {
										uni.navigateTo({
											url: '/pages/public/404?error=' + '站点编号错误!!'
										})
									}
								} else {
									uni.navigateTo({
										url: '/pages/public/404?error=' + '二维码链接错误!!'
									})
								}
							},
						});
					} else {
						uni.navigateTo({
							url: '/pages/public/login',
						});
					}
				} else {
					if (this.$storage.getJson("token")) {
						uni.switchTab({
							url: '/pages/ucenter/index',
						});
					} else {
						uni.navigateTo({
							url: '/pages/public/login',
						});
					}
				}
			},
			getRoute() {
				let curPage = getCurrentPages();
				let route = curPage[curPage.length - 1].route
				if (route === 'pages/index/index') {
					this.currentPage = 1
				} else if (route === 'pages/ucenter/index') {
					this.currentPage = 3
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tab-bar{
		display: flex;
		flex-direction: column;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0rpx;
		.bottom{
			background-color: #fff;
			height: 48rpx;
			display: block;
		}
		.footer {
			width: 100%;
			height: 106rpx;
			background-image: url(~@/static/bbg.png);
			background-size: 100% 100%;
			display: flex;
			justify-content: space-around;
			padding-top: 15rpx;
			font-size: 26rpx;
			box-sizing: border-box;
			image {
				width: 39rpx;
				height: 39rpx;
			}
		
			.index {
				width: 240rpx;
				text-align: center;
			}
		
			.scan {
				position: relative;
				display: flex;
				flex: 1;
				text-align: center;
		
				view:first-child {
					position: absolute;
					bottom: 30rpx;
					width: 100%;
				}
		
				view:last-child {
					position: absolute;
					bottom: 6rpx;
					width: 100%;
				}
		
				image {
					width: 100rpx;
					height: 100rpx;
				}
			}
		
			.hover {
				color: #58be6b;
			}
		
			.current {
				color: #959ea8;
			}
		}
	}
	
</style>
