<template>
	<view class="container">
		<view class="vheigh"></view>
		<view class="user-section">
			<view class="user-info-box ">
				<view class="avatar"><image class="avatarImg" :src="userInfo.avatar||'../../static/error/missing-face.png'" mode="widthFix"></image></view>
				<view class="username">
					{{userInfo.nickname}}<text v-if="userInfo.level > 0">({{userInfo.level == 1? '会员': userInfo.level == 2 ? '店长': '代理'}})</text>
				</view>
				<view class="userphone" v-if="userInfo.phone">
					{{userInfo.phone|phoneXh}}
				</view>
			</view>
			<view class="rightBox">
				<view class="rz" v-if="userInfo.is_real">
					已认证 
				</view>
				<view class="card" v-if="userInfo.card_no">
					NO.{{userInfo.card_no}}
				</view>
				<view class="zc" v-if="userInfo.spread_store">
					{{userInfo.spread_store}}
				</view>
			</view>
		</view>
		<view class="cover-container">
			<view class="item-box">
				<view class="order-section">
					<view class="order-item" @click="navTo('/pages/order/order?state=0')" hover-class="common-hover" :hover-stay-time="50">
						<view class=" icon position-relative">
							<image class="icon-img" src="/static/icon/u1.png" mode="aspectFit"></image>
							<view class="corner" v-if="orderInfo.unpaid_count > 0">
								<text>{{ orderInfo.unpaid_count }}</text>
							</view>
						</view>
						<text>待付款</text>
					</view>
					<view class="order-item" @click="navTo('/pages/order/order?state=1')" hover-class="common-hover" :hover-stay-time="50">
						<view class=" icon position-relative">
							<image class="icon-img" src="/static/icon/u2.png" mode="aspectFit"></image>
							<view class="corner" v-if="orderInfo.unshipped_count > 0">
								<text>{{ orderInfo.unshipped_count }}</text>
							</view>
						</view>
						<text>待发货</text>
					</view>
					<view class="order-item" @click="navTo('/pages/order/order?state=2')" hover-class="common-hover" :hover-stay-time="50">
						<view class="icon position-relative">
							<image class="icon-img" src="/static/icon/u3.png" mode="aspectFit"></image>
							<view class="corner" v-if="orderInfo.received_count > 0">
								<text>{{ orderInfo.received_count }}</text>
							</view>
						</view>
						<text>待收货</text>
					</view>
					<view class="order-item" @click="navTo('/pages/order/order?state=3')" hover-class="common-hover" :hover-stay-time="50">
						<view class="icon position-relative">
							<image class="icon-img" src="/static/icon/u4.png" mode="aspectFit"></image>
							<!-- <view class="corner" v-if="orderInfo.complete_count > 0">
									<text>{{ orderInfo.complete_count }}</text>
								</view> -->
						</view>
						<text>已完成</text>
					</view>
				</view>
			</view>
			<!-- 浏览历史 -->
			<view class="history-section icon">
				<uni-list>
					<uni-list-item title="我的积分" @click="navTo('/pages/money/wallet')" thumb="/static/icon/v1.png"></uni-list-item>
					<uni-list-item title="积分转账" @click="navTo('/pages/user/withdmoenys')" thumb="/static/icon/v2.png"></uni-list-item>
					<uni-list-item title="我的推广" @click="navTo('/pages/user/extension')" thumb="/static/icon/v3.png"></uni-list-item>
					<uni-list-item title="邀请海报" @click="navTo('/pages/user/shareQrCode')" thumb="/static/icon/v4.png"></uni-list-item>
					<uni-list-item title="收货地址" @click="navTo('/pages/set/address')" thumb="/static/icon/v5.png"></uni-list-item>
					<!-- <uni-list-item title="加盟信息" @click="navTo('/pages/user/addmen?id=1')" thumb="/static/icon/v8.png"></uni-list-item> -->
					<uni-list-item title="店长核销" @click="openQr" thumb="/static/icon/v7.png"></uni-list-item>
					<uni-list-item title="设置" @click="navTo('/pages/set/set')" thumb="/static/icon/v6.png"></uni-list-item>
				</uni-list>
			</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 { orderData, getUserInfo,orderVerific } from '@/api/user.js';
