<template>
	<view :class="[AppTheme]" class="all_box container">
		<!-- #ifdef APP-PLUS || MP-WEIXIN -->
		<u-navbar title="个人中心" :titleStyle="{color:'#ffffff'}" :bgColor="primary" :autoBack="false" :fixed="true"
			:placeholder="true" :height="45">
			<view class="u-nav-slot" slot="left"></view>
		</u-navbar>
		<!-- #endif -->
		<view class="user-section bg-primary">
			<view class="user-info-box">
				<view class="portrait-box">
					<image class="portrait" :src="headimgurl">
					</image>
				</view>
				<view class="info-box">
					<view v-if="user.id" style="text-align: left;">
						<text class="username">{{ user.nickname ?user.nickname:'暂无昵称' }}</text>
					</view>
					<view v-if="user.id" style="text-align: left;">
						<text class="username2">ID:{{ user.id }}</text>
					</view>
					<!-- #ifdef APP-PLUS||H5 -->
					<view v-if="!user.id" @click="gologin" class="user-mask">点击登录</view>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<view v-if="!user.id" @click="toLogin" class="user-mask">点击登录</view>
					<!-- #endif -->
				</view>
				<view @click="navTo('/pagesD/pages/set/set')"
					style="width: 35vh;display: flex;justify-content: flex-end;padding-right: 10rpx;">
					<u-icon name="setting" color="#ffffff" size="25"></u-icon>
				</view>
			</view>
			<view class="tj-sction">
				<view class="top_box">
					<view class="left">
						可用余额:
						<text class="text-neutral">¥{{user.money||0.00}}</text>
					</view>
					<navigator class="right bg-primary" url="/pagesA/pages/mypurse/withdraw/withdraw">立即提现</navigator>
				</view>
				<view class="bottom_box">
					<view class="tj-item" @click="navTo('/pagesC/pages/user/collection/collection')">
						<text class="num">{{ goodslikecount || 0 }}</text>
						<text>收藏</text>
					</view>
					<view class="tj-item" @click="navTo('/pagesA/pages/mypurse/index')">
						<text class="num">{{ user.money || 0 }}</text>
						<text>余额</text>
					</view>
					<view class="tj-item" @click="navTo('/pagesE/pages/mall/integral')">
						<text class="num">{{ user.score || 0 }}</text>
						<text>积分</text>
					</view>
				</view>
			</view>
		</view>

		<view class="cover-container" :style="[
				{
					transform: coverTransform,
					transition: coverTransition
				}
			]">

			<!-- 订单 -->
			<view class="order-section">
				<view class="order-item" @click="navTo('/pagesD/pages/order/order?state=0')" hover-class="common-hover"
					:hover-stay-time="40">
					<u-icon class="yticon" size="35" name="order" :color="primary"></u-icon>
					</image>
					<text>全部订单</text>
				</view>
				<view class="order-item" @click="navTo('/pagesD/pages/order/order?state=1')" hover-class="common-hover"
					:hover-stay-time="40">
					<u-icon class="yticon" size="35" name="red-packet" :color="primary"></u-icon>
					<text>待付款</text>
				</view>
				<view class="order-item" @click="navTo('/pagesD/pages/order/order?state=2')" hover-class="common-hover"
					:hover-stay-time="40">
					<u-icon class="yticon" size="35" name="gift" :color="primary"></u-icon>
					<text>待发货</text>
				</view>
				<view class="order-item" @click="navTo('/pagesD/pages/order/order?state=3')" hover-class="common-hover"
					:hover-stay-time="40">
					<u-icon class="yticon" size="35" name="car" :color="primary"></u-icon>
					<text>待收货</text>
				</view>
				<view class="order-item" @click="navTo('/pagesD/pages/order/order?state=4')" hover-class="common-hover"
					:hover-stay-time="40">
					<u-icon class="yticon" size="35" name="checkmark-circle" :color="primary"></u-icon>
					<text>已完成</text>
				</view>
			</view>
			<view class="order-section2" v-if="modular.length">
				<view class="wrap">
					<view class="inner" v-if="check(item)" v-for="(item, index) in modular" :key="index"
						@click="navTo(item.url)">
						<image class="yticon" :src=" item.img"></image>
						<text>{{ item.modularname }}</text>
					</view>
				</view>
			</view>
			<view class="inner">
			</view>
			<!-- 浏览历史 -->
			<view class="history-section icon">
				<view class="itme" @click="navTo('/pagesD/pages/address/address')">
					<view style="width: 80vh;">
						<u-icon name="map" labelSize="14" labelPos="right" color="rgb(95, 205, 162)" label="地址管理"
							size="20"></u-icon>
					</view>
					<view>
						<u-icon name="arrow-right" size="20"></u-icon>
					</view>
				</view>
				<view class="itme">
					<u-line length="90%"></u-line>
				</view>
				<view class="itme" @click="navTo('/pagesC/pages/index/article/article?type=1')">
					<view style="width: 80vh;">
						<u-icon name="list-dot" labelSize="14" labelPos="right" color="rgb(151, 137, 247)" label="关于我们"
							size="20">
						</u-icon>
					</view>
					<view>
						<u-icon name="arrow-right" size="20"></u-icon>
					</view>
				</view>
				<view class="itme">
					<u-line length="90%"></u-line>
				</view>
				<view class="itme" @click="navTo('/pagesD/pages/set/set')">
					<view style="width: 80vh;">
						<u-icon name="setting" labelSize="14" color="rgb(224, 116, 114)" labelPos="right" label="账号设置"
							size="20">
						</u-icon>
					</view>
					<view>
						<u-icon name="arrow-right" size="20"></u-icon>
					</view>
				</view>
				<view class="itme">
					<u-line length="90%"></u-line>
				</view>
			</view>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<nologin ref="nologin" v-on:loginback="loginback"></nologin>
		<!-- #endif -->
		<!-- 分享 -->
		<sharebox ref="sbox" v-if="sharedata" :data="sharedata"></sharebox>
		<tabbar :primary="primary"></tabbar>
		<admyself :opshow="true"></admyself>
		<!-- #ifdef MP-WEIXIN -->
		<view style="height: 120rpx;"></view>
		<!-- #endif -->
		<!-- #ifdef APP-PLUS || H5 -->
		<view style="height: 50rpx;"></view>
		<!-- #endif -->
	</view>
