<template>
	<view class="container">
		<!-- #ifdef APP-PLUS -->
		<view class="bar-height"></view>
		<!-- #endif -->
		<!-- <scroll-view class="content-box" scroll-y="true"> -->
		<view class="user-section">
			<view class="bg"><image src="../../static/user/ground.png" mode=""></image></view>
			<!-- top -->
			<view class="user-info-box ">
				<view class="detail flex" @click="navTo('/pages/userinfo/userinfo')">
					<view class="portrait-box"><image class="portrait" :src="userInfo.avatar || '/static/error/missing-face.png'"></image></view>
					<view class="info-box">
						<view class="username">{{ userInfo.nickname || '游客' }}</view>
						<view class="font-size-sm" v-if="userInfo.uid">{{ userInfo.uid }}</view>
					</view>
				</view>
				<view class="config">
					<text class="setting " @click="navTo('/pages/userinfo/userinfo')"><image src="../../static/user/set.png" mode=""></image></text>
					设置
				</view>
			</view>
			<!-- 会员卡功能 -->
		</view>
		<view class="item-box">
			<image class="arc" src="/static/img/arc.png"></image>
			<view class="mendian">
				<view class="mendian-lift" v-if="userInfo.level != 0">
					<view class="img"><image src="../../static/img/mendian01.png" mode=""></image></view>
					<text class="null-mendian">{{ userInfo.store_name}}</text>
				</view>
				<view class="mendian-lift" v-else>
					<view class="img"><image src="../../static/img/mendian02.png" mode=""></image></view>
					<text class="null-mendian">无绑定门店</text>
				</view>
				<view class="mendian-lift" v-if="userInfo.level != 0">
					<text class="duihuan">{{userInfo.vip}}</text>
				</view>
				<view class="mendian-right"  @click="go()" v-else>
					<text class="duihuan">兑换会员卡</text>
					<text class="iconfont iconenter"></text>
				</view>
			</view>
			<view class="my-order">
				<view class="box-title flex borde-b">
					<view class="title" @click="go"><text>我的订单</text></view>
					<view class="link" @click="navTo('/pages/order/order?state=5')" hover-class="common-hover"><text class="iconfont iconenter"></text></view>
				</view>
				<view class="order-section">
					<view class="order-item" @click="navTo('/pages/order/order?state=1')" hover-class="common-hover" :hover-stay-time="50">
						<view class=" icon1 position-relative">
							<image class="icon-img" src="/static/icon/i2.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="icon1 position-relative">
							<image class="icon-img" src="/static/icon/i3.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="icon1 position-relative">
							<image class="icon-img" src="/static/icon/i4.png" mode="aspectFit"></image>
							<view class="corner" v-if="orderInfo.evaluated_count > 0">
								<text>{{ orderInfo.evaluated_count }}</text>
							</view>
						</view>
						<text>待评价</text>
					</view>
					<view class="order-item" @click="navTo('/pages/order/refundOrder?state=0')" hover-class="common-hover" :hover-stay-time="50">
						<view class=" icon1 position-relative"><image class="icon-img" src="/static/icon/i1.png" mode="aspectFit"></image></view>
						<text>退款</text>
					</view>
				</view>
			</view>
		</view>
		<!-- <view class="cover-container"> -->
		<view class="vip" @click="navTo('/pages/wallet/openMember')" v-if="userInfo.level < 3"><image src="../../static/user/upgrade.png" mode=""></image></view>

		<!-- 订单 -->

		<view class="item-box " v-if="isShowIllegality">
			<view class="order-section  order">
				<view class="order-item" @click="navTo('/pages/wallet/wallet')" hover-class="common-hover" :hover-stay-time="50">
					<view class=" icon position-relative"><image class="icon-img" src="../../static/user/balance.png" mode="aspectFit"></image></view>
					<text>我的余额</text>
				</view>
				<view class="order-item" @click="navTo('/pages/award/award')" hover-class="common-hover" :hover-stay-time="50">
					<view class=" icon position-relative"><image class="icon-img" src="../../static/user/commission.png" mode="aspectFit"></image></view>
					<text>我的佣金</text>
				</view>
				<view class="order-item" @click="navTo('/pages/coupon/consumer')" hover-class="common-hover" :hover-stay-time="50">
					<view class=" icon position-relative"><image class="icon-img" src="../../static/user/coupon.png" mode="aspectFit"></image></view>
					<text>我的消费券</text>
				</view>
			</view>
			<!-- 浏览历史 -->
			<view class="history-section ">
				<uni-list>
					<uni-list-item v-if="isShowIllegality" title="我的推广" @click="navTo('/pages/myteam/myteam')" thumb="/static/user/tuiguang.png"></uni-list-item>
					<uni-list-item title="邀请海报" @click="navTo('/pages/shareQrCode/index')" thumb="/static/user/haibao.png"></uni-list-item>
					<uni-list-item title="我的收藏" @click="navTo('/pages/favorites/favorites')" thumb="/static/user/collect.png"></uni-list-item>
					<uni-list-item title="收货地址" @click="navTo('/pages/address/address')" thumb="/static/user/address.png"></uni-list-item>
					<uni-list-item title="商家入口" @click="navTo('/pages/merchant/merchant')" thumb="/static/user/shopping.png" v-if="userInfo.service == 1"></uni-list-item>
				</uni-list>
			</view>
		</view>
		<!-- </scroll-view> -->
		<uni-popup ref="popup" type="center">
			<view class="popup-box">
				<view class="img">
					<view class="img-font">兑换充值卡</view>
					<view class="img-bg"><image src="../../static/img/img009.png" mode=""></image></view>
				</view>
				<view class="mian">
					<view class="main-item">
						<view class="font">卡号</view>
						<input type="text" v-model="card" class="input" placeholder="请输入卡号" placeholder-class="input-font" />
					</view>
					<view class="main-item" style="margin-top: 40rpx;">
						<view class="font">密码</view>
						<input type="password" v-model="pwd" placeholder="请输入密码" class="input" placeholder-class="input-font" />
					</view>
					<view class="main-item" style="margin-top: 40rpx;">
						<view class="font">选择门店</view>
						<view class="input" @click="chooseStore">{{ storeInfo.name || '请选择门店' }}</view>
					</view>
					<view class="comfirm-box">
						<view class="cancel" @click="cancel">取消</view>
						<view class="comfirm" @click="comfirm()">确认兑换</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