import { saveUrl, interceptor } from '@/utils/loginUtils.js';
import weixinObj from "@/plugin/jweixin-module/index.js";
let startY = 0,
	moveY = 0,
	pageAtTop = true;
export default {
	components: {
		uniList,
		uniListItem
	},
	data() {
		return {
			coverTransform: 'translateY(0px)',
			coverTransition: '0s',
			moving: false,
			userDowm: 0, //卡片升级专属高度
			userMaxDowm: 0 //卡片最高高度
		};
	},
	filters: {
		phoneXh: function(value) {
			let a = value.substring(3,8)
			return value.replace(a,'*****');
		}
	},
	onShow() {
		// 判断是否已经登录
		if (this.hasLogin) {
			this.loadBaseData();
		}
	},
	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']),
		// #ifdef H5
		...mapState(['weichatObj'])
		// #endif
		
	},
	methods: {
		...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
		// 加载初始数据
		loadBaseData() {
			getUserInfo({})
				.then(({ data }) => {
					this.setUserInfo(data);
					if(+data.is_real!=1){
						uni.showModal({
							title: '提示',
							content: '为保护未成年人，未实名认证不可购买商品！',
							showCancel: false,
							success: res => {
								uni.navigateTo({
									url: '/pages/set/approve',
								});
							},
						});
					}
					// 获取用户数据完毕后在获取订单数据防止多次跳转到登录页
					orderData({})
						.then(({ data }) => {
							this.setOrderInfo(data);
						})
						.catch(e => {
							this.setOrderInfo({
								complete_count: 0, //完成
								received_count: 0, //待收货
								unshipped_count: 0, //待发货
								order_count: 0, //订单总数
								unpaid_count: 0 //待付款
							});
						});
				})
				.catch(e => {
					console.log(e);
				});
		},
		/**
		 * 统一跳转接口,拦截未登录路由
		 * navigator标签现在默认没有转场动画，所以用view
		 */
		navTo(url) {
			if (!this.hasLogin) {
				// 保存地址
				saveUrl();
				// 登录拦截
				interceptor();
			} else {
				uni.navigateTo({
					url
				});
			}
		},
		// 核销
		openQr() {
			let obj = this;
			// #ifndef H5
			uni.scanCode({
				success(e) {
					obj.orderVerific(e.result);
				}
			});
			// #endif
			// #ifdef H5
			this.weichatObj.scanQRCode({
				needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
				scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
				success: function(res) {
					obj.orderVerific(res.resultStr); // 当needResult 为 1 时，扫码返回的结果
				}
			});
			// #endif
		},
		// 核销请求
		orderVerific(data) {
			orderVerific({
				verify_code: data
			})
				.then(e => {
					uni.showModal({
						title: '核销',
						content: '已核销成功',
						showCancel: false
					});
				})
				.catch(e => {
					uni.showModal({
						title: '错误',
						content: JSON.stringify(e),
						showCancel: false
					});
					console.log(e);
				});
		}
	}
};
</script>
<style lang="scss">
page {
	height: 100%;
}
%flex-center {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
}
%section {
	display: flex;
	justify-content: space-around;
	align-content: center;
	background: #fff;
	border-radius: 10rpx;
}
.container {
	height: 100%;
	background-color: $page-color-base;
	background: url(../../static/img/indexBg.png) no-repeat;
	background-size: 100% auto;
	padding-top: 130rpx;
}
.content-box {
	height: 100%;
}
.vheigh {
	height: var(--status-bar-height);
	background-color: $base-color;
}
.user-section {
	margin: 15rpx 30rpx 0;
	background: url(../../static/img/userBg.png) no-repeat;
	background-size: 100% auto;
	height: 235rpx;
	position: relative;
	.user-info-box {
		color: white;
		text-align: center;
		.avatar {
			text-align: center;
			width: 100%;
			.avatarImg {
				margin-top: -70rpx;
				width: 140rpx;
				height: 140rpx;
				border: 5px solid rgba($color: #ffffff, $alpha: 0.3);
				border-radius: 99rpx;
				background-color: #ffffff;
			}
		}
		.username{
			font-size: $font-base;
		}
		.userphone{
			font-size:18rpx;
		}
	}
	.rightBox{
		color: white;
		position: absolute;
		text-align: right;
		bottom: 0;
		right: 0;
		padding-right: 20rpx;
		padding-bottom: 10rpx;
	}
}
.vip-card-box {
	display: flex;
	flex-direction: column;
	color: #f7d680;
	height: 240rpx;
	background: linear-gradient(left, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.8));
	border-radius: 16rpx 16rpx 0 0;
	overflow: hidden;
	position: relative;
	padding: 20rpx 24rpx;
	.card-bg {
		position: absolute;
		top: 20rpx;
		right: 0;
		width: 380rpx;
		height: 260rpx;
	}
	.b-btn {
		position: absolute;
		right: 20rpx;
		top: 16rpx;
		width: 132rpx;
		height: 40rpx;
		text-align: center;
		line-height: 40rpx;
		font-size: 22rpx;
		color: #36343c;
		border-radius: 20px;
		background: linear-gradient(left, #f9e6af, #ffd465);
		z-index: 1;
	}
	.tit {
		font-size: $font-base + 2rpx;
		color: #f7d680;
		margin-bottom: 28rpx;
		.iconfont {
			color: #f6e5a3;
			margin-right: 16rpx;
		}
	}
	.e-b {
		font-size: $font-sm;
		color: #d8cba9;
		margin-top: 10rpx;
	}
}
.cover-container {
	padding: 0 30rpx;
	position: relative;
	padding-bottom: 20rpx;
	.arc {
		position: absolute;
		left: 0;
		top: -34rpx;
		width: 100%;
		height: 36rpx;
	}
}
.tj-sction {
	@extend %section;
	.tj-item {
		@extend %flex-center;
		flex-direction: column;
		height: 140rpx;
		font-size: $font-sm;
		color: #75787d;
	}
	.num {
		font-size: $font-lg;
		color: $font-color-dark;
		margin-bottom: 8rpx;
	}
}

.item-box {
	border-radius: 10rpx;
	background-color: white;
	margin-top: 20rpx;
	.box-title {
		line-height: 1;
		padding: 30rpx;
		.title {
			font-size: $font-lg;
			font-weight: bold;
		}
		.link {
			font-size: $font-base - 2rpx;
			color: $font-color-light;
		}
	}
	.order-section {
		@extend %section;
		padding: 28rpx 0;
		.order-item {
			@extend %flex-center;
			width: 120rpx;
			height: 120rpx;
			border-radius: 10rpx;
			font-size: $font-sm;
			color: $font-color-dark;
		}
		.iconfont {
			font-size: 48rpx;
			margin-bottom: 18rpx;
			color: #fa436a;
		}
		.icon-shouhoutuikuan {
			font-size: 44rpx;
		}
		.icon {
			height: 50rpx;
			width: 48rpx;
			margin-bottom: 18rpx;
			background-size: 100%;
			background-repeat: no-repeat;
			background-position: center;
			.icon-img {
				width: 100%;
				height: 100%;
			}
		}
	}
}

.history-section {
	// padding: 30rpx 0 0;
	margin-top: 20rpx;
	background: #fff;
	border-radius: 10rpx;
	overflow: hidden;
	.sec-header {
		display: flex;
		align-items: center;
		font-size: $font-base;
		color: $font-color-dark;
		line-height: 40rpx;
		margin-left: 30rpx;
		padding-top: 30rpx;
		.iconfont {
			font-size: 44rpx;
			color: $color-red;
			margin-right: 16rpx;
			line-height: 40rpx;
		}
	}
	.h-list {
		white-space: nowrap;
		padding: 30rpx 30rpx 0;
		.h-list-image {
			display: inline-block;
			width: 160rpx;
			height: 160rpx;
			margin-right: 20rpx;
			border-radius: 10rpx;
		}
	}
}
</style>
