<template>
	<view class="container">
		<view class="vheigh"></view>
		<view class="top-bg"><view class="title">会员中心</view></view>
		<view class="userinfo-box">
			<view class="userinfo-info">
				<view class="portrait-box"><image class="portrait" :src="userInfo.avatar || '/static/error/missing-face.png'"></image></view>
				<view class="username clamp">y{{ userInfo.nickname || '游客' }}</view>
				<view class="phone clamp">{{ userInfo.phone | phrule }}</view>
				<view class="level clamp">{{ userInfo.level_info ? userInfo.level_info.name : '会员' }}</view>
			</view>
			<view class="money-box flex">
				<view class="money-box-item" @click="navTo('/pages/money/wallet')">
					<view class="money-box-item-price">{{ userInfo.now_money || '0.00' }}</view>
					<view class="money-box-item-name">钱包余额</view>
				</view>
				<view class="xian"></view>
				<view class="money-box-item" @click="navTo('/pages/user/scoreAccumulate')">
					<view class="money-box-item-price">{{ userInfo.integral || '0.00' }}</view>
					<view class="money-box-item-name">兑换券余额</view>
				</view>
				<view class="xian"></view>
				<view class="money-box-item" @click="navTo('/pages/user/barter')">
					<view class="money-box-item-price">{{ userInfo.barter_integral || '0.00' }}</view>
					<view class="money-box-item-name">钱</view>
				</view>
			</view>
			<!-- <view class="userinfo-box-tip">本月分享：0人，本月您已复购：0PV</view> -->
		</view>
		<view class="main-box">
			<view class="main-item" @click="navTo('/pages/order/order?state=0')">
				<view class="image"><image class="image1" src="../../static/icon/u1.png" mode=""></image></view>
				<view class="main-item-font">订单管理</view>
			</view>
			<view class="main-item" @click="navTo('/pages/set/address')">
				<view class="image"><image class="image2" src="../../static/icon/u2.png" mode=""></image></view>
				<view class="main-item-font">地址管理</view>
			</view>
			<view class="main-item" @click="navTo('/pages/money/recharge?type=1')">
				<view class="image"><image class="image3" src="../../static/icon/u3.png" mode=""></image></view>
				<view class="main-item-font">电子钱包</view>
			</view>
			<view class="main-item" @click="navTo('/pages/user/award')">
				<view class="image"><image class="image4" src="../../static/icon/u4.png" mode=""></image></view>
				<view class="main-item-font">奖金管理</view>
			</view>
			<view class="main-item" @click="navTo('/pages/user/shareQrCode')">
				<view class="image"><image class="image6" src="../../static/icon/u6.png" mode=""></image></view>
				<view class="main-item-font">分享二维码</view>
			</view>
			<view class="main-item" @click="navTo('/pages/user/team')">
				<view class="image"><image class="image5" src="../../static/icon/u5.png" mode=""></image></view>
				<view class="main-item-font">分享查询</view>
			</view>
			<view class="main-item" @click="navTo('/pages/user/favorites')">
				<view class="image"><image class="image7" src="../../static/icon/u7.png" mode=""></image></view>
				<view class="main-item-font">我的收藏</view>
			</view>
			<view class="main-item" @click="navTo('/pages/set/set')">
				<view class="image"><image class="image8" src="../../static/icon/u8.png" mode=""></image></view>
				<view class="main-item-font">个人资料</view>
			</view>
			<!-- <view class="main-item" @click="navTo('/pages/user/about')">
				<view class="image"><image class="image9" src="../../static/icon/u9.png" mode=""></image></view>
				<view class="main-item-font">关于我们</view>
			</view> -->
			<view class="main-item" @click="navTo('/pages/merchant/merchant')" v-if="userInfo.store != 0">
				<view class="image"><image class="image10" src="../../static/icon/u10.png" mode=""></image></view>
				<view class="main-item-font">店铺管理</view>
			</view>
		</view>
	</view>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
import uniList from '@/components/uni-list/uni-list.vue';
import uniListItem from '@/components/uni-list-item/uni-list-item.vue';
import { getAddressList, setAddressDefault, addressDel } from '@/api/user.js';
import { orderData, getUserInfo } from '@/api/user.js';
import { saveUrl, interceptor } from '@/utils/loginUtils.js';
let startY = 0,
	moveY = 0,
	pageAtTop = true;
