<template>
	<view class="container">
		<view class="vheigh"></view>
		<view class="top-image">
			<image :src="baseURL + '/static/image/user-top.png'" mode=""></image>
		</view>
		<view class="user-set flex">
			<view class="set-logo" @click="navTo('/pages/set/userinfo')">
				<!-- <image src="../../static/user/user1.png" mode=""></image> -->
			</view>
		</view>
		<view class="user-wrap">
			<view class="info-left flex">
				<view class="user-name">
					<image :src="userInfo.avatar" mode="" v-if="userInfo.avatar"></image>
					<image src="../../static/error/missing-face.png" mode="" v-else></image>
					<view class="user-font">
						<view class="name clamp" v-if="userInfo.nickname">{{ userInfo.nickname }}</view>
						<view class="name clamp" v-else>游客</view>
						<view class="phone" v-if="userInfo.spread_nickname">推荐人：{{ userInfo.spread_nickname }}</view>
						<view class="phone" v-if="userInfo.uid">邀请码：{{ userInfo.uid }}</view>
					</view>
				</view>
				<view class="kpi" v-if="userInfo.level >= 1">
					<image src="../../static/icon/u-v.png" mode="widthFix"></image>
					{{userInfo.level == 1?'会员VIP':'创业合伙人'}}
				</view>
			</view>
			<view class="main-box">
				<view class="main flex">
					<view class="item" @click="navTo('/pages/money/wallet')">
						<view class="item-num">{{ userInfo.now_money || 0 }}</view>
						<view class="item-font">我的钱包</view>
					</view>
					<view class="" style="width: 1rpx;height: 100rpx;background: #EEEEEE;"></view>
					<view class="item" @click="navTo('/pages/user/award')">
						<view class="item-num">{{ userInfo.brokerage_price || 0 }}</view>
						<view class="item-font">我的佣金</view>
					</view>
					<view class="" style="width: 1rpx;height: 100rpx;background: #EEEEEE;"></view>
					<view class="item" @click="navTo('/pages/user/team')">
						<view class="item-num">{{ userInfo.spread_count || 0 }}</view>
						<view class="item-font">我的推广</view>
					</view>
				</view>
			</view>
			<!-- <view class="shenqing" @click="shareLL"><image src="../../static/user/shenqing.png" mode="heightFix"></image></view> -->
		</view>

		<uni-list class="tool-list">
			<uni-list-item v-if="userInfo.clerk_type == 1" title="券码核销" @click="shao()" thumb="/static/user/user1.png">
			</uni-list-item>
			<uni-list-item v-if="userInfo.clerk_type == 1" title="核销历史" @click="navTo('/pages/user/hxjl')"
				thumb="/static/user/user2.png"></uni-list-item>
			<uni-list-item title="我的优惠券" @click="navTo('/pages/user/myyhq')" thumb="/static/icon/g-l.png">
			</uni-list-item>
			<uni-list-item title="兑换码兑换" @click="openDh" thumb="/static/user/mybank.png" v-if="userInfo.uid">
			</uni-list-item>
			<uni-list-item title="邀请有礼" @click="navTo('/pages/user/shareQrCode')" thumb="/static/user/user3.png"
				v-if="userInfo.level">
			</uni-list-item>
			<uni-list-item title="加入创业合伙人" @click="navTo('/pages/product/product?id=1')"
				thumb="/static/icon/good-gift.png" v-if="userInfo.level != 2">
			</uni-list-item>
			<uni-list-item title="我的收藏" @click="navTo('/pages/user/favorites')" thumb="/static/user/user4.png">
			</uni-list-item>
			<uni-list-item title="客服中心" @click="openKf()" thumb="/static/user/user5.png"></uni-list-item>
			<uni-list-item title="设置" @click="navTo('/pages/set/userinfo')" thumb="/static/user/user6.png">
			</uni-list-item>
		</uni-list>
		<uni-popup ref="popupkf" type="center">
			<view class="popup-box">
				<view class="img">
					<image :src="baseURL +'/static/image/img009.png'" mode=""></image>
				</view>
				<view class="mian">
					<view class="delivery">
						<view class="title">已经为您定制专属客服</view>
						<image src="../../static/img/img010.png" mode=""></image>
					</view>
					<view class="nocancel">客服VX:{{ text }}</view>
					<view class="comfirm-box">
						<view class="cancel" @click="cancel">取消</view>
						<view class="comfirm" @click="comfirm(text)">复制微信</view>
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popuphx" class="agree-wrapper">
			<view class="hx-wrapper">
				<view class="hx-img">
					<image src="../../static/img/hxbg.png" mode=""></image>
				</view>
				<view class="hx-body">
					<view class="hx-title">输入核销码核销</view>
					<input type="text" v-model="code" placeholder="请输入核销码" placeholder-class="hx-placeholder" />
					<view class="hx-btn" @click="qhx">立即核销</view>
				</view>
				<view class="hx-close" @click="close">
					<image src="../../static/icon/close.png" mode=""></image>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popupdh" class="agree-wrapper">
			<view class="hx-wrapper">
				<view class="hx-img">
					<image src="../../static/img/hxbg.png" mode=""></image>
				</view>
				<view class="hx-body">
					<view class="hx-title">输入礼包兑换码</view>
					<input type="text" v-model="dhcode" placeholder="请输入兑换码" placeholder-class="hx-placeholder" />
					<view class="hx-btn" @click.stop="goDh()">立即兑换</view>
				</view>
				<view class="hx-close" @click="closeDh()">
					<image src="../../static/icon/close.png" mode=""></image>
				</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
	import {
		goDh
	} from '@/api/shop.js'
	import {
		tabbar1
	} from '@/utils/tabbar.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,
		getUserInfo,
		coupon_verific
	} from '@/api/user.js';
	import {
		saveUrl,
		interceptor
	} from '@/utils/loginUtils.js';
	// #ifdef H5
	import {
		weixindata,
		shareLoad
	} from '@/utils/wxAuthorized';
	// #endif
	let startY = 0,
		moveY = 0,
		pageAtTop = true;
	export default {
		components: {
			uniList,
			uniListItem
		},
		data() {
			return {
				dhcode: '', //兑换码
				dhing: false, //是否兑换中
				code: '',
				current: 4,
				tabbar: tabbar1,
				qded: false, //是否已签到
				text: 'Xiao-666k', //客服微信
				today_integral: '', //签到获得的数值
				today_type: '', //签到获得的数值单位
				tom_integral: '', //明天签到获得的数值
				tom_type: '', //签到获得的数值单位
				userDowm: 0, //卡片升级专属高度
				userMaxDowm: 0, //卡片最高高度
				toolList: [{
						id: 't1',
						name: '新人通道',
						width: '56rpx',
						heigt: '54rpx',
						img: '../../static/icon/tool-1.png',
						path: '/pages/user/xrtd'
					},
					{
						id: 't2',
						name: '邀请好友',
						width: '56rpx',
						heigt: '57rpx',
						img: '../../static/icon/tool-2.png',
						path: '/pages/user/shareQrCode'
					},
					{
						id: 't3',
						name: '我的粉丝',
						width: '68rpx',
						heigt: '53rpx',
						img: '../../static/icon/tool-3.png',
						path: '/pages/user/myfans'
					},
					{
						id: 't4',
						name: '我的订单',
						width: '55rpx',
						heigt: '54rpx',
						img: '../../static/icon/tool-4.png',
						path: '/pages/order/order'
					},
					{
						id: 't5',
						name: '我的商品',
						width: '50rpx',
						heigt: '58rpx',
						img: '../../static/icon/tool-5.png',
						path: '/pages/order/order'
					},
					{
						id: 't6',
						name: '馆长申请',
						width: '59rpx',
						heigt: '56rpx',
						img: '../../static/icon/tool-6.png',
						path: '/pages/user/gzsq'
					}
				]
			};
		},
		onShow() {
			console.log(this.$store.state.baseURL + '/static/image/user-top.png')
			// 判断是否已经登录
			if (this.hasLogin) {
				this.loadBaseData();
			}
			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();
		},
		// #ifndef MP
		// onNavigationBarButtonTap(e) {
		// 	const index = e.index;
		// 	if (index === 0) {
		// 		this.navTo('/pages/set/set');
		// 	} else if (index === 1) {
		// 		// #ifdef APP-PLUS
		// 		const pages = getCurrentPages();
		// 		const page = pages[pages.length - 1];
		// 		const currentWebview = page.$getAppWebview();
		// 		currentWebview.hideTitleNViewButtonRedDot({
		// 			index
		// 		});
		// 		// #endif
		// 		uni.navigateTo({
		// 			url: '/pages/user/notice'
		// 		});
		// 	}
		// },
		// #endif
		computed: {
			...mapState('user', ['userInfo', 'orderInfo', 'hasLogin']),
			...mapState(['baseURL'])
		},
		methods: {
			...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
			// 打开兑换弹窗
			openDh() {
				this.$refs.popupdh.open()
			},
			// 关闭兑换弹窗
			closeDh() {
				this.dhcode = ''
				this.$refs.popupdh.close()
			},
			// 兑换
			goDh() {
				let obj = this
				if (obj.dhing) {
					return
				}
				if (obj.dhcode == '') {
					return obj.$api.msg('请输入兑换码')
				}
				obj.dhing = true
				goDh({
					code: obj.dhcode
				}).then(res => {
					obj.dhing = false
					obj.closeDh()
					obj.$api.showOk('兑换成功')
				}).catch(err => {
					obj.dhing = false
				})
			},
			shareLL() {
				this.$api.msg('功能建设中，敬请期待');
			},
			comfirm(text) {
				console.log(text);
				const result = this.uniCopy(text);
				if (result === false) {
					uni.showToast({
						title: '不支持'
					});
				} else {
					uni.showToast({
						title: '复制成功',
						icon: 'none'
					});
				}
				this.$refs.popupkf.close();
			},
			shao() {
				let obj = this;
				// #ifndef H5
				uni.scanCode({
					success(e) {
						obj.code = e.result;
						obj.$refs.popuphx.open();
					}
				});
				// #endif
				// #ifdef H5
				weixindata().then(wxOjb => {
					console.log(wxOjb, '获取微信');
					wxOjb.scanQRCode({
						needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
						scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
						success: function(res) {
							obj.code = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
							obj.$refs.popuphx.open();
						},
						fail() {
							// obj.shao()
							// window.location.href = obj.$store.state.baseURL + '/wap/#/pages/user/user'
							// obj.shao()
						}
					});
				});
				// #endif
			},
			qhx() {
				coupon_verific({
						verify_code: this.code
					})
					.then(e => {
						this.$api.msg('核销成功');
						obj.$refs.popuphx.close();
						console.log(e);
					})
					.catch(e => {
						console.log(e);
					});
			},
			close() {
				this.$refs.popuphx.close();
				this.code = '';
			},
			uniCopy(content) {
				/**
				 * 小程序端 和 app端的复制逻辑
				 */
				//#ifndef H5
				uni.setClipboardData({
					data: content,
					success: function() {
						console.log('success');
						return true;
					}
				});
				//#endif

				/**
				 * H5端的复制逻辑
				 */
				// #ifdef H5
				if (!document.queryCommandSupported('copy')) {
					//为了兼容有些浏览器 queryCommandSupported 的判断
					// 不支持
					return false;
				}
				let textarea = document.createElement('textarea');
				textarea.value = content;
				textarea.readOnly = 'readOnly';
				document.body.appendChild(textarea);
				textarea.select(); // 选择对象
				textarea.setSelectionRange(0, content.length); //核心
				let result = document.execCommand('copy'); // 执行浏览器复制命令
				textarea.remove();
				return result;
				// #endif
			},
			// 加载初始数据
			loadBaseData() {
				getUserInfo({})
					.then(({
						data
					}) => {
						this.setUserInfo(data);
						// 获取用户数据完毕后在获取订单数据防止多次跳转到登录页
						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) {
				console.log(url);
				if (!this.hasLogin) {
					// 保存地址
					saveUrl();
					// 登录拦截
					interceptor();
				} else {
					uni.navigateTo({
						url
					});
				}

				// uni.navigateTo({
				// 	url
				// });
			},
			useTool(e) {
				this.navTo(e.path);
			},
			// 签到弹窗
			goQd() {
				this.$refs.popupqd.open();
				this.qded = true;
			},
			// 关闭签到弹窗
			closeQd() {
				this.$refs.popupqd.close();
			},
			// 打开客服
			openKf() {
				this.$refs.popupkf.open();
			},
			// 关闭客服
			cancel() {
				this.$refs.popupkf.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,
	page {
		min-height: 100%;
		height: auto;
		background-color: $page-color-base;
	}

	.vheigh {
		height: var(--status-bar-height);
		background-color: $base-color;
	}

	.top-image {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		height: 230rpx;

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

	.tool-list {
		width: 690rpx;
		margin: auto;
		margin-top: 20rpx;
		background: #ffffff;
		box-shadow: 0px 0px 20rpx 0px rgba(50, 50, 52, 0.06);
		border-radius: 20rpx;
	}

	.popup-box {
		width: 522rpx;
		height: 605rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		position: relative;

		.img {
			position: relative;
			top: -56rpx;
			left: 0;
			width: 522rpx;
			height: 132rpx;
			display: flex;
			justify-content: center;

			image {
				border-radius: 20rpx 20rpx 0 0;
				width: 450rpx;
				height: 132rpx;
			}
		}

		.mian {
			margin-top: -44rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			// padding: 32rpx 32rpx;
			background-color: #ffffff;
			border-radius: 0 0 20rpx 20rpx;
			text-align: center;

			.delivery {
				font-size: 40rpx;
				color: #333333;
				display: flex;
				align-items: center;
				flex-direction: column;

				image {
					margin-top: 48rpx;
					width: 172rpx;
					height: 160rpx;
				}
			}

			.nocancel {
				font-size: 32rpx;
				color: #333333;
				margin-top: 14rpx;
			}

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

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

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

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

	.popup {
		width: 560rpx;
		padding-bottom: 45rpx;
		background-color: #ffffff;
		border-radius: 15rpx;
		text-align: center;
		line-height: 1;

		.popup-dox {
			position: relative;

			.popup-logo {
				margin: -160rpx auto 0;
				width: 400rpx;
				height: 200rpx;
			}
		}

		.popup-title {
			margin-top: 85rpx;
			font-size: 40rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #2a2a2a;

			text {
				font-size: 56rpx;
				color: #e83f30;
			}
		}

		.popup-tip {
			margin-top: 20rpx;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #8c8c8c;

			text {
				color: #e83f30;
			}
		}

		.popup-btn {
			margin: 58rpx auto 0;
			width: 270rpx;
			height: 66rpx;
			background: #f0c838;
			border-radius: 34rpx;
			text-align: center;
			line-height: 66rpx;
			font-size: 36rpx;
			font-family: Source Han Sans CN;
			font-weight: 500;
			color: #ffffff;
		}
	}

	.user-wrap {
		position: relative;
		z-index: 10;
		width: 690rpx;
		// height: 283rpx;
		background: #ffffff;
		box-shadow: 0px 4rpx 13rpx 0px rgba(229, 229, 229, 0.46);
		border-radius: 20rpx;
		padding: 34rpx 0 0;
		margin: auto;

		.info-left {
			padding-left: 34rpx;

			.user-name {
				display: flex;
				align-items: center;

				image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 50%;
				}

				.user-font {
					padding-left: 34rpx;

					.name {
						width: 250rpx;
						font-size: 40rpx;
						font-family: Source Han Sans CN;
						font-weight: 400;
						color: #333333;
					}

					.phone {
						margin-top: 10rpx;
						font-size: 20rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}
			}

			.kpi {
				background: #f5eede;
				border-radius: 30rpx 0px 0px 30rpx;
				padding: 18rpx 24rpx;
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #c6914e;
				display: flex;

				image {
					width: 34rpx;
					margin-right: 8rpx;
				}
			}
		}

		.info-right {
			display: flex;
			align-items: center;
			margin-top: 54rpx;
			padding: 0 34rpx;

			.iright-icon {
				width: 32rpx;
				height: 32rpx;
			}

			.iright-font {
				margin-left: 12rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}
		}
	}

	.user-set {
		justify-content: flex-end;
		// height: 40rpx;
		height: 150rpx;
		// padding-top: 40rpx;

		view {
			flex-shrink: 0;
		}

		padding: 30rpx;

		image {
			height: 40rpx;
			width: 40rpx;
		}
	}

	.main-box {
		margin: 20rpx auto 0;
		width: 690rpx;
		background: #ffffff;
		// box-shadow: 0px 4rpx 14rpx 0px rgba(229, 229, 229, 0.46);
		border-radius: 20rpx;

		.title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 24rpx;
			border-bottom: 1px solid #eeeeee;

			.title-left {
				display: flex;
				align-items: center;

				.title-icon {
					width: 32rpx;
					height: 32rpx;
				}

				.title-font {
					margin-left: 20rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #333333;
				}
			}

			.title-right {
				width: 14rpx;
				height: 24rpx;
			}
		}

		.main {
			padding: 50rpx 0 30rpx;

			.item {
				padding-bottom: 20rpx;
				width: 33%;
				display: flex;
				flex-direction: column;
				align-items: center;

				.item-num {
					font-size: 36rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #333333;
				}

				.item-font {
					margin-top: 18rpx;
					font-size: 22rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
				}
			}

			.oitem {
				width: 25%;
				display: flex;
				flex-direction: column;
				align-items: center;

				.oitem-image {
					height: 50rpx;
					width: 48rpx;
				}

				.oitem-font {
					margin-top: 20rpx;
					font-size: 24rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
				}
			}
		}
	}

	.gpai {
		width: 100rpx;
		line-height: 40rpx;
		border-radius: 25rpx;
		position: absolute;
		top: 120rpx;
		left: 40rpx;
		font-size: 24rpx;
		color: #c6914e;
		text-align: center;
		background-color: #f2d4ae;
	}

	.shenqing {
		height: 100rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #ffd893;
		border-radius: 0 0 20rpx 20rpx;

		image {
			display: inline-block;
			margin: auto;
			height: 50rpx;
		}
	}

	.hx-wrapper {
		width: 536rpx;
		height: 630rpx;
		position: relative;

		// background-color: #fff;
		.hx-img {
			width: 536rpx;
			height: 281rpx;

			image {
				width: 536rpx;
				height: 281rpx;
			}
		}

		.hx-close {
			position: absolute;
			left: 243rpx;
			bottom: -80rpx;
			width: 52rpx;
			height: 52rpx;

			image {
				width: 52rpx;
				height: 52rpx;
			}
		}

		.hx-body {
			width: 536rpx;
			height: 349rpx;
			background-color: #fff;
			border-radius: 0 0 10rpx 10rpx;

			.hx-title {
				width: 536rpx;
				font-size: 36rpx;
				font-weight: 500;
				color: #333333;
				line-height: 1;
				padding-top: 42rpx;
				text-align: center;
			}

			input {
				width: 439rpx;
				height: 68rpx;
				background: #dbf3e9;
				border-radius: 10rpx;
				margin: 39rpx auto 0;
				padding-left: 26rpx;

				.hx-placeholder {
					font-size: 26rpx;
					font-weight: 500;
					color: #52c696;
				}
			}

			.hx-btn {
				margin: 44rpx auto 0;
				width: 353rpx;
				height: 71rpx;
				background: #52c696;
				border-radius: 34rpx;
				font-size: 36rpx;
				font-weight: 500;
				color: #f8f9f9;
				line-height: 71rpx;
				text-align: center;
			}
		}
	}
</style>
