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


		<view class="topBox" :style="{'background-image':`url(${picBase+'my_bg.png'})`}">
			<view class="userBox" @click.stop="toUserInfo">
				<!-- <view class="userBox"> -->
				<image class="avatar" @click.stop="toUserInfo"
					:src="!userInfo.avatar?picBase+'defaultAvatar.png':userInfo.avatar" mode=""></image>
				<view class="nickname" @click.stop="toUserInfo" v-if="userInfo">
					{{!userInfo.name?'默认用户':userInfo.name}}
				</view>
				<button class="nickname" v-else open-type="getPhoneNumber" @getphonenumber="onGetphonenumber">
					立即登录
				</button>

				<image class="icon" @click.stop="toUserInfo" :src="picBase+'edit.png'" mode="" v-if="userInfo"></image>
			</view>

			<view class="infoBox" :style="{'background-image':`url(${picBase+'user_bg.png'})`}">
				<view class="left">
					<image class="icon" :src="picBase+'charge_yibiao.png'" mode=""></image>
					当前剩余次数：{{userInfo.diamond?userInfo.diamond:0}}
				</view>

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


		<view class="contentBox" :style="{'background-image':`url(${picBase+'my_b_bg.png'})`}">
			<view class="tabBox">
				<view class="item" @click="toHuibenJilu">
					<image class="icon" :src="picBase+'my_hbjl.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+'my_tgds.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="item.icon" mode=""></image>
						{{item.name}}
					</view>
					<image class="icon_r" :src="picBase+'arrR.png'" mode=""></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// import navBar from '@/components/navBar/index.vue'

	import {
		getUserInfo,
		login,
		addTeam,

		getPosterBg
	} from '@/api/index/index.js'
	export default {
		// components: {
		// 	navBar
		// },
		data() {
			return {
				userInfo: null,


				picBase: this.$picBase,
				navImg: this.$picBase + '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 + 'my_jl.png',
					},
					{
						name: '推荐奖励',
						icon: this.$picBase + 'my_jl.png',
					},
					{
						name: '消费记录',
						icon: this.$picBase + 'my_xfjl.png',
					},
					{
						name: '主角管理',
						icon: this.$picBase + 'my_xfjl.png',
					},
					{
						name: '客服中心',
						icon: this.$picBase + 'my_kfzx.png',
					},
					{
						name: '关于我们',
						icon: this.$picBase + 'my_gywm.png',
					},
				]
			}
		},
		onLoad() {

			// console.log(this.picBase + 'defaultAvatar.png');
			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()
				this.getShare()
			}

		},
		onShareAppMessage(params) {
			if (params.from === 'button' || params.from === 'menu') {
				return {
					// title: this.shareTitle,
					// imageUrl: this.$ossBaseUrl + this.shareImg,
					path: '/pages/my/index',
					mpId: this.$appId,
					type: this.$shareType,
				}
			}
		},
		methods: {
			onGetphonenumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					this.login(e.detail)
				} else {
					// // 调用uni.getSetting()函数获取用户授权状态
					// uni.getSetting({
					//   success(res) {
					//     if (res.authSetting['scope.userInfo']) {
					//       // 用户已经授权了获取用户信息的权限
					//       authStatus = true;
					//     } else {
					// 用户还没有授权获取用户信息的权限
					uni.showModal({
						title: '提示',
						content: '应用需要获取您的手机号才能正常登录，点击确定跳转到授权设置页面？',
						confirmText: '确定',
						cancelText: '取消',
						success(modalRes) {
							if (modalRes.confirm) {
								// 用户同意跳转到授权设置页面
								uni.openSetting();
							}
						},
					});
					//     }
					//   },
					// });
				}

				console.log('onGetphonenumber()=============================', e);
			},

			async getShare() {
				let res1 = await getPosterBg({
					key: 'share_title'
				})
				console.log('分享信息返回值--------1', res1);
				if (res1.code == 0) {
					this.shareTitle = res1.data.value
				} else {
					this.$toast(res1.message)
				}

				let res2 = await getPosterBg({
					key: 'share_image'
				})
				console.log('分享信息返回值--------2', res2);
				if (res2.code == 0) {
					this.shareImg = res2.data.value
				} else {
					this.$toast(res2.message)
				}

				// 
			},
			async addTeam(p) {
				let res1 = await addTeam(p)
				console.log('我的-加入团队返回值--------2', res1);
				if (res1.code == 0) {
					console.log('我的--加入团队成功--------------------------------');
				} else {
					this.$toast(res1.message)
				}
			},

			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(detail) {
				let _this = this
				uni.showLoading({
					title: '登录中...'
				})
				uni.login({
					provider: 'weixin',
					success: async (res) => {
						if (res.errMsg == 'login:ok') {
							let parmas = {
								code: res.code,
								helpId: uni.getStorageSync('inviteInfo') ? uni.getStorageSync(
									'inviteInfo') : '',
								detail
							}
							console.log('登录所传的parmas参数:', parmas);
							let res1 = await login(parmas)

							console.log('登录返回值--------', res1);
							if (res1.code == 0) {
								// 处理加入团队逻辑
								// _this.addTeam({
								// 	id: uni.getStorageSync('inviteInfo')
								// })


								uni.hideLoading()
								uni.setStorageSync('token', res1.data.token)
								uni.setStorageSync('userInfo', res1.data.user_info)
								_this.userInfo = res1.data.user_info
								_this.$toast('登录成功')
								uni.removeStorageSync('inviteInfo')
								// setTimeout(() => {
								// 	uni.reLaunch({
								// 		url: '/pages/my'
								// 	})
								// }, 1500)
							} else {
								uni.hideLoading()
								_this.$toast(res1.message)
							}
						}
					}
				});
			},
			toPage(i) {
				if (i == 0) {
					if (!uni.getStorageSync('token')) {
						return this.$toast('请登录后操作')
					}

					if (this.userInfo.is_share == 0) {
						uni.navigateTo({
							url: '/pages/my/tuiguangDashi/index'
						})
					} else {
						uni.navigateTo({
							url: '/pages/my/pubCenter/index'
						})
					}

				} else if (i == 1) {
					if (!uni.getStorageSync('token')) {
						return this.$toast('请登录后操作')
					}
					uni.navigateTo({
						url: '/pages/my/jiangli/index'
					})
				} else if (i == 3) {
					if (!uni.getStorageSync('token')) {
						return this.$toast('请登录后操作')
					}
					uni.navigateTo({
						url: '/pages/my/roleManage/index'
					})
				} else if (i == 2) {
					if (!uni.getStorageSync('token')) {
						return this.$toast('请登录后操作')
					}
					uni.navigateTo({
						url: '/pages/my/myRecord/index?tabIndex=1'
					})
				} else if (i == 4) {
					uni.navigateTo({
						url: '/pages/my/kefu/index'
					})
				} else if (i == 5) {
					uni.navigateTo({
						url: '/pages/my/aboutUs/index'
					})
				}
			},
			toTuguangDashi() {
				if (!uni.getStorageSync('token')) {
					return this.$toast('请登录后操作')
				}
				if (this.userInfo.is_share) {
					uni.navigateTo({
						url: '/pages/my/pubCenter/index'
					})
				} else {
					uni.navigateTo({
						url: '/pages/my/tuiguangDashi/index'
					})
				}
			},
			toTuguangCenter() {
				if (!uni.getStorageSync('token')) {
					return this.$toast('请登录后操作')
				}
				uni.navigateTo({
					url: '/pages/my/pubCenter/index'
				})
			},
			onTabBar(e) {
				console.log('e------------', e);
				uni.switchTab({
					url: '/' + e
				})
			},

			toCharge() {
				if (!uni.getStorageSync('token')) {
					return this.$toast('请登录后操作')
				}
				let res = wx.getSystemInfoSync();

				console.log('wx.getSystemInfoSync()', res.platform);
				if (res.platform == 'ios') {
					return wx.showModal({
						title: '提示',
						content: '【由于相关规范，iOS功能暂不可用】',
						success(res) {
							if (res.confirm) {
								console.log('用户点击确定')
							} else if (res.cancel) {
								console.log('用户点击取消')
							}
						}
					})
				}
				uni.navigateTo({
					url: '/pages/my/charge/index'
				})
			},
			toHuibenJilu() {
				if (!uni.getStorageSync('token')) {
					return this.$toast('请登录后操作')
				}
				uni.navigateTo({
					url: '/pages/my/huiBen_record/index'
				})
			},
			toUserInfo() {
				if (!uni.getStorageSync('token')) {
					return this.$toast('请登录后操作')
				}
				uni.navigateTo({
					url: '/pages/my/userInfo/index'
				})
			}
		}
	}
</script>

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