export default {
	components: {
		uniList,
		uniListItem
	},
	data() {
		return {};
	},
	onShow() {
		// 判断是否已经登录
		if (this.hasLogin) {
			this.loadBaseData();
		}
	},
	filters: {
		phrule(provider) {
			if (provider) {
				var subStr1 = provider.substr(0, 3);
				var subStr2 = provider.substr(provider.length - 4, 4);
				var subStr = subStr1 + '...' + subStr2;
				provider = subStr;
			}
			return provider;
		}
	},
	onReady() {
		// 初始化获取页面宽度
		uni.createSelectorQuery()
			.select('.container')
			.fields(
				{
					size: true
				},
				data => {
					// 计算最多下拉的高度
					this.userDowm = Math.floor((data.width / 750) * 185);
					// 计算最大触发修改高度事件
					this.userMaxDowm = Math.floor((data.width / 750) * 250);
				}
			)
			.exec();
	},
	computed: {
		...mapState('user', ['userInfo', 'orderInfo', 'hasLogin'])
	},
	methods: {
		...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
		// 加载初始数据
		loadBaseData() {
			getUserInfo({})
				.then(({ data }) => {
					this.setUserInfo(data);
					// 获取用户数据完毕后在获取订单数据防止多次跳转到登录页
					getAddressList({
						page: 1,
						limit: 100
					}).then(({ data }) => {
						if (data.length == 0) {
							uni.showModal({
								title: '提示',
								content: '您未填写收货地址，请您先去填写收货地址',
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										uni.navigateTo({
											url: '/pages/set/address'
										});
									}
								}
							});
						}
					});
				})
				.catch(e => {
					console.log(e);
				});
		},
		/**
		 * 统一跳转接口,拦截未登录路由
		 * navigator标签现在默认没有转场动画，所以用view
		 */
		navTo(url) {
			if (!this.hasLogin) {
				// 保存地址
				saveUrl();
				// 登录拦截
				interceptor();
			} else {
				uni.navigateTo({
					url
				});
			}
		}
	}
};
</script>
<style lang="scss">
page,
.container {
	min-height: 100%;
	height: auto;
	background: #f8f8f8;
}
.vheigh {
	height: var(--status-bar-height);
	background-color: $base-color;
}
.top-bg {
	width: 750rpx;
	height: auto;
	background: #5dbc7c;
	padding: 20rpx 0 212rpx;
	.title {
		font-size: 34rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #ffffff;
		text-align: center;
	}
}
.userinfo-box {
	width: 690rpx;
	background: #ffffff;
	border-radius: 19rpx;
	margin: -106rpx auto 0;
	.userinfo-info {
		position: relative;
		top: -70rpx;
		display: flex;
		align-items: center;
		flex-direction: column;
		line-height: 1;
		.portrait-box {
			width: 140rpx;
			height: 140rpx;
			border-radius: 50%;
			.portrait {
				width: 140rpx;
				height: 140rpx;
				border-radius: 50%;
			}
		}
		.username {
			margin-top: 24rpx;
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #333333;
		}
		.phone {
			margin-top: 16rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
		}
		.level {
			background: #ff4c4c;
			border-radius: 30rpx;
			padding: 10rpx 30rpx;
			margin-top: 10rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #fff;
		}
	}
	.money-box {
		position: relative;
		top: -30rpx;
		.money-box-item {
			width: 50%;
			padding: 2rpx 0 8rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			line-height: 1;
			.money-box-item-price {
				font-size: 36rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}
			.money-box-item-name {
				margin-top: 18rpx;
				font-size: 22rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #666666;
			}
		}
		.xian {
			width: 1px;
			height: 78rpx;
			background: #ebebeb;
		}
	}
	.userinfo-box-tip {
		padding: 0 0 24rpx;
		text-align: center;
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #ff4c4c;
	}
}
.main-box {
	width: 690rpx;
	background: #ffffff;
	border-radius: 19rpx;
	margin: 20rpx auto 0;
	display: flex;
	flex-wrap: wrap;
	justify-content: center;
	padding-bottom: 98rpx;
	.main-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 33%;
		padding-top: 78rpx;
		.image {
			display: flex;
			justify-content: center;
			width: 70rpx;
			height: 70rpx;
		}
		.image1 {
			width: 60rpx;
			height: 68rpx;
		}
		.image2 {
			width: 44rpx;
			height: 58rpx;
		}
		.image3 {
			width: 68rpx;
			height: 68rpx;
		}
		.image4 {
			width: 66rpx;
			height: 70rpx;
		}
		.image5 {
			width: 76rpx;
			height: 62rpx;
		}
		.image6 {
			width: 70rpx;
			height: 70rpx;
		}
		.image7 {
			width: 60rpx;
			height: 54rpx;
		}
		.image8 {
			width: 60rpx;
			height: 60rpx;
		}
		.image9 {
			width: 54rpx;
			height: 54rpx;
		}
		.image10 {
			width: 60rpx;
			height: 60rpx;
		}
		.main-item-font {
			margin-top: 24rpx;
			font-size: 26rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #333333;
			line-height: 21px;
		}
	}
}
</style>
