<template>
	<view class="content">
		<view class="card_info flex-colum">
			<view class="title">账户余额(元)</view>
			<view class="card_info_con flex-between">
				<view class="balance">{{userInfo.amount || '0.00'}}</view>
				<view class="card_info_con_right flex-end">
					<view class="card_info_con_right_item flex-colum" v-for="(item,index) in cardManageList" :key="index">
						<image class="card_info_con_right_item_icon" :src="item.icon" mode="aspectFill"></image>
						<text class="card_info_con_right_item_text">{{item.name}}</text>
					</view>
				</view>
			</view>
		
		</view>
	<!-- 	<view class="therecontent">
			<input type="number" v-model="amount" placeholder="自定义金额">
		</view> -->
		<view class="rechargeList">
			<view class="rechargeList_f"  @click="clickinput(item,index)" v-for="(item,index) in listdata" :key="index">
				<viwe class="rechargeList_item flex-colum " :class="index == inputshow?'active':''">
					<view class="rechargeList_item_title">充值</view>
					<view class="flex-colum-start">
						<view class="rechargeList_item_amount">￥<text>{{item.minNum}}</text></view>
						<view class="rechargeList_item_gift" v-show="index == inputshow">
							<image class="rechargeList_item_gift_bg" src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/menber/gift_icon.png" mode="aspectFill"></image>
							<view class="rechargeList_item_gift_text">
								<text>赠</text>
								<text>￥{{ item.handselNum }}</text>
							</view>
						</view>
						<view class="rechargeList_item_handsel" v-show="index != inputshow">赠送{{item.handselNum}}元</view>
					</view>
					
				</viwe>
			</view>
		</view>
		
		<!-- 开通会员 -->
		<u-popup :show="memberOpen" :closeOnClickOverlay="true" overlayOpacity="0.8" :round="20" mode="bottom" @close="memberCancel" height="500">
			<view class="u-popup-content">
				<image src="https://czg-qr-order.oss-cn-beijing.aliyuncs.com/wkthuiyuanbg.png" class="imgstyle" mode=""></image>
				<button class="btnclass" open-type="getPhoneNumber" @getphonenumber="sumbit">立即开通 </button>
			</view>
		</u-popup>
		
	</view>

</template>