import uniPopup from '@/components/uni-popup/uni-popup.vue';
import { verification } from '@/api/merchant.js';
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, orderVerific } from '@/api/user.js';
import { getUserInfo } from '@/api/login.js';
let startY = 0,
	moveY = 0,
	pageAtTop = true;
export default {
	components: {
		uniList,
		uniListItem,
		uniPopup
	},
	data() {
		return {
			coverTransform: 'translateY(0px)',
			coverTransition: '0s',
			moving: false,
			userDowm: 0, //卡片升级专属高度
			userMaxDowm: 0, //卡片最高高度
			orderinfo: {
				complete_count: 0,
				received_count: 0,
				unshipped_count: 0,
				order_count: 0
			},
			// 判断是否已经点击过一次取消关注公众号
			watchful: true,
			card: '',
			pwd: '',
			storeInfo: {
				name: ''
			}
		};
	},
	onShow() {
		// 判断是否已经登录
		if (this.hasLogin) {
			this.loadBaseData();
		}
	},
	onReady() {
		// 初始化获取页面宽度
		uni.createSelectorQuery()
			.select('.container')
			.fields(
				{
					size: true
				},
				data => {
					// 计算最多下拉的高度
					this.userDowm = Math.floor((data.width / 750) * 200);
					// 计算最大触发修改高度事件
					this.userMaxDowm = Math.floor((data.width / 750) * 250);
				}
			)
			.exec();
	},
	computed: {
		...mapState('user', ['userInfo', 'orderInfo', 'hasLogin']),

		// #ifdef H5
		...mapState(['weichatObj']),
		// #endif
		...mapState(['isShowIllegality'])
	},
	methods: {
		...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
		// 加载初始数据
		loadBaseData() {
			let obj = this;
			getUserInfo({})
				.then(({ data }) => {
					orderData({})
						.then(({ data }) => {
							obj.setOrderInfo(data);
						})
						.catch(e => {
							obj.setOrderInfo({
								complete_count: 0, //完成
								received_count: 0, //待收货
								unshipped_count: 0, //待发货
								order_count: 0, //订单总数
								unpaid_count: 0 //待付款
							});
						});
					obj.setUserInfo(data);

					// 判断是否已经关注公众号
					if (data.subscribe == 0 && obj.watchful) {
						uni.showModal({
							title: '温馨提醒',
							content: '为提供更好的服务，请关注公众号。',
							success: res => {
								obj.watchful = false;
								// 判断是否点击了确认
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/shareQrCode/wechatQr'
									});
								}
							}
						});
					}
				})
				.catch(e => {
					console.log(e);
				});
		},
		/**
		 * 统一跳转接口,拦截未登录路由
		 * navigator标签现在默认没有转场动画，所以用view
		 */
		navTo(url) {
			if (!this.hasLogin) {
				url = '/pages/public/login';
				// #ifdef H5
				let weichatBrowser = uni.getStorageSync('weichatBrowser');
				// 判断是否为微信浏览器
				if (weichatBrowser) {
					url = '/pages/public/wxLogin';
				}
				// #endif
				// #ifdef MP-WEIXIN
				url = '/pages/public/wxLogin';
				// #endif
			}
			uni.navigateTo({
				url
			});
		},
		/**
		 *  会员卡下拉和回弹
		 *  1.关闭bounce避免ios端下拉冲突
		 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
		 *    transition设置0.1秒延迟，让css来过渡这段空窗期
		 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
		 */
		coverTouchstart(e) {
			// console.log(e);
			if (pageAtTop === false) {
				return;
			}

			this.coverTransition = 'transform .1s linear';
			startY = e.touches[0].clientY;
		},
		coverTouchmove(e) {
			// console.log(e);
			moveY = e.touches[0].clientY;
			let moveDistance = moveY - startY;
			let maxDowm = this.userMaxDowm;
			let Dowm = this.userDowm;
			if (moveDistance < 0) {
				this.moving = false;
				return;
			}
			this.moving = true;
			if (moveDistance >= Dowm && moveDistance < maxDowm) {
				moveDistance = Dowm;
			}

			if (moveDistance > 0 && moveDistance <= Dowm) {
				this.coverTransform = `translateY(${moveDistance}px)`;
			}
		},
		coverTouchend() {
			if (this.moving === false) {
				return;
			}
			this.moving = false;
			this.coverTransition = 'transform 0.3s cubic-bezier(.21,1.93,.53,.64)';
			this.coverTransform = 'translateY(0px)';
		},
		// 核销
		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);
				});
		},
		// 选择门店
		chooseStore() {
			uni.navigateTo({
				url: '/pages/shoping/list?type=3'
			});
		},
		go() {
			this.$refs.popup.open();
		},
		cancel() {
			this.$refs.popup.close();
			this.card = '';
			this.pwd = '';
			this.storeInfo = { name: '' };
		},
		comfirm() {
			verification({
				card_number: this.card,
				card_password: this.pwd,
				type: 0,
				is_confirm: 1,
				reg_store_id: this.storeInfo.id
			})
				.then(e => {
					console.log(e);
					this.card = '';
					this.pwd = '';
					this.storeInfo = { name: '' };
				})
				.catch(e => {
					console.log(e);
					this.card = '';
					this.pwd = '';
					this.storeInfo = { name: '' };
				});
			this.$refs.popup.close();
		}
	}
};
</script>
<style lang="scss">
%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: #ffffff;
	/* #ifdef APP-PLUS */
	.bar-height {
		height: var(--status-bar-height);
		background-color: $base-color;
	}
	/* #endif */
}
.content-box {
	height: 100%;
}

