<template>
	<view class="container">
		<view class="topBox">
			<view class="userBox flex">
				<view class="userInfo flex_item" @click="navTo('/pageExpert/users/user_info/index')">
					<view class="avatarBox">
						<image class="avatar" :src="userInfo.avatar || '/static/error/missing-face.png'"></image>
						<view class="level">{{userInfo.Staff && userInfo.Staff.is_expert == 1?'合作专家':'普通用户'}}</view>
					</view>
					<view class="nameBox">
						<view class="name">{{userInfo.Staff && userInfo.Staff.staff_name || userInfo.real_name || userInfo.nickname}}</view>
						<view class="phone">{{userInfo.Staff && userInfo.Staff.phone || userInfo.phone}}</view>
						<!-- <view class="phone">所属门店:360健康旗舰店</view> -->
					</view>
				</view>
				<image v-if="hasSwitch == true" class="set" src="/static/image/set.png" @click="switchUser"></image>
			</view>
			<view class="flex statistic">
				<view class="statTpl bg1" @click="navTo('/pageExpert/wallet/wallet')">
					<view class="statName">我的健康米</view>
					<view class="statNum">{{userInfo.now_money | formatNumber }}</view>
				</view>
				<view class="statTpl bg2" @click="navTo('/pageExpert/users/promoter-list/index')">
					<view class="statName">我的推广</view>
					<view class="statNum">{{userInfo.spread_count }}</view>
				</view>
			</view>
		</view>
		<view class="list-box">
			<view class="user-list flex" @click="navTo('/pageExpert/users/user_spread_code/index')">
				<image src="/static/image/img24.png" mode="widthFix" class="left-img"></image>
				<view class="">邀请好友</view>
				<image src="/static/image/img28.png" mode="" class="right-img"></image>
			</view>
			<view class="user-list flex" @click="navTo('/pageExpert/user/statistics')">
				<image src="/static/work/img07.png" mode="widthFix" class="left-img"></image>
				<view class="">咨询统计</view>
				<image src="/static/image/img28.png" mode="" class="right-img"></image>
			</view>
			<!-- <view class="user-list flex" @click="callService">
				<image src="/static/image/img27.png" mode="widthFix" class="left-img"></image>
				<view class="">客服</view>
				<image src="/static/image/img28.png" mode="" class="right-img"></image>
			</view> -->
		</view>
		<!-- 切换账号身份 -->
		<u-action-sheet :show="showSwitch" :actions="actions" title="请选择切换身份" :description="description" @close="showSwitch = false" @select="switchSelect"></u-action-sheet>
		<pageFooter @newDataStatus="newDataStatus"></pageFooter>
	</view>
</template>
<script>
	import pageFooter from "../components/pageFooter/index.vue";
	import { getUserInfo } from '@/api/user.js';
	import { toLogin } from '@/libs/login.js';
	import { getList } from '@/api/new_store.js'
	import { mapState, mapGetters} from "vuex";
	// #ifdef H5
	import Auth from '@/libs/wechat';
	// #endif
	// #ifdef MP
	import editUserModal from '@/components/eidtUserModal/index.vue';
	// #endif
	import { HTTP_REQUEST_URL } from '@/config/app';
	export default {
		components: {
			pageFooter,
			// #ifdef MP
			editUserModal
			// #endif
			// service
		},
		computed: {
		  ...mapGetters({
		    isLogin: 'isLogin'
		  })
		},
		data() {
			return {
				userInfo:'',
				isFooter: false,
				//切换账号身份
				showSwitch:false,
				actions:[],
				description:'',
				hasSwitch:false,//账号是否有多个身份  true有多个身份
			};
		},
		created() {
		  let that = this;
		  // #ifdef MP
		  // 小程序静默授权
		  console.log(this.$store.getters.isLogin,'this.$store.getters.isLogin')
		  if (!this.$store.getters.isLogin) {
			// Routine.getCode()
			//   .then(code => {
			//     Routine.silenceAuth(code).then(res => {
			//       this.onLoadFun();
			//     })
			//   })
			//   .catch(res => {
			//     uni.hideLoading();
			//   });
		  }
		  // #endif
		  if (that.isLogin == false) {
				// #ifdef H5 || APP-PLUS
				toLogin()
				// #endif
				// #ifdef MP
				//this.isShowAuth = true;
				// #endif
		  }
		  // #ifdef APP-PLUS
		  that.$set(that, 'pageHeight', app.globalData.windowHeight);
		  // #endif
		},
		filters: {
			formatNumber(num) {
				num = Number(num);
				if (num == 0 || (num > 0 && num < 10000)) {
					return num + '';
				} else {
					return (num / 10000).toFixed(2) + '万';
				}
			}
		},
		onShow() {
			 let that = this;
			if (that.isLogin) {
				this.loadData()
			};
		},
		methods: {
			// #ifdef MP
			editSuccess() {
				this.editModal = false;
				this.getUserInfo();
			},
			closeEdit() {
				this.editModal = false;
			},
			// #endif
			loadData(){
				let obj = this;
				getUserInfo().then(res => {
					obj.userInfo = res.data
					if(obj.userInfo.Staff && obj.userInfo.Staff.uid){
						//有多个身份
						obj.hasSwitch = true
						if(obj.userInfo.Staff.is_expert == 1){
							obj.actions = [{name:'用户端',url:'/pages/index/index'},{name:'专家端',url:'/pageExpert/index/index'}]
						}
					}else{
						obj.hasSwitch = false
					}
					obj.$store.commit("SETUID", res.data.uid);
					obj.$store.commit("UPDATE_USERINFO", res.data);
				});
			},
			//切换用户身份
			switchUser(){
				let obj = this;
				let switchInfo = uni.getStorageSync("switchInfo")
				console.log(switchInfo,'switchInfo')
				if(switchInfo){
					obj.description = '您当前在'+switchInfo.name
				}else{
					obj.description = '您当前在用户端'
				}
				this.showSwitch = true
			},
			//显示切换用户下拉框
			switchSelect(e){
				let switchInfo = uni.getStorageSync("switchInfo")
				if(switchInfo){
					if(switchInfo.name == e.name){
						uni.showToast({
							title: '您当前在'+ e.name,
							icon: 'none',
						})
						return
					}else{
						uni.setStorageSync('switchInfo', e);
						uni.reLaunch({
							url:e.url
						})
					}
				}else{
					if(e.name == '用户端'){
						uni.showToast({
							title: '您当前在用户端',
							icon: 'none'
						})
					}else{
						uni.setStorageSync('switchInfo', e);
						uni.reLaunch({
							url:e.url
						})
					}
				}
			},
			//联系客服
			callService(){
				this.$util.getCustomer(this.userInfo)
			},
			//跳转
			navTo(url) {
				if (this.isLogin == false) {
					toLogin()
				}else{
					uni.navigateTo({
						url:url
					})
				}
			},
			newDataStatus(val) {
				this.isFooter = val;
			},
		},
	};
