<template>
	<view>
		<Home :token="token" :userInfo="userInfo" :orderStatistics="orderStatistics" v-if="nowchos === 0"
			:key="Math.random()"></Home>
		<Mine ref="mine" :token="token" :userInfo="userInfo" v-else-if="nowchos === 1"></Mine>
		<view class="bottomboxs">
			<x-navbottom :nowchos='nowchos' @botomchos='botomchos'></x-navbottom>
		</view>
	</view>
</template>

<script>
	import Home from './home/index.vue' // 引入首页
	import Mine from './mine/index.vue' // 我的
	export default {
		components: { //在这里注册相应的组件
			Home,
			Mine,
		},
		data() {
			return {
				nowchos: 0, //当前选择了那个底部菜单
				isorders: 0,
				userType: 3,
				token: '',
				userInfo: {},
				orderStatistics: {},
			}
		},
		onShow() {
			var token = this.$cache.getToken()
			this.token = token
			var userInfo = this.$cache.getCache('userInfo')
			if (userInfo) {
				this.userInfo = userInfo
			} else {
				this.userInfo = {}
			}
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				this.$api.get('/api/user/profile').then(res => {
					if (res.code == 200) {
						this.userInfo = res.data.user
						this.$cache.setCache('userInfo', this.userInfo)
						if (this.nowchos == 0) {
							this.getHomeList()
						}
					} else {
						this.orderStatistics = {}
						this.$cache.removeToken()
						this.$cache.removeCache('userInfo')
						uni.showToast({
							title: '当前用户不存在',
							icon: 'none'
						});
					}
				})
			},
			// 获取运单统计
			getHomeList() {
				this.$api.get('/api/waybill/home').then(res => {
					if (res.code == 200) {
						this.orderStatistics = res.data
					}
				})
			},
			// tab
			botomchos(e) {
				const that = this
				if (e == 2) {
					that.$nextTick(() => {
						setTimeout(function() {
							that.$refs.mine.refreshTokenil()
						}, 100);
					})
				}
				uni.setStorageSync('nowchos', e);
				that.nowchos = e
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bottomboxs {
		position: relative;
		bottom: 0;
		z-index: 2023;
		height: 100rpx;
	}
</style>