.user-section {
	height: 435rpx;
	padding: 60rpx 0 0 0;
	position: relative;
	.bg {
		position: absolute;
		height: 480rpx;
		width: 100%;
		left: 0;
		top: 0;
		image {
			width: 100%;
			height: 100%;
		}
		// background-color: $base-color;
	}
}
.user-info-box {
	width: 90%;
	padding-left: 20rpx;
	margin: 0 auto;
	display: flex;
	height: 280rpx;
	align-items: stretch;
	background-color: #e9f8f1;
	border-radius: 20rpx;
	color: white;
	display: flex;
	justify-content: space-between;
	position: relative;
	z-index: 0;

	.detail {
		margin: 20rpx 0 0 0;
		height: 130rpx;

		.portrait-box {
			height: 100%;
			.portrait {
				width: 130rpx;
				height: 100%;
				border: 5rpx solid #fff;
				border-radius: 50%;
			}
		}
		.info-box {
			margin-left: 20rpx;
			line-height: 1.5;
			color: black;
			.username {
				font-size: $font-lg + 6rpx;
				height: 100%;
			}
		}
	}
	.config {
		margin-top: 60rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #52c696;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 165rpx;
		height: 64rpx;
		background: #ffffff;
		border-radius: 32px 0px 0px 32px;
		.setting {
			width: 32rpx;
			height: 32rpx;
			image {
				width: 100%;
				height: 100%;
			}

			// margin-right: 51rpx;
		}
	}
}