</script>
<style scoped lang="scss">
	.container{
		width: 100%;
		height: 100%;
		background-color: #f1f7f4;
	}
	.topBox{
		width: 100%;
		padding: 15rpx 25rpx;
		background: url('/static/image/userBg.png') no-repeat;
		background-size: 100% 100%;
		padding-top: calc(88px + env(safe-area-inset-top));
		
	}
	.userBox{
		.userInfo{
			.avatarBox{
				position: relative;
				.avatar{
					width: 166rpx;
					height: 166rpx;
					border-radius: 100%;
					border: 4px solid #2AD5DD;
				}
				.level{
					position: absolute;
					background-color: #FFFFFF;
					border: 1px solid #2AD5DD;
					border-radius: 25rpx;
					font-size: 24rpx;
					color: #41B8C7;
					bottom: 0rpx;
					padding: 2rpx 0rpx;
					width: 150rpx;
					left: 8rpx;
					text-align: center;
					
				}
			}
			.nameBox{
				padding-left: 25rpx;
				.name{
					font-size: 40rpx;
					font-weight: bold;
					color: #262626;
				}
				.phone{
					font-size: 26rpx;
					color: #999999;
					padding-top: 15rpx;
				}
			}
		}
		.set{
			width: 90rpx;
			height: 90rpx;
		}
	}
	.statistic{
		margin-top: 50rpx;
		.statTpl{
			width: 50%;
			height: 210rpx;
			border-radius: 20rpx;
		}
		.bg1{
			background: url('/static/image/img16.png') no-repeat;
			background-size: 100% 100%;
		}
		.bg2{
			background: url('/static/image/img17.png') no-repeat;
			background-size: 100% 100%;
		}
		.bg3{
			background: url('/static/image/img18.png') no-repeat;
			background-size: 100% 100%;
		}
		.statName{
			font-size: 30rpx;
			padding-bottom: 14rpx;
			font-weight: bold;
			padding: 40rpx 0rpx 15rpx 37rpx;
			color: #FFFFFF;
		}
		.statNum{
			font-size: 60rpx;
			font-weight: bold;
			color:#FFFFFF;;
			padding: 0rpx 0rpx 0rpx 37rpx;
		}
	}
	.list-box {
		margin: 0rpx 25rpx;
		.user-list {
			border-radius: 28rpx;
			background: #ffffff;
			width: 100%;
			background: #ffffff;
			padding: 30rpx 30rpx;
			margin-bottom: 25rpx;
			.left-img {
				width: 40rpx;
				height: 40rpx;
			}
		
			view {
				padding-left: 20rpx;
				flex-grow: 1;
				font-size: 29rpx;
				font-weight: bold;
				color: #5d5d5d;
			}
		
			.right-img {
				width: 13rpx;
				height: 23rpx;
			}
		}
	}
</style>
