<template>
	<view class="vip-content">
		<view class="container-box">
			
			<view class="title-box">
				<image class="title-img" src="@/static/images/my/vip-title.png" mode="widthFix"></image>
				<view class="desc">
					甄选权益，开启您的按摩之旅
				</view>
			</view>
			<!-- <image class="bgimg" src="../static/workbg.png" mode="widthFix"></image> -->
			<!-- <image class="bgimg" src="/static/image/vip_bg.png" style="height: 100vh;"></image> -->
			<view>
				<!-- <image src="../../static/ktvip.png" style="margin: 80rpx 70rpx;width: 610rpx;height: 70rpx;"></image> -->

				<view class="vip_kapian">
					<!-- <image src="/static/image/vip_top.png"></image> -->
					<view style="padding: 52rpx;">
						<image class="img" src="/static/image/vip-text.png" mode=""></image>
						<view class="kapian_text">会员享受超值VIP特权</view>
						<!-- <view class="kapian_ktmoney" v-if="!isVip">￥ <text>{{payMoney}}</text> </view> -->
						<view class="kapian_ktname" v-if="endTime">到期时间</view>
						<view class="kapian_ktmoney" v-if="endTime">{{endTime}}</view>
						<view class="kapian_ktmoney" v-if="!isVip">------</view>
					</view>
				</view>
				<view class="save_vipbtn" v-if="!isVip" @tap="showpay = true">立即开通</view>
				<!-- <view class="quanyi_view">
					<image :src="quanyiImage" mode="widthFix"></image>
				</view> -->
				<view class="quanyi_view">
					<view class="quanyi_titview">
						<view class="flex justify-center align-center">
							<image src="../../static/vipleft.png"></image>
							<view class="quanyi_titname">VIP超值权益</view>
							<image src="../../static/vipright.png"></image>
						</view>

						<view class="quanyi_itemview">
							<view class="quanyi_listitem" v-for="(item,index) in gridData">
								<image :src="item.image"></image>
								<view class="listitem_name">{{item.title}}</view>
							</view>
						</view>

						<!-- <view class="save_vipbtn">立即开通</view> -->


						<view class="flex margin-top-xl flex-wrap justify-between" v-if="!isVip">
							<view v-for="(item,index) in vipList" :key='index' @click="select(item)" class="text-center flex flex-direction justify-between padding-tb radius"
									:class="selNum == item.id?'active': 'noactive'">
									<view class="text-bold" style="font-size: 28rpx;" >{{item.vipName}}</view>
									<view class="text-bold"  style="font-size: 28rpx;">¥<text  style="font-size: 48rpx;" >{{item.money}}</text></view>
								</view>
							</view>
					</view>

				</view>

			</view>
		</view>

		<!-- 支付方式 -->
		<u-popup v-model="showpay" mode="bottom">
			<view class="popup_pay">
				<view style="background-color: #fff;">
					<view style="padding: 0 20upx;margin-top: 60rpx;margin-bottom: 20rpx;">
						<view
							style="display: flex;height: 100upx;align-items: center;padding: 20upx 0;justify-content: center;"
							v-for="(item,index) in openLists" :key='index'>
							<image :src="item.image" style="width: 55upx;height: 55upx;border-radius: 50upx;">
							</image>
							<view style="font-size: 30upx;margin-left: 20upx;width: 70%;">
								{{item.text}}
							</view>
							<radio-group name="openWay" style="margin-left: 45upx;" @tap='selectWay(item.id)'>
								<label class="tui-radio">
									<radio color="#2FB57A" :checked="openWay === item.id ? true : false" />
								</label>
							</radio-group>
						</view>
					</view>
				</view>
				<view class="pay_btn" @click="pay()">确认支付</view>
			</view>
		</u-popup>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				endTime: '',
				customStyle: {
					width: '250rpx',
					color: '#402321',
					background: "#DFC5A7",
					border: 0,
					fontWeight: '700'
				},
				gridData: [{
						title: '专享九折',
						image: '../static/1.png'
					},
					{
						title: '身份标识',
						image: '../static/3.png'
					}
				],
				avatar: '',
				userName: '匿名',
				vipList: [],
				selNum: 0,
				newPrice: 0,
				money: 0,
				price: 0,
				MemberList: [],
				isVip: true,
				openLists: [],
				openWay: 2,
				showpay: false,
			}
		},
		onLoad() {
			this.avatar = uni.getStorageSync('avatar')
			this.userName = uni.getStorageSync('userName')
			// #ifdef MP-WEIXIN
			this.openWay = 1;
			this.openLists = [{
				image: '../static/icon_weixin.png',
				text: '微信',
				id: 1
			}, {
				image: '../../static/images/lingqian.png',
				text: '余额',
				id: 3
			}]
			// #endif
			// #ifndef MP-WEIXIN
			this.openWay = 1;
			this.openLists = [{
				image: '../static/icon_weixin.png',
				text: '微信',
				id: 1
			}, {
				image: '../static/zhifubao.png',
				text: '支付宝',
				id: 2
			}, {
				image: '../../static/images/lingqian.png',
				text: '余额',
				id: 3
			}]
			// #endif
			this.getvipEndTime();
			this.getVipList()
			this.getMemberList()
			this.getMoney()
			this.getIsVip()
		},
		methods: {
			selectWay: function(id) {
				this.openWay = id;
			},
			getvipEndTime() {
				this.$Request.get("/app/UserVip/selectUserVip").then(res => {
					if (res.code == 0 && res.data) {
						this.endTime = res.data.endTime
					}
				});
			},
			//获取VIP列表
			getVipList() {
				this.$Request.get('/app/VipDetails/selectVipDetails').then(res => {
					if (res.code == 0) {
						this.vipList = res.data
						this.selNum = this.vipList[0].id
						this.price = this.vipList[0].money
					}
				})
			},
			// 获取特权列表
			getMemberList() {
				this.$Request.get('/app/member/selectMemberList').then(res => {
					if (res.code == 0) {
						this.MemberList = res.data
					}
				})
			},
			getIsVip() {
				this.$Request.get("/app/UserVip/isUserVip").then(res => {
					if (res.code == 0) {
						this.isVip = res.data
						this.$queue.setData('isVIP',this.isVip)
						if (this.isVip) {
							this.getvipEndTime();
						}
					}
				});
			},
			// 我的金元
			getMoney() {
				this.$Request.get("/app/userMoney/selectMyMoney").then(res => {
					if (res.code == 0 && res.data) {
						this.money = res.data.money
					}
				});
			},
			select(e) {
				this.selNum = e.id
				this.price = e.money
			},
			pay() {
				this.getMoney()
				this.showpay = false;
				this.$queue.showLoading('支付中...')
				let that = this;
				let data = {
					id: this.selNum,
					type: '2'
				}
				if (this.openWay == 1) {
					// #ifdef MP-WEIXIN
					that.$Request.postT("/app/wxPay/payUserVip", {
						vipDetailsId: that.selNum,
						type: 3
					}).then(red => {
						if (red.code == 0) {
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: red.data.timestamp,
								nonceStr: red.data.noncestr,
								package: red.data.package,
								signType: red.data.signType,
								paySign: red.data.sign,
								success: function(redd) {
									uni.hideLoading();
									uni.showToast({
										title: '开通成功！'
									})
									setTimeout(function() {
										that.isVip = true
										that.getIsVip()
									}, 1000)
								},
								fail: function(err) {
									uni.hideLoading();
									that.$queue.showToast(
										'支付失败');
								}
							});
						} else {
							uni.showToast({
								title: red.msg,
								icon: 'none'
							})
						}
					});
					// #endif
					
					// #ifdef H5
					let ua = navigator.userAgent.toLowerCase();
					console.log(ua)
					if (ua.indexOf('micromessenger') !== -1) {
						that.$Request.post("/app/wxPay/payUserVip", {
							vipDetailsId: that.selNum,
							type: 2
						}).then(red => {
							if (red.code == 0) {
								that.callPay(red.data);
							} else {
								uni.showToast({
									title: red.msg,
									icon: 'none'
								})
							}
						});
					}else{
						that.$Request.post("/app/wxPay/payUserVip", {
							vipDetailsId: that.selNum,
							type: 4
						}).then(red => {
							if (red.code == 0) {
								const urlArr = window.location.href;
								const hostUrl = urlArr.split("/");
								const callBack = hostUrl[0] + "//" + hostUrl[2] + "/";
								const url = '&redirect_url=' + callBack + 'my/vip/index';
								window.location = red.data.mweb_url + url
							} else {
								uni.showToast({
									title: red.msg,
									icon: 'none'
								})
							}
						});
					}
					// #endif
					
					// #ifdef APP-PLUS
					that.$Request.post("/app/wxPay/payUserVip", {
						vipDetailsId: that.selNum,
						type: 1
					}).then(red => {
						if (red.code == 0) {
							console.log(JSON.stringify(red))
							that.setPayment('wxpay', JSON.stringify(red
								.data));
						} else {
							uni.showToast({
								title: red.msg,
								icon: 'none'
							})
						}
					});
					// #endif
				} else if (this.openWay == 2) {
					// #ifdef H5
					that.$Request.post("/app/aliPay/payUserVip ", {
						vipDetailsId: that.selNum,
						classify: 2
					}).then(red => {
						if (red.code == 0) {
							const div = document.createElement('div')
							div.innerHTML = red.data //此处form就是后台返回接收到的数据
							document.body.appendChild(div)
							document.forms[0].submit()
						} else {
							uni.showToast({
								title: red.msg,
								icon: 'none'
							})
						}
					});
					// #endif
					// #ifdef APP-PLUS
					that.$Request.post("/app/aliPay/payUserVip", {
						vipDetailsId: that.selNum,
						classify: 1
					}).then(red => {
						if (red.code == 0) {
							that.setPayment('alipay', red.data);
						} else {
							uni.showToast({
								title: red.msg,
								icon: 'none'
							})
						}
					});
					// #endif
				} else if (this.openWay == 3) {
					if (this.money >= this.price) {
						this.$Request.postT('/app/artificer/payUserVip', {
							vipDetailsId: this.selNum
						}).then(res => {
							console.log(res.data)
							if (res.code == 0) {
								this.isVip = true
								this.getIsVip()
								uni.showToast({
									title: '开通成功！'
								})
								// setTimeout(function() {
								// 	uni.navigateBack()
								// },1000)
							} else {
								this.$queue.showToast(res.msg)
							}
						})

					} else {
						uni.showModal({
							title: '提示',
							content: '当前账户余额不足，请充值',
							success: function(res) {
								if (res.confirm) {
									console.log('用户点击确定');
									uni.navigateTo({
										url: '/my/wallet/index'
									})
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						})
					}
				}
			},
			callPay: function(response) {
				if (typeof WeixinJSBridge === "undefined") {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(response), false);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(response));
						document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(response));
					}
				} else {
					this.onBridgeReady(response);
				}
			},
			onBridgeReady: function(response) {
				let that = this;
				if (!response.package) {
					return;
				}
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": response.appid, //公众号名称，由商户传入
						"timeStamp": response.timestamp, //时间戳，自1970年以来的秒数
						"nonceStr": response.noncestr, //随机串
						"package": response.package,
						"signType": response.signType, //微信签名方式：
						"paySign": response.sign //微信签名
					},
					function(res) {
						if (res.err_msg === "get_brand_wcpay_request:ok") {
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
							uni.hideLoading();
							that.isVip = true
							that.getIsVip()
							uni.showToast({
								title: '开通成功！'
							})
						} else {
							uni.hideLoading();
						}
						WeixinJSBridge.log(response.err_msg);
					}
				);
			},
			setPayment(name, order) {
				let that = this;
				uni.requestPayment({
					provider: name,
					orderInfo: order, //微信、支付宝订单数据
					success: function(res) {
						uni.hideLoading();
						that.isVip = true
						that.getIsVip()
						uni.showToast({
							title: '开通成功！'
						})
					},
					fail: function(err) {
						uni.hideLoading();
						console.log(12)
					}
				});
			}
		},
		computed: {

		}
	}
