<template>
	<view class="content">
		<!-- <image class="bgimg" src="../static/workbg.png" mode="widthFix"></image> -->
		<image class="bgimg" src="https://admin.sjajk.com/file/uploadPath/2023/01/16/0bd832e6cd894903f6ad2ece7f07824a.png" style="height: 100vh;"></image>
		<view class="bgbox" style="">
			<view class="" style="padding: 80rpx 0;text-align: center;">
				<image src="../../static/ktvip.png" style="width: 513rpx;height: 79rpx;"></image>
			</view>
			<view class="vip_kapian">
				<view style="position: absolute;z-index: 9;padding: 16rpx 40rpx;">
					<view class="kapian_name">VIP会员</view>
					<view class="kapian_text">会员享受超值VIP特权</view>
					<!-- <view class="kapian_ktmoney" v-if="!isVip">￥ <text>{{payMoney}}</text> </view> -->
					<view class="kapian_ktname" v-if="endTime">有效期至：{{endTime}}</view>
					<view class="kapian_ktmoney" v-if="!isVip">------</view>
				</view>
			</view>
			
			
			<!-- <view class="quanyi_view">
				<image :src="quanyiImage" mode="widthFix"></image>
			</view> -->
			<view class="quanyi_view">
				<image src="https://admin.sjajk.com/file/uploadPath/2023/01/16/902aa9b364bca4bb65e7513a6b7687b9.png"></image>
				<view class="quanyi_titview">
					
					<view class="flex margin-tb-xl flex-wrap padding-lr-sm" v-if="!isVip">
						<view v-for="(item,index) in vipList" :key='index' @click="select(item)" class=""
							style="display: inline-block;width: 216rpx;height: 250rpx;margin: 10rpx 10rpx;">
							<view class="text-center flex flex-direction justify-between padding-tb radius"
								:class="selNum == item.id?'active': 'noactive'">
								<view class="text-bold" style="font-size: 36rpx;">{{item.vipName}}</view>
								<view class="text-bold" style="font-size: 30rpx;">¥ <text class="text-xxl" style="font-size: 46rpx;margin-left: 10rpx;">{{item.money}}</text></view>
							</view>
						</view>
					</view>
					<view class="" style="width: 100%;padding: 0 100rpx;z-index: 999;margin-bottom: 50rpx;">
						<view class="save_vipbtn" v-if="!isVip" @tap="showpay = true" style="width: 100% !important;">立即开通</view>
					</view>
					<view class="flex justify-center align-center">
						<image src="../../static/vipleft.png"></image>
						<view class="quanyi_titname">VIP超值权益</view>
						<image src="../../static/vipleft.png"></image>
					</view>
					<view class="quanyi_itemview" style="margin-bottom: 60rpx;">
						<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>

			</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: 30rpx;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="#096f4b" :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">
	page{
		background-color: #32303a;
	}
	.content {
		width: 100%;
		min-height: 100vh;
	}

	.bgimg {
		position: absolute;
		height: 100vh;
		width: 100%;
	}
	.bgbox{
		background: url(../../static/vipbox.png);
		background-size: cover;
		height: 213px;
	}
	.noactive {
		color: #eecea7;
		width: 216rpx;
		height: 250rpx;
		background: linear-gradient(122deg, #2e2f41, #464b5e);
		border: 2px solid #bf9676;
	}

	.active {
		color: #483123;
		width: 216rpx;
		height: 250rpx;
		background: linear-gradient(122deg, #efcfa9, #fbe7cf);
		border: 4px solid #bf9676;
		border-radius: 8px;
	}

	.save_vipbtn {
		width: 100%;
		height: 80upx;
		line-height: 80upx;
		border-radius: 60upx;
		background: linear-gradient(90deg, #eecea8 0%, #fae8d0 100%);
		text-align: center;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #56260A;
		z-index: 9999;
	}

	.quanyi_view {
		position: absolute;
		z-index: 9;
		margin-top: -70rpx;

		.quanyi_titview {
			position: absolute;
			z-index: 99;
			top: 80rpx;
			width: 100%;

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

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

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

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

			.quanyi_titname {
				font-size: 38rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #deaa8a;
				margin: 0rpx 12rpx;
			}

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

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

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

	.pay_btn {
		width: 90%;
		margin: 0 auto;
		text-align: center;
		background: linear-gradient(to right, #223845, #00a85b);
		height: 80upx;
		border-radius: 60rpx;
		color: #ffffff;
		line-height: 80upx;
	}
	.vip_kapian {
		background: url(../../static/vipqy.png);
		background-size: cover;
		width: 100%;
		height: 332rpx;
		padding: 30rpx 0 0 40rpx;

		image {
			width: 686rpx;
			height: 320rpx;
			position: absolute;
		}

		.kapian_name {
			font-size: 42rpx;
			font-family: YouSheBiaoTiHei;
			font-weight: bold;
			color: #fdd193;
		}

		.kapian_text {
			font-size: 26rpx;
			font-family: PingFang SC;
			color: #fdd193;
			margin-top: 16rpx;
		}

		.kapian_ktname {
			font-size: 28rpx;
			font-family: PingFang SC;
			color: #fdd193;
			margin-top: 24rpx;
		}

		.kapian_ktmoney {
			font-size: 26rpx;
			font-family: PingFang SC;
			font-weight: 800;
			color: #fdd193;
			margin-top: 10rpx;

			text {
				font-size: 44rpx;
			}
		}
	}
</style>
