<template>
	<view class="center">
		<view class="nav_area">
			<view class="" style="font-size: 18px;color: #101010;">我的</view>
		</view>
		<!-- <uni-sign-in ref="signIn"></uni-sign-in> -->
		<view class="userInfo">
			<cloud-image width="120rpx" height="120rpx" style="margin-right: 8px;border-radius: 50%;overflow: hidden;" v-if="hasLogin&&userInfo.avatar" :src="userInfo.avatar"></cloud-image>
			
			<view v-else class="defaultAvatarUrl"></view>
			
			<view class="logo-title">
				<text class="uer-name" v-if="hasLogin">ID: {{userInfo.id}}</text>
				<text class="uer-name" v-else @click.capture="toUserInfo">点击登陆</text>
			</view>
			<view class="logo-out" @click.stop="logoOut" v-if="hasLogin">
			</view>
		</view>
		<view class="balance_area" v-if="userType=='user'||userType=='agent'">
			<view class="item">
				<text style="font-size: 20px;margin-bottom: 10rpx;">{{userInfo.balance}}</text>
				<view class="item_btn" :style="showRecharge?'justify-content: space-between;':''">
					<text>余额</text>
					<text v-if="showRecharge" @click="toRecharge()" style="color: #4976FF;">充值</text>
				</view>
			</view>
			<view class="line"></view>
			<view class="item">
				<text style="font-size: 20px;margin-bottom: 10rpx;">{{userInfo.withdrawal_balance}}</text>
				<text>可提余额</text>
			</view>
		</view>
		<uni-grid class="grid" :column="4" :showBorder="false" :square="true" v-if="0">
			<uni-grid-item class="item" v-for="(item,index) in gridList" @click.native="tapGrid(index)" :key="index">
				<uni-icons class="icon" color="#007AFF" :type="item.icon" size="26"></uni-icons>
				<text class="text">{{item.text}}</text>
			</uni-grid-item>
		</uni-grid>
		<uni-list class="center-list" >
			<template v-if="userType=='user'">
				<uni-list-item v-for="(item,i) in ucenterList[0]" :title="item.title" link :rightText="item.rightText" :key="i"
					:clickable="true" :to="item.to" @click="ucenterListClick(item)" :show-extra-icon="true"
					:extraIcon="{type:item.icon,color:'#999'}">
					<template v-slot:footer>
						<view class="icon_new" v-if="item.iconN">
							<image :src="'../../static/user/icon_'+item.iconN+'.png'" mode=""></image>
						</view>
						<view v-if="item.showBadge" class="item-footer">
							<text class="item-footer-text">{{item.rightText}}</text>
							<view class="item-footer-badge"></view>
						</view>
					</template>
				</uni-list-item>
			</template>
			<template v-if="userType=='agent'">
				<uni-list-item v-for="(item,i) in ucenterList[1]" :title="item.title" link :rightText="item.rightText" :key="item.title"
					:clickable="true" :to="item.to" @click="ucenterListClick(item)" :show-extra-icon="true"
					:extraIcon="{type:item.icon,color:'#999'}">
					<template v-slot:footer>
						<view class="icon_new" v-if="item.iconN">
							<image :src="'../../static/user/icon_'+item.iconN+'.png'" mode=""></image>
						</view>
						<view v-if="item.showBadge" class="item-footer">
							<text class="item-footer-text">{{item.rightText}}</text>
							<view class="item-footer-badge"></view>
						</view>
					</template>
				</uni-list-item>
			</template>
			<template v-if="userType=='user'||userType=='agent'">
				<uni-list-item v-for="(item,i) in ucenterList[2]" :title="item.title" link :rightText="item.rightText" :key="item.title"
					:clickable="true" :to="item.to" @click="ucenterListClick(item)" :show-extra-icon="true"
					:extraIcon="{type:item.icon,color:'#999'}">
					<template v-slot:footer>
						<view class="icon_new" v-if="item.iconN">
							<image :src="'../../static/user/icon_'+item.iconN+'.png'" mode=""></image>
						</view>
						<view v-if="item.showBadge" class="item-footer">
							<text class="item-footer-text">{{item.rightText}}</text>
							<view class="item-footer-badge"></view>
						</view>
					</template>
				</uni-list-item>
			</template>
			<template v-if="userType=='background'">
				<uni-list-item v-for="(item,i) in ucenterList[3]" :title="item.title" link :rightText="item.rightText" :key="item.title"
					:clickable="true" :to="item.to" @click="ucenterListClick(item)" :show-extra-icon="true"
					:extraIcon="{type:item.icon,color:'#999'}">
					<template v-slot:footer>
						<view class="icon_new" v-if="item.iconN">
							<image :src="'../../static/user/icon_'+item.iconN+'.png'" mode=""></image>
						</view>
						<view v-if="item.showBadge" class="item-footer">
							<text class="item-footer-text">{{item.rightText}}</text>
							<view class="item-footer-badge"></view>
						</view>
					</template>
				</uni-list-item>
			</template>
		</uni-list>
	</view>