</script>

<style lang="scss">
	.vip-content {
		width: 100%;
		min-height: 100vh;
		background: linear-gradient( 225deg, #13141D 0%, #55565D 35%, #15161F 100%);
		.container-box{
			padding: 80rpx 32rpx;
			.title-box{
				.title-img{
					width: 340rpx;
					height: 46rpx;
				}
				.desc{
					font-size: 28rpx;
					color: #707070;
					margin-top: 36rpx;
				}
			}
		}
	}

	.bgimg {
		position: absolute;
		height: 100vh;
		width: 100%;
	}

	.noactive {
		width: 216rpx;
		height: 226rpx;
		background: #4E4F55;
		border-radius: 32rpx 32rpx 32rpx 32rpx;
		border: 1rpx solid #F4E5BD;
		color: #FEE3AD;
	}

	.active {
		width: 216rpx;
		height: 226rpx;
		background: linear-gradient( 138deg, #FEE3AD 0%, #FFFFFF 20%, #BEA576 44%, #FFFFFF 71%, #FEE3AD 100%);
		border-radius: 32rpx 32rpx 32rpx 32rpx;
		color: #292929!important;
	}

	.save_vipbtn {
		width: 686rpx;
		height: 100rpx;
		background: linear-gradient( 121deg, #F9D385 0%, #FFEDCB 49%, #FEE3AD 100%);
		border-radius: 50rpx;
		text-align: center;
		line-height: 100rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #56260A;
		position: fixed;
		left: 32rpx;
		bottom: 100rpx;
		z-index: 9999;
	}

	.quanyi_view {
		margin-top: 36rpx;
		.quanyi_titview {
			.save_vipbtn {
				width: 686rpx;
				height: 100rpx;
				background: linear-gradient(90deg, #FEDFB1 0%, #FDC673 100%);
				border-radius: 50rpx;
				text-align: center;
				line-height: 100rpx;
				margin: 100rpx 30rpx 0rpx;
				font-size: 32rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #56260A;
			}

			.quanyi_itemview {
				margin-top: 20rpx;
				display: flex;
				width: 100%;
				flex-wrap: wrap;

				.quanyi_listitem {
					width: 25%;
					text-align: center;
					margin-top: 40rpx;

					.listitem_name {
						font-family: PingFang SC, PingFang SC;
						margin-top: 24rpx;
						font-weight: bold;
						font-size: 28rpx;
						color: #FEE3AD;
					}

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

			.quanyi_titname {
				font-weight: bold;
				font-size: 36rpx;
				color: #FEE3AD;
				margin: 0rpx 12rpx;
			}

			image {
				width: 114rpx;
				height: 14rpx
			}
		}

		image {
			width: 749rpx;
			height: 1255rpx;
		}
	}

	.popup_pay {
		width: 100%;
		position: relative;
		padding-bottom: 45rpx;
	}

	.pay_btn {
		width: 90%;
		margin: 0 auto;
		text-align: center;
		background: #2FB57A;
		height: 80rpx;
		border-radius: 16rpx;
		color: #ffffff;
		line-height: 80rpx;

	}

	.vip_kapian {
		width: 686rpx;
		height: 370rpx;
		box-sizing: border-box;
		background: url('@/static/image/vip_top.png') no-repeat;
		background-size: 686rpx 370rpx;
		margin-top: 63rpx;

		.img {
			width: 219rpx;
			height: 55rpx;
		}


		.kapian_ktname,.kapian_text,.kapian_ktmoney {
			font-size: 28rpx;
			color: #707070;
			font-family: PingFang SC;
			margin-top: 26rpx;
		}

		
	}
</style>
