<template>
	<view class="container">
		<navBar title="" :navImg="navImg" :back="false" color="#333333" background="#FFFFFF" />
		<tabBar :tabBarList="tabBarList" :routePath="routePath" @onTabBar="onTabBar" />


		<view class="topBox">
			<view class="userBox">
				<image class="avatar" @click="toUserInfo" :src="!userInfo?picBase+'defaultAvatar.png':userInfo.avatar" mode=""></image>
				<view class="nickname" @click="toUserInfo" v-if="userInfo">
				{{!userInfo.name?'默认用户':userInfo.name}}
				</view>
				<view class="nickname" @click="login" v-else>
					立即登录
				</view>
				<image class="icon" @click="toUserInfo" :src="picBase+'edit.png'" mode="" v-if="userInfo"></image>
			</view>

			<view class="infoBox">
				<view class="left">
					<image class="icon" :src="picBase+'edit.png'" mode=""></image>
					当前剩余次数：2
				</view>

				<view class="btn" @click="toCharge">
					立即充值
				</view>
			</view>
		</view>


		<view class="contentBox">
			<view class="tabBox">
				<view class="item" @click="toHuibenJilu">
					<image class="icon" :src="picBase+'edit.png'" mode=""></image>
					<view class="right">
						<view class="t1">
							绘本记录
						</view>
						<view class="t2">
							绘本历史记录
						</view>
					</view>
				</view>

				<view class="item" @click="toTuguangDashi">
					<image class="icon" :src="picBase+'edit.png'" mode=""></image>
					<view class="right">
						<view class="t1">
							推广大使
						</view>
						<view class="t2">
							推广大使招募
						</view>
					</view>
				</view>
			</view>

			<view class="serListBox">
				<view class="item" v-for="(item,index) in serList" @click="toPage(index)">
					<view class="left">
						<image class="icon" src="../../static/other/edit.png" mode=""></image>
						{{item.name}}
					</view>
					<image class="icon_r" src="../../static/other/arrR.png" mode=""></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		login,
		getUserInfo
	} from '@/api/index/index.js'
	// import navBar from '@/components/navBar/index.vue'
	export default {
		// components: {
		// 	navBar
		// },
		data() {
			return {
				userInfo:null,
				
				
				picBase: this.$picBase,
				navImg: require('@/static/other/logo.png'),
				tabBarList: [{
						"pagePath": "pages/index/index",
						"iconPath": "../../static/tabbar/index.png",
						"selectedIconPath": "../../static/tabbar/index_a.png",
						"text": "首页"
					},
					{
						"pagePath": "pages/my/index",
						"iconPath": "../../static/tabbar/my.png",
						"selectedIconPath": "../../static/tabbar/my_a.png",
						"text": "我的"
					}
				],
				routePath: '',



				serList: [{
						name: '推荐奖励',
						icon: this.$picBase + 'edit.png',
					},
					{
						name: '消费记录',
						icon: this.$picBase + 'edit.png',
					},
					{
						name: '客服中心',
						icon: this.$picBase + 'edit.png',
					},
					{
						name: '关于我们',
						icon: this.$picBase + 'edit.png',
					},
				]
			}
		},
		onLoad() {
			uni.hideTabBar()
			let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
			let curRoute = routes[routes.length - 1].route // 获取当前页面路由，也就是最后一个打开的页面路由
			this.routePath = curRoute;
			console.log('routePath', this.routePath);

		},
		async onShow(){
			
			if(uni.getStorageSync('token')){
				this.getUserInfo()
			}
			
		},
		methods: {
			async getUserInfo() {
				let res1 = await getUserInfo()
				console.log('用户信息返回值--------', res1);
				if (res1.code == 0) {
					uni.setStorageSync('userInfo', res1.data)
					this.userInfo=res1.data
				} else {
				this.$toast(res1.message)
				}
			},
			login() {
				let _this = this
				uni.showLoading({
					title: '登录中...'
				})
				uni.login({
					provider: 'weixin',
					success: async (res) => {
						if (res.errMsg == 'login:ok') {
							let parmas = {
								code: res.code,
							}
							console.log('登录所传的parmas参数:', parmas);
							let res1 = await login(parmas)
			
							console.log('登录返回值--------', res1);
							if (res1.code == 0) {
								uni.hideLoading()
								uni.setStorageSync('token', res1.data.token)
								uni.setStorageSync('userInfo', res1.data.user_info)
								_this.userInfo=res1.data.user_info
								_this.$toast('登录成功')
								// setTimeout(() => {
								// 	uni.reLaunch({
								// 		url: '/pages/my'
								// 	})
								// }, 1500)
							} else {
								uni.hideLoading()
							_this.$toast(res1.message)
							}
						}
					}
				});
			},
			toPage(i) {
				if (i == 0) {
					uni.navigateTo({
						url: '/pages/my/jiangli/index'
					})
				} else if (i == 1) {
					uni.navigateTo({
						url: '/pages/my/myRecord/index?tabIndex=1'
					})
				}
			},
			toTuguangDashi() {
				uni.navigateTo({
					url: '/pages/my/tuiguangDashi/index'
				})
			},
			toTuguangCenter() {
				uni.navigateTo({
					url: '/pages/my/pubCenter/index'
				})
			},
			onTabBar(e) {
				console.log('e------------', e);
				uni.switchTab({
					url: '/' + e
				})
			},

			toCharge() {
				uni.navigateTo({
					url: '/pages/my/charge/index'
				})
			},
			toHuibenJilu() {
				uni.navigateTo({
					url: '/pages/my/huiBen_record/index'
				})
			},
			toUserInfo() {
				uni.navigateTo({
					url: '/pages/my/userInfo/index'
				})
			}
		}
	}
</script>

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