</template>

<script>
	import API from '@/common/js/api.js'
	import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
	import callCheckVersion from '@/uni_modules/uni-upgrade-center-app/utils/call-check-version';
	// #ifdef APP
	import UniShare from '@/uni_modules/uni-share/js_sdk/uni-share.js';
	const uniShare = new UniShare()
	// #endif
	// const db = uniCloud.database();
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
	// import updateTabBar from '@/common/js/updateTabBar.js'
	export default {
		// #ifdef APP
		onBackPress({from}) {
			if(from=='backbutton'){
				this.$nextTick(function(){
					uniShare.hide()
				})
				return uniShare.isShow;
			}
		},
		// #endif
		data() {
			return {
				userType: 'background',//user|agent|background
				gridList: [{
						"text": this.$t('mine.showText'),
						"icon": "chat"
					},
					{
						"text": this.$t('mine.showText'),
						"icon": "cloud-upload"
					},
					{
						"text": this.$t('mine.showText'),
						"icon": "contact"
					},
					{
						"text": this.$t('mine.showText'),
						"icon": "download"
					}
				],
				ucenterList: [
					[
						{
							"title": '中奖记录',
							"to": '/pages/recordsList/recordsList?type=ZJ',
							"icon": "compose",
							"iconN": "target"
						},
						{
							"title": '消费记录',
							"to": '/pages/recordsList/recordsList?type=XF',
							"icon": "compose",
							"iconN": "card"
						},
						{
							"title": '充值记录',
							"to": '/pages/recordsList/recordsList?type=CZ',
							"icon": "compose",
							"iconN": "gold"
						},
						{
							"title": '提现记录',
							"to": '/pages/recordsList/recordsList?type=TX',
							"icon": "compose",
							"iconN": "wallet"
						},
						{
							"title": '规则中心',
							"event": '',
							"icon": "compose",
							"iconN": "smile"
						}
					],
					[
						{
							"title": '充值记录',
							"to": '/pages/recordsList/recordsList?type=CZ',
							"icon": "compose",
							"iconN": "card"
						},
						{
							"title": '上分记录',
							"to": '/pages/recordsList/recordsList?type=UP',
							"icon": "compose",
							"iconN": "gold"
						},
						{
							"title": '下分记录',
							"to": '/pages/recordsList/recordsList?type=DOWN',
							"icon": "compose",
							"iconN": "wallet"
						},
						{
							"title": '邀请码',
							"to": '/pages/ucenter/invite/invite',
							"icon": "compose",
							"iconN": "menu"
						},
					],
					[
						{
							"title": '在线客服',
							"event": '',
							"icon": "compose",
							"iconN": "people"
						},
						{
							"title": '修改密码',
							"event": 'toChangePWD',
							"icon": "compose",
							"iconN": "lock"
						},
						{
							"title": '关于',
							"to": '/pages/ucenter/about/about',
							"icon": "compose",
							"iconN": "list"
						},
					],
					[
						{
							"title": '上分记录',
							"to": '/pages/recordsList/recordsList?type=UP',
							"iconN": "gold",
							"icon": "download"
						},
						{
							"title": '下分记录',
							"to": '/pages/recordsList/recordsList?type=DOWN',
							"iconN": "wallet",
							"icon": "download"
						},
						{
							"title": '关于',
							"to": '/pages/about/about',
							"iconN": "list",
							"icon": "download"
						},
						{
							"title": '清除缓存',
							"event": 'clearCache',
							"iconN": "clear",
							"icon": "download"
						},
					],
					// [
					// 	// #ifdef APP-PLUS
					// 	{
					// 		"title": this.$t('mine.signInByAd'),
					// 		"event": 'signInByAd',
					// 		"icon": "compose"
					// 	},
					// 	// #endif
					// 	{
					// 		"title": this.$t('mine.signIn'),
					// 		"event": 'signIn',
					// 		"icon": "compose"
					// 	},
					// 	// #ifdef APP-PLUS
					// 	{
					// 		"title": this.$t('mine.toEvaluate'),
					// 		"event": 'gotoMarket',
					// 		"icon": "star"
					// 	},
					// 	//#endif
					// 	{
					// 		"title":this.$t('mine.readArticles'),
					// 		"to": '/pages/ucenter/read-news-log/read-news-log',
					// 		"icon": "flag"
					// 	},
					// 	{
					// 		"title": this.$t('mine.myScore'),
					// 		"to": '',
					// 		"event": 'getScore',
					// 		"icon": "paperplane"
					// 	}
					// 	// #ifdef APP-PLUS
					// 	, {
					// 		"title": this.$t('mine.invite'),
					// 		"event": 'share',
					// 		"icon": "redo"
					// 	}
					// 	// #endif
					// ],
					// [{
					// 	"title": this.$t('mine.feedback'),
					// 	"to": '/uni_modules/uni-feedback/pages/opendb-feedback/opendb-feedback',
					// 	"icon": "help"
					// }, {
					// 	"title": this.$t('mine.settings'),
					// 	"to": '/pages/ucenter/settings/settings',
					// 	"icon": "gear"
					// }]
				],
				listStyles: {
					"height": "150rpx", // 边框高度
					"width": "150rpx", // 边框宽度
					"border": { // 如果为 Boolean 值，可以控制边框显示与否
						"color": "#eee", // 边框颜色
						"width": "1px", // 边框宽度
						"style": "solid", // 边框样式
						"radius": "100%" // 边框圆角，支持百分比
					}
				},
				showRecharge: true,
				userInfo: {},
			}
		},
		onLoad() {
			//#ifdef APP-PLUS
			this.ucenterList[this.ucenterList.length - 2].unshift({
				title:this.$t('mine.checkUpdate'),// this.this.$t('mine.checkUpdate')"检查更新"
				rightText: this.appVersion.version + '-' + this.appVersion.versionCode,
				event: 'checkVersion',
				icon: 'loop',
				showBadge: this.appVersion.hasNew
			})
			//#endif
			if(uni.getStorageSync('userType')=='background'){
				this.getAdminUserInfo();
			}else{
				this.getUserInfo();
			}
			
		},
		onShow() {
			this.userType = uni.getStorageSync('userType');
			// if(this.userType=='user'){
			// 	updateTabBar.changeUser();
			// }else if(this.userType=='agent'){
			// 	updateTabBar.changeAgent();
			// }else if(this.userType=='background'){
			// 	updateTabBar.changebackground();
			// }
		},
		computed: {
			// userInfo() {
			// 	return store.userInfo
			// },
			hasLogin(){
				return uni.getStorageSync('user_token');
				// return store.hasLogin
			},
			// #ifdef APP-PLUS
			appVersion() {
				return getApp().appVersion
			},
			// #endif
			appConfig() {
				return getApp().globalData.config
			},
			
		},
		methods: {
			toSettings() {
				uni.navigateTo({
					url: "/pages/ucenter/settings/settings"
				})
			},
			signIn() { //普通签到
				this.$refs.signIn.open()
			},
			signInByAd(){ //看激励视频广告签到
				this.$refs.signIn.showRewardedVideoAd()
			},
			/**
			 * 个人中心项目列表点击事件
			 */
			ucenterListClick(item) {
				if (!item.to && item.event) {
					this[item.event]();
				}
			},
			async checkVersion() {
				let res = await callCheckVersion()
				console.log(res);
				if (res.result.code > 0) {
					checkUpdate()
				} else {
					uni.showToast({
						title: res.result.message,
						icon: 'none'
					});
				}
			},
			toUserInfo() {
				uni.navigateTo({
					url: '/uni_modules/uni-id-pages/pages/login/login-withpwd'
				})
				// uni.navigateTo({
				// 	url: '/uni_modules/uni-id-pages/pages/userinfo/userinfo'
				// })
			},
			logoOut(e){
				let url = '/user/LogOut';
				if(this.userType=='agent'){
					url = '/agentTeam/logOut';
				}else if(this.userType=='background'){
					url = '/adminUser/logOut';
				}
				
				API.request(url, {}, res=>{
					uni.showToast({
						title: res.msg
					})
					
					uni.removeStorageSync('user_token');
					setTimeout(()=>{
						uni.reLaunch({
							url: '/uni_modules/uni-id-pages/pages/login/login-withpwd'
						})
					}, 1000)
				})
			},
			tapGrid(index) {
				uni.showToast({
					// title: '你点击了，第' + (index + 1) + '个',
					title: this.$t('mine.clicked') + " " + (index + 1) ,
					icon: 'none'
				});
			},
			/**
			 * 去应用市场评分
			 */
			gotoMarket() {
				// #ifdef APP-PLUS
				if (uni.getSystemInfoSync().platform == "ios") {
					// 这里填写appstore应用id
					let appstoreid = this.appConfig.marketId.ios; // 'id1417078253';
					console.log({appstoreid});
					plus.runtime.openURL("itms-apps://" + 'itunes.apple.com/cn/app/wechat/' + appstoreid + '?mt=8',err=>{
						console.log('plus.runtime.openURL err:' + JSON.stringify(err));
					});
				}
				if (uni.getSystemInfoSync().platform == "android") {
					var Uri = plus.android.importClass("android.net.Uri");
					var uri = Uri.parse("market://details?id=" + this.appConfig.marketId.android);
					var Intent = plus.android.importClass('android.content.Intent');
					var intent = new Intent(Intent.ACTION_VIEW, uri);
					var main = plus.android.runtimeMainActivity();
					main.startActivity(intent);
				}
				// #endif
			},
			/**
			 * 获取积分信息
			 */
			getScore() {
				if (!this.userInfo) return uni.showToast({
					title: this.$t('mine.checkScore'),
					icon: 'none'
				});
				uni.showLoading({
					mask: true
				})
				// db.collection("uni-id-scores")
				// 	.where('"user_id" == $env.uid')
				// 	.field('score,balance')
				// 	.orderBy("create_date", "desc")
				// 	.limit(1)
				// 	.get()
				// 	.then((res) => {
				// 		console.log(res);
				// 		const data = res.result.data[0];
				// 		let msg = '';
				// 		msg = data ? (this.$t('mine.currentScore')+ data.balance) : this.$t('mine.noScore');
				// 		uni.showToast({
				// 			title: msg,
				// 			icon: 'none'
				// 		});
				// 	}).finally(()=>{
				// 		uni.hideLoading()
				// 	})
			},
			async share() {
				let {result} = {}
				// = await db.collection('uni-id-users').where("'_id' == $cloudEnv_uid").field('my_invite_code').get()
				let myInviteCode = result.data[0].my_invite_code
				if(!myInviteCode){
					return uni.showToast({
						title: '请检查uni-config-center中uni-id配置，是否已启用 autoSetInviteCode',
						icon: 'none'
					});
				}
				console.log({myInviteCode});
				let {
					appName,
					logo,
					company,
					slogan
				} = this.appConfig.about
				// #ifdef APP-PLUS
				uniShare.show({
					content: { //公共的分享类型（type）、链接（herf）、标题（title）、summary（描述）、imageUrl（缩略图）
						type: 0,
						href: this.appConfig.h5.url +
							`/#/pages/ucenter/invite/invite?code=uniInvitationCode:${myInviteCode}`,
						title: appName,
						summary: slogan,
						imageUrl: logo +
							'?x-oss-process=image/resize,m_fill,h_100,w_100' //压缩图片解决，在ios端分享图过大导致的图片失效问题
					},
					menus: [{
							"img": "/static/app-plus/sharemenu/wechatfriend.png",
							"text": this.$t('common.wechatFriends'),
							"share": {
								"provider": "weixin",
								"scene": "WXSceneSession"
							}
						},
						{
							"img": "/static/app-plus/sharemenu/wechatmoments.png",
							"text": this.$t('common.wechatBbs'),
							"share": {
								"provider": "weixin",
								"scene": "WXSceneTimeline"
							}
						},
						{
							"img": "/static/app-plus/sharemenu/weibo.png",
							"text": this.$t('common.weibo'),
							"share": {
								"provider": "sinaweibo"
							}
						},
						{
							"img": "/static/app-plus/sharemenu/qq.png",
							"text": "QQ",
							"share": {
								"provider": "qq"
							}
						},
						{
							"img": "/static/app-plus/sharemenu/copyurl.png",
							"text": this.$t('common.copy'),
							"share": "copyurl"
						},
						{
							"img": "/static/app-plus/sharemenu/more.png",
							"text": this.$t('common.more'),
							"share": "shareSystem"
						}
					],
					cancelText: this.$t('common.cancelShare'),
				}, e => { //callback
					console.log(e);
				})
				// #endif
			},
			
			
			toChangePWD(){
				uni.navigateTo({
					url: "/uni_modules/uni-id-pages/pages/userinfo/change_pwd/change_pwd"
				})
			},
			toRecharge(){
				uni.navigateTo({
					url: "/pages/recharge/recharge"
				})
			},
			clearCache(){
				uni.showModal({
					title:'温馨提示',
					content:"确定后，将清除所有缓存数据",
					success() {
						
						// let tk = uni.getStorageSync('user_token');
						// let ut = uni.getStorageSync('userType');
						// uni.clearStorageSync();
						// uni.setStorageSync('user_token', tk);
						// uni.setStorageSync('userType', ut);
					}
				})
			},
			getUserInfo(){
				API.request('/user/userInfo', {}, res=>{
					this.userInfo = res.data.data;
				})
			},
			getAdminUserInfo(){
				API.request('/adminUser/userInfo', {}, res=>{
					this.userInfo = res.data.data;
				})
			}
			
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}

	page {
		background-color: #f8f8f8;
	}
	/* #endif*/
	
	.center {
		flex: 1;
		flex-direction: column;
		background-color: #fff;
		padding: 90rpx 0px 0px;
		min-height: calc(100vh - 50px);
	}
	
	.nav_area {
		position: fixed;
		top: 0px;
		left: 0px;
		z-index: 99;
		background-color: #fff;
		box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.05);
		width: 700rpx;
		height: 88rpx;
		padding: 0px 25rpx;
		padding-top: var(--status-bar-height);
		box-sizing: content-box;
		color: #b0b0b0;
		font-size: 14px;
		text-align: center;
		line-height: 88rpx;
	}
	.userInfo {
		// padding: 20rpx;
		// padding-top: 60px;
		// background-image: url(../../static/uni-center/headers.png);
		flex-direction: row;
		align-items: center;
		padding: 40rpx;
	}
	.defaultAvatarUrl{
		width: 120rpx;
		height: 120rpx;
		// background-color: #007aff;
		border-radius: 100%;
		justify-content: center;
		align-items: center;
		background-image: url("../../static/user/avatar.png");
		background-size: contain;
		margin-right: 8px;
	}

	.logo-title {
		flex: 1;
		align-items: center;
		justify-content: space-between;
		flex-direction: row;
	}

	.uer-name {
		height: 100rpx;
		line-height: 100rpx;
		font-size: 28rpx;
		color: #999;
	}
	.logo-out{
		width: 48rpx;
		height: 48rpx;
		background-image: url("../../static/user/logout.png");
		background-size: contain;
	}
	
	.balance_area{
		width: 670rpx;
		height: 160rpx;
		background-image: linear-gradient(to right, #B2E6FF, #57ACFF);
		border-radius: 10px;
		display: flex;
		flex-direction: row;
		align-items: center;
		position: relative;
		margin: 0px 40rpx 20rpx;
		
		.item{
			width: 50%;
			text-align: center;
			color: #fff;
			font-size: 12px;
			line-height: 20px;
			
			.item_btn{
				width: 60%;
				height: 100%;
				margin: 0 auto;
				display: flex;
				flex-direction: row;
				justify-content: center;
			}
		}
		.line{
			position: absolute;
			width: 1px;
			height: 30rpx;
			top: 65rpx;
			left: 50%;
			background-color: #fff;
		}
	}

	.center-list {
		// margin-bottom: 30rpx;
		background-color: #f9f9f9;
	}

	.center-list-cell {
		width: 750rpx;
		background-color: #007AFF;
		height: 40rpx;
	}

	.grid {
		background-color: #FFFFFF;
		margin-bottom: 6px;
	}

	.uni-grid .text {
		font-size: 16px;
		height: 25px;
		line-height: 25px;
		color: #817f82;
	}

	.uni-grid .item ::v-deep .uni-grid-item__box {
		justify-content: center;
		align-items: center;
	}

	.center-list /deep/ .uni-list-item__container{
		padding: 12px 40rpx;
	}
	.center-list /deep/ .uni-icon-wrapper{
		padding: 0px 40rpx 0px 10px;
		font-size: 12px !important;
	}
	.center-list /deep/ .uni-list--border:after{
		content: none;
	}

	/*修改边线粗细示例*/
	/* #ifndef APP-NVUE */
	.center-list ::v-deep .uni-list--border:after {
		-webkit-transform: scaleY(0.2);
		transform: scaleY(0.2);
		margin-left: 80rpx;
	}

	.center-list ::v-deep .uni-list--border-top,
	.center-list ::v-deep .uni-list--border-bottom {
		display: none;
	}

	/* #endif */
	
	.icon_new{
		position: absolute;
		left: 32rpx;
		top: 22rpx;
		width: 48rpx;
		height: 48rpx;
		background-color: #fff;
		image{
			width: 48rpx;
			height: 48rpx;
		}
	}
	.item-footer {
		flex-direction: row;
		align-items: center;
	}

	.item-footer-text {
		color: #999;
		font-size: 24rpx;
		padding-right: 10rpx;
	}

	.item-footer-badge {
		width: 20rpx;
		height: 20rpx;
		/* #ifndef APP-NVUE */
		border-radius: 50%;
		/* #endif */
		/* #ifdef APP-NVUE */
		border-radius: 10rpx;
		/* #endif */
		background-color: #DD524D;
	}
</style>