<template>
  <!-- svip会员模块 -->
	<view class="">
		<view class="card-section">
			<view class="header-card">
				<view class="acea-row row-middle">
					<image class="image" :src="userInfo.avatar ? userInfo.avatar : '/static/images/f.png'"></image>
					<view class="text">
						<view class="name">{{userInfo.nickname || ''}}</view>
						<view>开通享{{ totalNumber(Object.values(memberRights).length) }}大特权，省钱又省心</view>
					</view>
				</view>
			</view>
			<view class="right-section">
				<view class="section-hd acea-row row-center-wrapper">
					<view class="title acea-row row-center row-bottom">开通会员尊享权</view>
				</view>
				<view class="section-bd acea-row">
					<view v-for="item in memberRights" :key="item.id" class="item" @click="goUrl(item.link)">
						<image class="pic" :src="item.pic"></image>
						<view class="text">
							<view class="name line1">{{item.name}}</view>
							<view class="text_info line1">{{item.info}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="type-section" id="card">
			<view class="title_bd">
				<view class="bold">会员套餐</view>
			</view>
			<scroll-view class="scroll" scroll-x="true">
				<view v-for="(item,index) in memberType" :key="item.group_data_id" class="item" :class="{on: index === type}"
					@click="checkType(item,index)">
					<view class="title line1">{{item.value && item.value.svip_name}}</view>
					<view class="new">￥<text class="num">{{item.value.price | moneyFormat}}</text></view>
					<view class="old">￥{{item.value.cost_price | moneyFormat}}</view>
				</view>
			</scroll-view>
			<view class="buy" @click="pay">{{svip_type == 1 ? '立即体验' : '立即开通'}}</view>
			<!--<view v-if="memberExplain" class="agree">-->
			<!--	<navigator class="link" url="/pages/annex/vip_clause/index" hover-class="none">购买即视为同意<text-->
			<!--			class="mark">《会员用户协议》</text></navigator>-->
			<!--</view>	-->
		</view>
		<view v-if="goodsList.length" class="goods-section">
			<view class="section-hd">会员专享价 <text>会员购买享超低价</text></view>
			<view class="section-bd acea-row">
				<view v-for="item in goodsList" :key="item.product_id" class="item" @click="goDetail(item.product_id)">
          <easy-loadimage class="image" mode="widthFix" :image-src="item.image"></easy-loadimage>
					<view class="name">{{item.store_name}}</view>
					<view class="svip-price">￥{{item.is_svip_price}}
						<image src="../../../static/images/svip.png"></image>
					</view>
					<view class="shop-price">商城价：￥{{item.price}}</view>
				</view>
			</view>
		</view>
		<payment :payMode="payMode" :pay_close="pay_close" :is-call="true" @onChangeFun="onChangeFun"
			:order_id="pay_order_id" :totalPrice="totalPrice"></payment>
		<authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize>
		<view v-show="false" v-html="formContent"></view>
	</view>
</template>

<script>

	import payment from '@/components/payment';
  import easyLoadimage from '@/components/easy-loadimage/easy-loadimage.vue';
	import authorize from '@/components/Authorize';
	import { mapGetters } from "vuex";
	import {
		memberCard,
		memberEquity,
		memberCardCreate,
		groomList,
	} from '@/api/user.js';
	import {
		orderOfflinePayType
	} from '@/api/order.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		openPaySubscribe
	} from '@/utils/SubscribeMessage.js';
	import dayjs from '@/plugin/dayjs/dayjs.min.js';
  import spread from "../../../libs/spread";
	export default {
		components: {
			payment,
			authorize,
      easyLoadimage
		},
		filters: {
			dateFormat: function(value) {
				return dayjs(value * 1000).format('YYYY-MM-DD');
			},
			moneyFormat: function(value) {
				return parseFloat(value);
			}
		},
		data() {
			return {
				memberType: [],
				userInfo: {},
				memberRights: [],
				memberExplain: [],
				memberCoupons: [],
				isGetFree: null,
				account: '',
				password: '',
				goodsList: [],
				pay_order_id: '',
				payMode: [{
					name: '微信支付',
					icon: 'icon-weixinzhifu',
					// #ifdef H5
					value: this.$wechat.isWeixin() ? 'weixin' : 'h5',
					// #endif
					// #ifdef MP
					value: 'routine',
					// #endif
					// #ifdef APP-PLUS
					value: 'weixin',
					// #endif
					title: '微信快捷支付',
					payStatus: true
					}
					// #ifdef H5 ||APP-PLUS
					,
					{
					name: '支付宝支付',
					icon: 'icon-zhifubao',
					// #ifdef H5 || APP-PLUS
					value: 'alipay',
					// #endif
					// #ifdef MP
					value: 'alipayQr',
					// #endif
					title: '支付宝支付',
					payStatus: true
					}
					// #endif
				],
				pay_close: false,
				totalPrice: '0',
				formContent: '',
				page: 1,
				limit: 15,
				finished: false,
				loading: false,
				memberEndTime: '',
				// #ifdef H5
				isWeixin: this.$wechat.isWeixin(),
				// #endif
				type: 0,
				svipDef: null,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				svip_type: 0
			}
		},
		watch: {},
		computed: mapGetters(['isLogin','uid']),
		onLoad(options) {
			if (this.isLogin) {
				this.memberEquity();
				this.getMemberCard();
				this.groomList();
			} else {
				this.isAuto = true;
				this.isShowAuth = true
			}

      // 关系处理
      if(options.spread) spread(options.spread, this.isLogin)
		},
		onShow(){
			uni.removeStorageSync('form_type_cart');
		},
		onReachBottom() {
			this.groomList();
		},
		methods: {
      totalNumber(num){
        console.log(num)
        let num_text = {
          1: '一',
          2: '二',
          3: '三',
          4: '四',
          5: '五',
          6: '六',
          7: '七',
          8: '八',
          9: '九',
        };

        return num_text[parseInt(num)] ?? '';
      },
			pay() {
				if (this.totalPrice == 0) {
					this.createMemberCard('weixin');
				} else {
					this.pay_close = true;
				}
			},
			payClose: function() {
				this.pay_close = false;
			},
			goDetail(id) {
				uni.navigateTo({
					url: `/pages/goods_details/index?id=${id}`
				});
			},
			goUrl(url){
				if(url.indexOf("http") != -1){
					// #ifdef H5
					location.href = url
					// #endif
				}else{
					if(['/pages/goods_cate/goods_cate','/pages/order_addcart/order_addcart','/pages/user/index','/pages/plant_grass/index'].indexOf(url) == -1){
						uni.navigateTo({
							url:url
						})	
					}else{
						uni.switchTab({
							url:url
						})
					}
				}
			},
			onLoadFun() {
				this.isShowAuth = false;
				this.memberCard();
				this.groomList();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			// 付费会员权益
			memberEquity() {
				memberEquity().then(res => {
					this.memberRights = res.data.interests;
					this.userInfo = res.data.user;
					if(res.data.user.is_svip > 0){
						uni.navigateTo({
							url: '/pages/annex/vip_center/index'
						});
					}
				}).catch(err => {
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
			},
			// 付费会员数据
			getMemberCard() {
				uni.showLoading({
					title: '正在加载…'
				});
				memberCard().then(res => {
					uni.hideLoading();
					this.memberType = res.data.list;
					this.svipDef = res.data.def;
					this.svip_type = res.data.def.svip_type;
					this.totalPrice = res.data.def.price.toString();
					this.pay_order_id = res.data.def.group_data_id.toString();
				}).catch(err => {
					uni.hideLoading();
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
			},
			groomList() {
				if (this.finished || this.loading) {
					return;
				}
				this.loading = true
				groomList({
					page: this.page,
					limit: this.limit
				}).then(res => {
					this.goodsList = this.goodsList.concat(res.data.list);
					this.finished = res.data.list.length < this.limit;
					this.loading = false;
					this.page += 1;
				}).catch(err => {
					
				});
			},
			checkType(svip,index) {
				this.type = index;
				this.svipDef = svip.value;
				this.svip_type = svip.value.svip_type;
				this.pay_order_id = svip.group_data_id;
				this.totalPrice = svip.value.price.toString();
			},
			payCheck: function(type) {
				this.createMemberCard(type);
			},
			// 立即购买
			createMemberCard(type) {
				uni.showLoading({
					title: '正在加载…'
				});
				let query = {
					pay_type: type,
					// #ifdef H5
					return_url: location.port ? location.protocol + '//' + location.hostname + ':' + location.port +
						'/pages/annex/vip_paid/index' : location.protocol + '//' + location.hostname +
						'/pages/annex/vip_paid/index'
					// #endif
				};
				let group_id = this.pay_order_id
				// #ifdef MP
				openPaySubscribe().then(() => {
					memberCardCreate(group_id,query).then(res => {
						if (parseFloat(this.totalPrice) > 0) {
							this.callPay(res);
						} else {
							uni.hideLoading();
							return this.$util.Tips({
								title: '开通成功',
							}, () => {
								uni.navigateTo({
									url: '/pages/annex/vip_center/index'
								})
							});
						}
					}).catch(err => {
						uni.hideLoading();
						uni.showToast({
							title: err,
							icon: 'none'
						});
					});
				});
				// #endif
				// #ifndef MP
				memberCardCreate(group_id,query).then(res => {
					if (parseFloat(this.totalPrice) > 0) {
						this.callPay(res);
					} else {
						uni.hideLoading();
						return this.$util.Tips({
							title: '开通成功',
						}, () => {
							uni.navigateTo({
								url: '/pages/annex/vip_center/index'
							})
						});
					}
				}).catch(err => {
					uni.hideLoading();
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
				// #endif
			},
			// 调用支付
			callPay(res) {
				let that = this;
				let status = res.data.status,
					orderId = res.data.result.order_id,
					callback_key = res.data.result.pay_key,
					jsConfig = res.data.result.config,
					goPages = '/pages/annx/vip_center/index'
				switch (status) {
					case 'ORDER_EXIST':
					case 'EXTEND_ORDER':
					case 'PAY_ERROR':
					case 'error':
						uni.hideLoading();
						that.payClose();
						return that.$util.Tips({
							title: res.message
						});
						break;
					case 'success':
						uni.hideLoading();
						that.payClose();
						return that.$util.Tips({
							title: res.message,
							icon: 'success'
						});
						break;
					case 'alipay':
					case "alipayQr":
						uni.hideLoading();
						that.payClose();
						uni.navigateTo({
							url: '/pages/order_pay_back/index?keyCode='+callback_key+'&url='+jsConfig,
						})	
						return;
						break;
						// #ifndef MP
					case "wechat":
					case "weixin":
					case "weixinApp":
						jsConfig.timeStamp = jsConfig.timestamp;
						// #ifndef APP-PLUS
						this.$wechat.pay(jsConfig).then(res => {
							uni.hideLoading();
							that.payClose();
							return that.$util.Tips({
								title: res.message,
								icon: 'success'
							}, {
								tab: 4,
								url: goPages
							});
						}).catch(res => {
							uni.hideLoading();
							that.payClose();
							if (res.errMsg == 'chooseWXPay:cancel') return that.$util.Tips({
								title: '取消支付'
							}, {
								tab: 5,
								url: goPages + '&status=0'
							});
						})
						// #endif
						// #ifdef APP-PLUS
						let mp_pay_name=''
						if(uni.requestOrderPayment){
							mp_pay_name='requestOrderPayment'
						}else{
							mp_pay_name='requestPayment'
						}
						uni[mp_pay_name]({
							provider: 'wxpay',
							orderInfo: jsConfig,
							success: (e) => {
								uni.hideLoading();
								that.payClose();
								let url="/pages/annx/vip_center/index"
								return that.$util.Tips({
									title: '支付成功',
									icon: 'success'
								}, {
									tab: 4,
									url: url
								});
							},
							fail: (e) => {
								uni.hideLoading();
								that.payClose();
								uni.showModal({
									content: "支付失败",
									showCancel: false,
									success: function(res) {}
								})
							},
							complete: () => {
								uni.hideLoading();
							},
						});
						// #endif
						break;
						// #endif
						// #ifdef MP
					case "routine":
						jsConfig.timeStamp = jsConfig.timestamp;
						let mp_pay_name=''
						if(uni.requestOrderPayment){
							mp_pay_name='requestOrderPayment'
						}else{
							mp_pay_name='requestPayment'
						}
						uni[mp_pay_name]({
							...jsConfig,
							success: function(res) {
								uni.hideLoading();
								that.payClose();
								return that.$util.Tips({
									title: '支付成功',
									icon: 'success'
								}, {
									tab: 5,
									url: goPages
								});
							},
							fail: function(e) {
								uni.hideLoading();
								that.payClose();		
								return that.$util.Tips({
									title: '取消支付'
								});
							},
						})
						break;
						// #endif
					case "balance":
						uni.hideLoading();
						that.payClose();
						//余额不足
						return that.$util.Tips({
							title: res.msg
						}, {
							tab: 5,
							url: goPages
						});
						break;
						// #ifdef H5
					case 'h5':
						let host = window.location.protocol + "//" + window.location.host;
						let url = `${host}/pages/annex/vip_paid/index`
						let eUrl = encodeURIComponent(url)
						let jsurl = jsConfig.mweb_url || jsConfig.h5_url
						let locations = `${jsurl}&redirect_url=${eUrl}`
						setTimeout(() => {
							location.href = locations;
						}, 100);
						break;
						// #endif
						// #ifdef APP-PLUS
					case 'alipayApp':
						uni.requestPayment({
							provider: 'alipay',
							orderInfo: jsConfig,
							success: (e) => {
								uni.hideLoading();
								that.payClose();
								return that.$util.Tips({
									title: '支付成功',
									icon: 'success'
								}, {
									tab: 5,
									url: goPages
								});
							},
							fail: (e) => {
								uni.hideLoading();
								uni.showModal({
									content: "支付失败",
									showCancel: false,
									success: function(res) {}
								})
							},
							complete: () => {
								uni.hideLoading();
							},
						});
						break;
						// #endif
				}
			},
			onChangeFun: function(e) {
				let opt = e;
				let action = opt.action || null;
				let value = opt.value != undefined ? opt.value : null;
				action && this[action] && this[action](value);
			},
		},
		// 滚动监听
		onPageScroll(e) {
			// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件
			uni.$emit('scroll');
		},

    // #ifdef MP
    // 分享给好友
    onShareAppMessage () {
      let pages = getCurrentPages();
      let page = pages[pages.length - 1]
      let shareData = {
        title: '开通会员',
        path: page.$page.fullPath || '/' + page.route,
      };
      // 判断：用户是否登录  已经登录则添加分享人信息，未登录则正常分享
      if (this.isLogin) shareData.path = shareData.path + '?spread=' + this.uid;
      // 返回最终的分享配置信息
      return shareData
    },
    // 分享到朋友圈
    onShareTimeline() {
      let shareData = {
        title: '开通会员',
        query: {},
      };
      // 判断：用户是否登录  已经登录则添加分享人信息，未登录则正常分享
      if (this.isLogin) shareData.query.spread = this.uid;
      // 返回最终的分享配置信息
      return shareData
    },
    // #endif

	}
</script>

<style lang="scss" scoped>
	.card-section {
		padding: 64rpx 0 50rpx;
		background: url("data:image/png;base64,[encoded data, 28312 characters]");
		background-size: cover;
		background-repeat: no-repeat;
		.header-card{
			width: 680rpx;
			height: 200rpx;
			margin: 0 auto;
			background-image: url("data:image/png;base64,[encoded data, 19892 characters]");
			background-repeat: no-repeat;
			background-size: cover;
			padding: 33rpx 40rpx;
		}
		.image {
			width:76rpx;
			height: 76rpx;
			border-radius: 50%;
			border: 3rpx solid #fff;
			margin-right: 20rpx;
		}
		.text {
			flex: 1;
			font-size: 22rpx;
			line-height: 30rpx;
			color: #89735B;
		}
		.name {
			margin-bottom: 2rpx;
			font-weight: bold;
			font-size: 30rpx;
			line-height: 42rpx;
			color: #865622;
		}
		.info {
			margin-top: 74rpx;
			font-size: 24rpx;
			color: #865622;
		}
	}
	.right-section {
		width: 690rpx;
		//height: 520rpx;
		margin: 0 auto;
		background-image: url("data:image/png;base64,[encoded data, 24220 characters]");
    background-size: 100% 100%;
		background-repeat: no-repeat;
		margin-top: -60rpx;
		.section-hd {
			padding-top: 34rpx;
			padding-bottom: 34rpx;	
			.title {
				width: 543rpx;
				font-size: 28rpx;
				line-height: 1.1;
				color: #FFEED2;
			}
			.iconfont {
				font-size: 34rpx;
				color: #89735B;
			}
		}
		.section-bd {
			padding: 35rpx 30rpx 15rpx;
			.item {
				width: 210rpx;
				margin-bottom: 30rpx;
				text-align: center;
			}
			.pic {
				width: 80rpx;
				height: 80rpx;
				border-radius: 100%;
			}
			.text {
				flex: 1;
				overflow: hidden;
				white-space: nowrap;
				font-size: 20rpx;
				line-height: 33rpx;
				color: #92846D;
			}
			.name {
				margin-bottom: 2rpx;
				font-size: 24rpx;
				line-height: 37rpx;
				color: #F4D7A3;
			}
		}
	}
	.type-section {
		position: relative;
		top: -20rpx;
		border-radius: 20rpx 20rpx 0 0;
		background-color: #FFFFFF;
		padding: 0 30rpx 50rpx;
		.title_bd {
			padding: 26rpx 40rpx 0 0;
			font-size: 24rpx;
			color: #797979;
			.bold {
				display: inline-block;
				margin-right: 14rpx;
				font-weight: bold;
				font-size: 32rpx;
				line-height: 45rpx;
				color: #333333;
				+view {
					display: inline-block;
				}
			}
			.time {
				margin-left: 14rpx;
				font-size: 24rpx;
				color: #AE5A2A;
			}
		}
		.scroll {
			white-space: nowrap;
		}
		.item {
			display: inline-flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			width: 210rpx;
			height: 232rpx;
			border-radius: 12rpx;
			margin: 32rpx 20rpx 30rpx 0;
			border: 1px solid #CFCFCF;
			line-height: 42rpx;
			&:last-child {
				margin-right: 0;
			}
			&.on {
				border: 3rpx solid #FCC282;
				background-color: #FEF7EC;
				.new {
					color: #DBAA4D;
				}
			}
			.title{
				color: #282828;
				font-weight: bold;
				font-size: 30rpx;
				width: 180rpx;
				text-align: center;
			}
		}
		.new {
			margin-top: 22rpx;
			font-weight: 600;
			font-size: 34rpx;
			color: #E7BE7D;
			.num {
				font-size: 48rpx;
				line-height: 48rpx;
			}
		}
		.old {
			margin-top: 13rpx;
			text-decoration: line-through;
			font-size: 24rpx;
			color: #999999;
		}
		.info {
			margin-top: 13rpx;
			font-size: 24rpx;
		}
		.agree {
			font-size: 22rpx;
			text-align: center;
			color: #797979;
			margin-top: 30rpx;
			.link {
				display: inline-block;
			}
			.mark {
				color: #AE5A2A;
			}
		}
		.buy {
			height: 80rpx;
			border-radius: 12rpx;
			margin: 20rpx 30rpx 0;
			background: linear-gradient(270deg, #E5BA62 0%, #ECCA7F 51%, #F3D99B 100%);
			font-size: 30rpx;
			font-weight: bold;
			line-height: 80rpx;
			text-align: center;
			color: #865622;
			border-radius:: 43rpx;
		}
		.cash {
			padding-top: 26rpx;
			padding-bottom: 29rpx;
			font-size: 28rpx;
			text-align: center;
			color: #754E19;
		}
	}
	.goods-section {
		margin-top: -20rpx;
		background: #ffffff left top/414px 215px no-repeat;
		background-image: linear-gradient(180deg, #FFF7E9 0%, #FFFFFF 100%);
		.section-hd {
			padding: 26rpx 30rpx 0;
			font-weight: bold;
			font-size: 32rpx;
			line-height: 45rpx;
			color: #333333;
			text {
				color: #BCA376;
				font-size: 24rpx;
				font-weight: normal;
				margin-left: 15rpx;
			}
		}
		.section-bd {
			padding: 0 30rpx 0 7rpx;
			margin-top: 26rpx;
			.item {
				width: 215rpx;
				padding-bottom: 24rpx;
				margin-left: 23rpx;
			}
			/deep/image, /deep/uni-image, /deep/.easy-loadimage {
				width: 215rpx;
				height: 215rpx;
				border-radius: 8rpx;
			}
			.name {
				margin-top: 10rpx;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				font-size: 26rpx;
				line-height: 37rpx;
				color: #282828;
			}
			.svip-price {
				margin-top: 6rpx;
				font-size: 26rpx;
				color: #282828;
				font-weight: bold;
				image {
					width: 65rpx;
					height: 28rpx;
					margin-left: 6rpx;
					position:  relative;
					top: 5rpx;
				}
			}
			.shop-price {
				margin-top: 4rpx;
				font-size: 20rpx;
				color: #999999;
			}
		}
	}
</style>