.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;
			font-size: 30rpx;
		}
	}
	.e-b {
		font-size: $font-sm;
		color: #d8cba9;
		margin-top: 10rpx;
	}
}
.vip {
	margin-top: -110rpx;
	margin-bottom: 110rpx;
	padding: 0 30rpx;
	height: 170rpx;
	image {
		height: 100%;
		width: 100%;
	}
}
.cover-container {
	display: flex;
	flex-direction: column;
	background: $page-color-base;
	padding: 0 30rpx;
	position: relative;
	background: #f5f5f5;
	padding-bottom: 20rpx;
	margin-top: -110rpx;

	.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 {
	position: relative;
	background-color: white;
	top: -150rpx;
	box-shadow: 0px 0px 20rpx 0px rgba(50, 50, 52, 0.06);
	.arc {
		position: absolute;
		left: 0;
		top: -34rpx;
		width: 100%;
		height: 36rpx;
	}
	.mendian {
		padding: 0 40rpx;
		height: 60rpx;
		background-color: #ffffff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.mendian-lift {
			display: flex;
			.null-mendian {
				font-size: 28rpx;
				font-weight: bold;
			}

			.img {
				margin: 0 10rpx 0;
				width: 36rpx;
				height: 36rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}

		.mendian-right {
			display: flex;
			align-items: center;

			.duihuan {
				font-size: 24rpx;
			}
		}
	}
	.my-order {
		padding: 0 20rpx;
		border-radius: 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{
	// 	margin-top: 70rpx;
	// }
	.order-section {
		@extend %section;
		padding: 28rpx 0;

		.order-item {
			@extend %flex-center;
			// width: 120rpx;
			width: 25%;
			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: 88rpx;
			width: 90rpx;
			margin-bottom: 18rpx;
			background-size: 100%;
			background-repeat: no-repeat;
			background-position: center;
			.icon-img {
				width: 100%;
				height: 100%;
			}
		}
		.icon1 {
			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;
	.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;
		image {
			display: inline-block;
			width: 160rpx;
			height: 160rpx;
			margin-right: 20rpx;
			border-radius: 10rpx;
		}
	}
}
.popup-box {
	width: 650rpx;
	padding-bottom: 30rpx;
	background-color: #ffffff;
	border-radius: 20rpx;
	position: relative;
	.img {
		position: relative;
		top: -56rpx;
		left: 0;
		width: 650rpx;
		height: 132rpx;
		z-index: 100;
		text-align: center;
		line-height: 132rpx;
		.img-bg {
			position: absolute;
			left: 50%;
			top: 0;
			margin-left: -250rpx;
			border-radius: 20rpx 20rpx 0 0;
			width: 500rpx;
			height: 132rpx;
			image {
				width: 100%;
				height: 100%;
			}
		}
		.img-font {
			line-height: 120rpx;
			position: relative;
			z-index: 10;
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			color: #54300f;
		}
	}

	.mian {
		margin-top: -10rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		// padding: 32rpx 32rpx;
		background-color: #ffffff;
		border-radius: 0 0 20rpx 20rpx;
		text-align: center;
		padding: 20rpx;
		width: 100%;
		.main-item {
			display: flex;
			width: 580rpx;
			align-items: center;
			padding: 20rpx 40rpx;
			border-radius: 10rpx;
			background: #f5f2e5;
			height: 100rpx;
			.font {
				color: #623324;
				font-size: 30rpx;
			}
			.input {
				margin-left: 20rpx;
				text-align: left;
				color: #cdc0b3;
				font-size: 28rpx;
			}
			.input-font {
				color: #cdc0b3;
				font-size: 28rpx;
			}
		}

		.comfirm-box {
			margin-top: 80rpx;
			display: flex;
			// margin-bottom: 32rpx;

			// justify-content: space-around;
			.cancel {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 260rpx;
				height: 100rpx;
				border: 1px solid #dcc786;
				border-radius: 10px;

				font-size: 32rpx;
				color: #605128;
			}

			.comfirm {
				margin-left: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 260rpx;
				height: 100rpx;
				background: linear-gradient(-90deg, #d1ba77 0%, #f7e8ad 100%);
				border-radius: 10px;
				font-size: 32rpx;
				color: #605128;
			}
		}
	}
}
</style>