<script>
	export default {
		components: {
		},
		data() {
			return {
				inputshow: 0,
				listdata: [],
				amount: '',
				userInfo: {},
				shopId: '',
				tokenShow: true,
				memberOpen: false,
				usershopUserinfo: null,
			}
		},
		onLoad(e) {
			console.log(e)
			if ( e.type == 'list' || e.type == 'index') {
				this.shopId = e.shopId;
				this.init();
			}
			if (e.q) {
				this.shopId = this.getQueryString(decodeURIComponent(e.q), 'shopId')
				if (!uni.cache.get('token')) {
					uni.login({
						provider: 'weixin',
						success: async (data) => {
							try {
								uni.getUserInfo({
									provider: 'weixin',
									success: async (infoRes) => {
										uni.cache.set('weixincode', data.code);
										let res = await this.api.userwxlogin({
											code: uni.cache.get('weixincode'), //临时登录凭证
											rawData: infoRes.rawData
										})
										if (res.code == 0) {
											uni.cache.set('token', res.data.token);
											uni.cache.set('miniAppOpenId', res.data.userInfo
												.miniAppOpenId)
											uni.cache.set('userInfo', res.data.userInfo);
											this.tokenShow = false;
											this.init();
										}
									},
									fail: (err) => {}
								});
							} catch (e) {}
						}
					});
				} else {
					this.init();
				}
			}
			console.log(this.shopId)
		},
		methods: {
			async init() {
				let res = await this.api.shopUserInfo({
					"shopId": this.shopId,
					"userId": uni.cache.get('userInfo').id,
				})
				if (res.code == 0) {
					this.usershopUserinfo = res.data
				}
				if ( this.usershopUserinfo.isVip == 0 ) {
					this.memberOpen = true;
					return;
				}
				this.paygetShopByMember(this.shopId)
				this.paygetActive()
			},
			memberCancel ()  {
				let pages = getCurrentPages()
				if ( pages.length > 1) {
					uni.navigateBack()
				} else {
					uni.exitMiniProgram({
					    success: function () {
					      console.log('退出成功');
					    },
					    fail: function () {
					      console.log('退出失败');
					    }
					});
				}
				
			},
			sumbit(d) {
				if (d.detail.iv) {
					uni.login({
						provider: 'weixin',
						success: async (data) => {
							let res = await this.api.userwxlogins({
								code: data.code,
								encryptedData: d.detail.encryptedData,
								iv: d.detail.iv,
								shopId: this.shopId
							})
							if (res.code == 0) {
								let resdata = await this.api.loginwxuserInfo({
									userId: uni.cache.get('userInfo').id
								})
								if (resdata.code == 0) {
									uni.cache.set('userInfo', resdata	.data);
									this.memberOpen = false
									this.paygetShopByMember(this.shopId)
									this.paygetActive()
								}
			
			
							}
						}
					})
				}
			},
			getQueryString(url, name) { //解码
				var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
				var r = url.substr(1).match(reg)
				if (r != null) {
					return r[2]
				}
				return null;
			},
			async paygetShopByMember(w) {
				let res = await this.api.paygetShopByMember({
					page: 1,
					pageSize: 10,
					userId: uni.cache.get('userInfo').id,
					shopId: this.shopId
				})
				this.userInfo = res.data.list[0]
			},
			async paygetActive() {
				let res = await this.api.paygetActive({
					shopId: this.shopId,
					page: 1,
					pageSize: 10
				})
				
				try {
					this.listdata = res.data.list
					this.amount = res.data.list[0].minNum
				} catch (e) {
					//TODO handle the exception
				}
			},
			async userbalancerechangesub() {
				if (this.amount == null || this.amount == '') {
					uni.showToast({
						title: '金额不能为空',
						icon: 'none'
					});
					return false;
				}
				if (this.amount <= 0) {
					uni.showToast({
						title: '金额必须大于0',
						icon: 'none'
					});
					return false;
				}
				let res = await this.api.paymemeberIn({
					shopId: this.shopId, // 判断显示哪家的作品,
					amount: this.amount // 判断显示哪家的作品,
				})
				if (res.code == 0) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					// #ifdef MP-WEIXIN
					uni.requestPayment({
						provider: 'wxpay', //支付类型-固定值
						partnerid: res.data.appId, // 微信支付商户号
						timeStamp: res.data.timeStamp, // 时间戳（单位：秒）
						nonceStr: res.data.nonceStr, // 随机字符串
						package: res.data.package, // 固定值
						signType: res.data.signType, //固定值
						paySign: res.data.paySign, //签名
						success: (res) => {
							uni.showToast({
								title: "支付成功"
							})
							uni.hideLoading()
							let pages = getCurrentPages()
							if ( pages.length > 1) {
								setTimeout(res => {
									uni.navigateBack()
								}, 500)
							} else {
								uni.exitMiniProgram({
								    success: function () {
								      console.log('退出成功');
								    },
								    fail: function () {
								      console.log('退出失败');
								    }
								});
							}
						
						},
						fail: (res) => {
							uni.hideLoading()
						}
					});
					uni.hideLoading()
					// #endif	
				}
			},
			clickinput(a, b) {
				console.log(a, b)
				this.inputshow = b
				this.amount = a.minNum
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #F9F9F9;
	}

	.content {
		padding: 0 20rpx;
		
		.card_info{
			width: 100%;
			height: 182rpx;
			background: linear-gradient( 132deg, #D6B68D 0%, #E6D6BC 100%);
			border-radius: 12rpx;
			padding: 32rpx 28rpx;
			align-items: initial;
			margin-bottom: 26rpx;
			.title{
				font-weight: 500;
				font-size: 32rpx;
				color: #333333;
			}
			.card_info_con{
				align-items: flex-end;
				.balance{
					font-weight: 400;
					font-size: 48rpx;
					color: #333333;
				}
				.card_info_con_right{
					.card_info_con_right_item{
						margin-left: 64rpx;
						.card_info_con_right_item_icon{
							width: 40rpx;
							height: 36rpx;
							margin-bottom: 5rpx;
						}
						.card_info_con_right_item_text{
							font-weight: 400;
							font-size: 24rpx;
							color: #333333;
						}
					}
				}
			}
		}
		
		.rechargeList{
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			.rechargeList_f{
				width: 33.333%;
				margin-bottom: 30rpx;
				padding-right: 20rpx;
				padding-left: 0;
				.rechargeList_item{
					height: 130rpx;
					border-radius: 0rpx 48rpx 0rpx 0rpx;
					border: 4rpx solid #E5E5E5;
					align-items: initial;
					padding: 26rpx 22rpx;
					justify-content: space-between;
					position: relative;
				
					.rechargeList_item_title,.rechargeList_item_handsel{
						font-weight: 400;
						font-size: 20rpx;
						color: #999999;
					}
					.rechargeList_item_amount{
						font-weight: bold;
						font-size: 28rpx;
						color: #666666;
						text{
							font-size: 44rpx;
							font-weight: bold;
							color: #666666;
						}
					}
					.rechargeList_item_handsel{
						
					}
					.rechargeList_item_gift{
						width: 92.52rpx;
						height: 88.74rpx;
						position: absolute;
						top: 12rpx;
						right: 12rpx;
						.rechargeList_item_gift_bg{
							width: 92.52rpx;
							height: 88.74rpx;
							position: absolute;
						}
						.rechargeList_item_gift_text{
							width: 92.52rpx;
							height: 88.74rpx;
							position: absolute;
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;
							text:nth-child(1){
								font-weight: bold;
								font-size: 24rpx;
								color: #C14D3A;
							}
							text:nth-child(2){
								font-weight: 400;
								font-size: 20rpx;
								color: #F7664E;
							}
						}
					}
				}
				.rechargeList_item.active{
					border: 4rpx solid #DCC19E;
					background: linear-gradient( 133deg, #F9F6ED 0%, #FFFFFF 100%);
					.rechargeList_item_title{
						color: #F7664E;
					}
					.rechargeList_item_amount{
						font-weight: bold;
						font-size: 28rpx;
						color: #FB604A;
						text{
							font-size: 44rpx;
							font-weight: bold;
							color: #FB604A;
						}
					}
				}
			}
		
			.rechargeList_f:nth-child(3n-1){
				padding-right: 10rpx;
				padding-left: 10rpx;
			}
			.rechargeList_f:nth-child(3n){
				padding-right: 0;
				padding-left: 20rpx;
			}
		}
		.imgstyle {
			width: 694rpx;
			height: 414rpx;
			margin: 70rpx auto;
		}
		
		.btnclass {
			width: 694rpx;
			height: 90rpx;
			line-height: 90rpx;
			background: #6D89A4;
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			text-align: center;
			margin: auto;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #FFFFFF;
			bottom: 50rpx;
			position: absolute;
			left: 0;
			right: 0;
			margin: auto;
		
		}
		.u-popup-content {
		  height: 80vh; /* 设置高度为视口高度的50% */
		  width: 100%;
		  /* 其他样式 */
		}
	}

</style>