</template>
<script>
	import mineApi from '@/api/home/index.js';
	import loginApi from '@/api/login/login.js';
	import mine from '@/api/mine/index.js';
	import tabbar from '@/components/tabbar/tabbar.vue';
	import nologin from '@/components/nologin/nologin.vue';
	import {
		mapState
	} from 'vuex';
	let startY = 0,
		moveY = 0,
		pageAtTop = true;
	export default {
		components: {
			tabbar,
			nologin,
		},
		data() {
			return {
				primary: this.$theme.primary,
				background: {
					backgroundColor: '#001f3f',
				},
				iflogin: '',
				coverTransform: 'translateY(0px)',
				coverTransition: '0s',
				moving: false,
				shuju: '',
				vipid: '',
				user: '',
				goodslikecount: 0,
				modular: [],
				headimgurl: '',
				settingFile: getApp().globalData.siteinfo,
				sharedata: null,
				operator: [],
				wxopen: false
			};
		},
		onLoad(options) {
			let that = this
			that.headimgurl = that.settingFile.siteroot + '/static/app/tab-my.png'
			that.$bindid.getbindid(options, that.$store);

			that.$init_config(2, function(res) {
				if (that.$config) {
					that.sharedata = that.$config.sharedata
				}
				// #ifdef APP-PLUS
				if (res.app.open_wx_auth == 1) {
					that.wxopen = true;
				}
				// #endif
				// #ifdef H5
				if (res.app.gzh_wx_auth == 1) {
					that.wxopen = true;
				}
				// #endif
			})
			// #ifdef H5
			let code = that.$getUrlParms('code')
			if (code) {
				uni.showLoading({
					title: '请稍等...'
				});
				let logdata = {
					type: 2,
					code: code,
					pid: uni.getStorageSync('sys_pid'),
				};
				loginApi.wxlogin_v2(logdata).then(res => {
					if (res.status == 200) {
						// 登陆成功
						uni.hideLoading()
						that.$store.commit('login', res.data);
						that.getInitInfo();
						let url = that.$until.getnewurl()
						window.history.replaceState(null, null, url);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			} else {
				that.getInitInfo();
			}
			// #endif
			// #ifndef H5
			that.getInitInfo();
			// #endif
		},
		onShareAppMessage(res) {
			let that = this
			let data = {
				title: that.sharedata.title,
				imageUrl: that.sharedata.imageUrl,
				path: that.sharedata.path
			};
			return data;
		},
		onShareTimeline(res) {
			let that = this
			return {
				title: that.sharedata.title,
				imageUrl: that.sharedata.imageUrl,
				path: that.sharedata.path
			}
		},
		onPullDownRefresh() {
			let that = this
			that.getInitInfo();
		},
		onShow() {
			let that = this;
			that.$init_config();
			if (that.primary == '' || that.primary != that.$theme.primary) {
				that.primary = that.$theme.primary;
			}
			let userInfo = uni.getStorageSync('userInfo');
			that.user = userInfo
			if (!that.user) {
				that.goodslikecount = 0;
			}
			that.getInitInfo();
		},
		watch: {
			// 当父组件需要子组件需要共享的参数发生了变化，手动通知子组件
			value: function(newVal, OldVal) {
				this.value = newVal;
			}
		},
		methods: {
			loginback() {
				//登录回调
				this.getInitInfo();
			},
			check(item) {
				let that = this;
				if (that.user && that.user.id > 0) {
					if (item.url == '/pagesC/pages/shoprequ/shoprequ' && that.user.farm_id > 0) {
						//农场入驻
						return false
					} else if (item.url == '/pagesB/pages/applyfofpromotion/applyfofpromotion' && that.user.isfx == 1) {
						//分销申请
						return false
					} else if (item.url == '/pagesC/pages/merchantdetails/index' && that.user.farm_id <= 0) {
						//商品管理
						return false;
					} else if (item.url == '/pagesD/pages/inspection/inspection' && that.user.is_inspection != 1) {
						//巡检任务
						return false;
					} else if ((item.url == '/pagesE/pages/serviceorder/operator/operator' || item.url ==
							'/pagesD/pages/financeoperator/financeoperator') && that.user.is_staff != 1) {
						//工单任务  认养工单
						return false;
					} else if (item.url == 'scancode') {
						// #ifdef APP-PLUS || MP-WEIXIN
						if (that.user.farm_id <= 0 && that.user.iswrite == 0) {
							return false;
						}
						// #endif
						// #ifndef APP-PLUS || MP-WEIXIN
						return false;
						// #endif
					}
				}
				return true;
			},
			subscribe(url, fun) {
				let that = this;
				let tmplIdsone = that.$config.temlist;
				if (tmplIdsone) {
					switch (url) {
						case "/pagesD/pages/inspection/inspection":
							that.$until.requestSubscribeMessage([tmplIdsone['InspectionResult']], tmplIdsone, function() {
								fun ? fun() : null
							});
							break
						case "/pagesD/pages/financeoperator/financeoperator":
						case "/pagesE/pages/serviceorder/operator/operator":
							that.$until.requestSubscribeMessage([tmplIdsone['operatorNewWork']], tmplIdsone, function() {
								fun ? fun() : null
							});
							break
						default:
							fun ? fun() : null
							break;
					}
				} else {
					fun ? fun() : null
				}
			},
			//打开扫一扫
			scancode() {
				uni.scanCode({
					scanType: ['qrCode'],
					success: function(res) {
						uni.navigateTo({
							url: '/pagesB/pages/activorderdails/writeoffdetails/writeoffdetails?code=' +
								res.result
						})
					}
				})
			},
			inspection(url) {
				let that = this;
				let tmplIdsone = that.$config.temlist;
				if (tmplIdsone) {
					that.$until.requestSubscribeMessage([tmplIdsone['InspectionResult']], tmplIdsone, function() {
						console.log('555555')
						that.$until.toUrl(url)
					});
				} else {
					console.log('pppppp')
					that.$until.toUrl(url)
				}
			},

			merchant() {
				let that = this;
				that.$until.toUrl('/pagesC/pages/merchantdetails/index')
			},
			financeoperator(url) {
				let that = this;
				let tmplIdsone = that.$config.temlist;
				if (tmplIdsone) {
					that.$until.requestSubscribeMessage([tmplIdsone['operatorNewWork']], tmplIdsone, function() {
						that.$until.toUrl(url)
					});
				} else {
					that.$until.toUrl(url)
				}
			},
			operators(url) {
				let that = this;
				let tmplIdsone = that.$config.temlist;
				if (tmplIdsone) {
					that.$until.requestSubscribeMessage([tmplIdsone['operatorNewWork']], tmplIdsone, function() {
						that.$until.toUrl(url)
					});
				} else {
					that.$until.toUrl(url)
				}
			},
			gologin() {
				let that = this
				let isWeixin = that.isWeiXin();
				if (isWeixin && that.wxopen) {
					that.h5_wx_auth();
				} else {
					uni.navigateTo({
						url: "/pagesC/pages/login/login"
					})
				}
			},
			h5_wx_auth() {
				let that = this;
				let appid = that.$config.app.wxgzhappid;
				if (appid == '') {
					uni.showToast({
						title: '公众号参数配置错误',
						icon: 'none'
					});
					return;
				}
				let pid = uni.getStorageSync('sys_pid');
				let purl = that.$siteinfo.h5_siteroot + "/h5?uuid=" + that.$uuid + "&pid=" + pid +
					"#/pages/user/user";
				let redirect_uri = encodeURIComponent(purl);
				that.$until.h5_wx_auth(appid, redirect_uri);
			},
			toLogin() {
				let that = this;
				that.$refs.nologin.open();
			},
			getInitInfo() {
				var data = null;
				// #ifdef APP-PLUS
				data = {
					apptype: 'app'
				};
				// #endif
				// #ifdef H5
				data = {
					apptype: 'h5'
				};
				// #endif
				// #ifdef MP-WEIXIN
				data = {
					apptype: 'weixin'
				};
				// #endif
				mineApi.vipIndex(data).then(res => {
					if (res.status == 200) {
						uni.stopPullDownRefresh()
						this.user = res.data.vip;
						this.headimgurl = res.data.vip.headimgurl
						this.modular = res.data.modular;
						this.operator = res.data.operator;
						this.goodslikecount = res.data.vip.goodslikecount;
					} else if (res.status == 401) {
						this.user = '';
						this.goodslikecount = 0;
					}
				});
			},

			/**
			 * 统一跳转接口,拦截未登录路由
			 * navigator标签现在默认没有转场动画，所以用view
			 */
			navTo(url) {
				let that = this;
				if (!that.user || !that.user.id) {
					// #ifdef MP-WEIXIN
					that.toLogin();
					// #endif
					// #ifdef APP-PLUS||H5
					that.gologin();
					// #endif
				} else {
					that.subscribe(url, function() {
						if (url == '#') {
							that.$api.msg('暂未开放');
							return;
						}
						if (url == '/pagesA/pages/distribution/distribution') {
							if (that.user.isfx != 1) {
								that.$until.toUrl('/pagesB/pages/applyfofpromotion/applyfofpromotion');
							} else {
								that.$until.toUrl(url);
							}
						} else if (url == '/pagesC/pages/merchantdetails/index') {
							if (that.user.farm_id <= 0) {
								that.$until.toUrl('/pagesC/pages/shoprequ/shoprequ');
							} else {
								that.$until.toUrl(url);
							}
						} else if (url == 'scancode') {
							that.scancode();
						} else {
							that.$until.toUrl(url);
						}
					})
				}
			},

			/**
			 *  会员卡下拉和回弹
			 *  1.关闭bounce避免ios端下拉冲突
			 *  2.由于touchmove事件的缺陷（以前做小程序就遇到，比如20跳到40，h5反而好很多），下拉的时候会有掉帧的感觉
			 *    transition设置0.1秒延迟，让css来过渡这段空窗期
			 *  3.回弹效果可修改曲线值来调整效果，推荐一个好用的bezier生成工具 http://cubic-bezier.com/
			 */
			coverTouchstart(e) {
				if (pageAtTop === false) {
					return;
				}
				this.coverTransition = 'transform .1s linear';
				startY = e.touches[0].clientY;
			},
			coverTouchmove(e) {
				moveY = e.touches[0].clientY;
				let moveDistance = moveY - startY;
				if (moveDistance < 0) {
					this.moving = false;
					return;
				}
				this.moving = true;
				if (moveDistance >= 80 && moveDistance < 100) {
					moveDistance = 80;
				}

				if (moveDistance > 0 && moveDistance <= 80) {
					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)';
			}
		}
	};
</script>
<style lang="scss">
	.all_box {
		min-height: calc(100vh - 100rpx);
	}

	.btn1 {
		width: 200rpx;
		height: 140rpx;
		margin-top: 20rpx;
		background-color: #f5f5f5;
		color: #999999;
		border-radius: 10rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
	}

	.btnImg {
		width: 60rpx;
		height: 50rpx;
	}

	.btn1::after {
		border: 0;
	}

	page {
		background-color: #f5f5f5;
		padding-bottom: 100rpx;
	}

	%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: 10upx;
	}

	%sectio {
		display: flex;
		justify-content: space-around;
		align-content: center;
		background: #fff;
		border-radius: 10upx 10upx 0 0;
	}

	.user-section {
		height: 320rpx;
		padding: 0upx 20rpx;

		.bg {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			filter: blur(1px);
			opacity: 0.7;
		}
	}

	.user-info-box {
		display: flex;
		text-align: center;
		align-items: center;
		position: relative;
		padding-bottom: 15rpx;
		z-index: 1;
		/* #ifdef H5 */
		padding-top: 15rpx;

		/* #endif */
		.portrait {
			width: 90upx;
			height: 90upx;
			border-radius: 50%;
			display: inline-block;
		}

		.info-box {
			margin-left: 20rpx;
			justify-items: center;
			width: 250rpx;

			.username {
				display: inline-block;
				font-size: 30rpx;
				color: #ffffff;
				margin-top: 20upx;
			}

			.username2 {
				display: inline-block;
				font-size: 28rpx;
				color: #f1f1f1;
				margin-top: 20upx;
			}

			.user-mask {
				// margin-top: 15upx;
				justify-content: start;
				text-align: left;
				color: #ffffff;
				font-size: 28rpx;
			}
		}

	}

	.tj-sction {
		// @extend %section;
		background: #fff;
		width: 100%;
		// margin: 0 30upx;
		border-radius: 10rpx;
		// z-index: 99;
		margin: auto;
		margin-top: -5rpx;
		position: relative;

		.top_box {
			padding: 20rpx;
			color: #75787d;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				width: 70%;

				text {
					font-size: 32rpx;
					padding-left: 20rpx;
				}
			}

			.right {
				color: #fff;
				font-size: 26rpx;
				padding: 15rpx 20rpx;
				border-radius: 40rpx;
				margin-right: 20rpx;
				margin-top: 30rpx;
			}
		}

		.bottom_box {
			display: flex;
			justify-content: space-around;
			align-items: center;

			.tj-item {
				@extend %flex-center;
				flex-direction: column;
				height: 140upx;
				font-size: $font-sm;
				color: #75787d;
			}
		}

		.num {
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 8upx;
		}
	}

	.vip-card-box {
		display: flex;
		flex-direction: column;
		color: #f7d680;
		height: 240upx;
		background: linear-gradient(left, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.8));
		border-radius: 16upx 16upx 0 0;
		overflow: hidden;
		position: relative;
		padding: 20upx 24upx;

		.card-bg {
			position: absolute;
			top: 20upx;
			right: 0;
			width: 380upx;
			height: 260upx;
		}

		.b-btn {
			position: absolute;
			right: 20upx;
			top: 16upx;
			width: 132upx;
			height: 40upx;
			text-align: center;
			line-height: 40upx;
			font-size: 22upx;
			color: #36343c;
			border-radius: 20px;
			background: linear-gradient(left, #f9e6af, #ffd465);
			z-index: 1;
		}

		.tit {
			font-size: $font-base + 2upx;
			color: #f7d680;
			margin-bottom: 28upx;

			.yticon {
				color: #f6e5a3;
				margin-right: 16upx;
			}
		}

		.e-b {
			font-size: $font-sm;
			color: #d8cba9;
			margin-top: 10upx;
		}
	}

	.cover-container {
		background: $page-color-base;
		/* #ifdef H5 */
		margin-top: 70rpx;
		/* #endif */
		/* #ifndef H5 */
		margin-top: 50rpx;
		/* #endif */
		padding: 20rpx;
		position: relative;
		background: #f5f5f5;
		padding-bottom: 20upx;

		.arc {
			width: 100%;
			height: 36upx;
		}
	}


	.order-section.order-section1 {
		margin-top: 0;
		border-radius: 0 0 10upx;
	}

	.order-section {
		@extend %sectio;
		padding: 28upx 0;
		// margin-top: 20upx;
		border-radius: 10rpx;

		.order-item {
			@extend %flex-center;
			width: 120upx;
			height: 120upx;
			border-radius: 10upx;
			font-size: $font-sm;
			color: $font-color-dark;
		}

		.yticon {
			// width: 52rpx;
			// height: 52rpx;
			/* font-size: 48upx; */
			margin-bottom: 18upx;
			color: #fa436a;
		}

		.icon-shouhoutuikuan {
			font-size: 44upx;
		}
	}

	// .demo-layout{
	// 	display: flex;
	// 	justify-content: center;
	// }

	.wrap {
		width: 100%;

		.inner {
			display: inline-block;
			margin-bottom: 60rpx;
			width: 25%;
			text-align: center;

			text {
				display: block;
				text-align: center;
				font-size: 24rpx;
				color: $font-color-dark;
			}

			.yticon {
				width: 52rpx;
				height: 52rpx;
				margin-bottom: 18rpx;
			}

			.icon-shouhoutuikuan {
				font-size: 44upx;
			}
		}
	}

	.order-section2 {
		width: 100%;
		padding: 28upx 0 0 0;
		margin-top: 20upx;
		background-color: #fff;
		border-radius: 10rpx;
	}

	.history-section {
		padding: 30upx 0 0;
		margin-top: 20upx;
		background: #fff;
		border-radius: 10upx;
		border-radius: 10rpx;

		.sec-header {
			display: flex;
			align-items: center;
			font-size: $font-base;
			color: $font-color-dark;
			line-height: 40upx;
			margin-left: 30upx;

			.yticon {
				font-size: 44upx;
				color: #5eba8f;
				margin-right: 16upx;
				line-height: 40upx;
			}
		}

		.h-list {
			white-space: nowrap;
			padding: 30upx 30upx 0;

			image {
				display: inline-block;
				width: 160upx;
				height: 160upx;
				margin-right: 20upx;
				border-radius: 10upx;
			}
		}

		.itme {
			padding: 10rpx 30rpx 20rpx 30rpx;
			display: flex;
			justify-content: space-evenly;
			align-items: center;
			// justify-content: space-between;
		}
	}
</style>
