<template>
	<view class="recharge">
		<view class="task-navbar">
			<u-navbar title="充值金币" back-icon-color="#333333" title-color="#333333" :background="background"></u-navbar>
		</view>
		<view class="recharge-hd">
			<text>{{userInfo.goldCoins}}</text>金币余额
		</view>
		<view class="recharge-bd">
			<view class="item" v-for="(a,index) in list" :key="index">
				<image src="@/static/coins.png"></image>
				<view class="info"><view class="num"><text>{{a.coins}}</text>金币</view v-if="a.giveCoins">限时多送{{a.giveCoins}}金币</view>
				<view class="btn" @click="toOpen(a)">￥{{a.price}}</view>
			</view>
		</view>
		<view class="tips">提示:请勿相信非官方渠道的充值信息，禁止未成年人充值!请理性消费谨防诈骗行为，参考《平台公约》及《防诈骗指南》点击充值即表示您已同意《充值协议》</view>
		<u-popup v-model="show" mode="bottom">
			<view class="payPopup">
				<view class="title">选择充值方式</view>
				<view class="content">
					<template v-for="(a,i) in payInfo">
						<template v-if="platform == 'ios'">
							<view class="item" v-if="a=='wx-1' || a=='ali-1' || a=='integral-1' || a=='ios-1'" :key="i" :class="tabIndex == i?'item-on':''" @click="tabItem(i,a)">
								<view class="a" v-if="a=='wx-1'"><image src="@/static/wxpay.png"></image>微信支付</view>
								<view class="a" v-if="a=='ali-1'"><image src="@/static/zfb.png"></image>支付宝支付</view>
								<view class="a" v-if="a=='integral-1'"><image src="@/static/yue.png"></image>余额支付<text>（账户余额{{userInfo.money}}元）</text></view>
								<view class="a" v-if="a=='ios-1'"><image src="@/static/pgzf.png"></image>苹果支付</view>
								<view class="b">
									<image class="img1" src="@/static/icon-check.png"></image>
									<image class="img2" src="@/static/icon-check-on.png"></image>
								</view>
							</view>
						</template>
						<template v-else>
							<view class="item" v-if="a=='wx-1' || a=='ali-1' || a=='integral-1'" :key="i" :class="tabIndex == i?'item-on':''" @click="tabItem(i,a)">
								<view class="a" v-if="a=='wx-1'"><image src="@/static/wxpay.png"></image>微信支付</view>
								<view class="a" v-if="a=='ali-1'"><image src="@/static/zfb.png"></image>支付宝支付</view>
								<view class="a" v-if="a=='integral-1'"><image src="@/static/yue.png"></image>余额支付<text>（账户余额{{userInfo.money}}元）</text></view>
								<view class="b">
									<image class="img1" src="@/static/icon-check.png"></image>
									<image class="img2" src="@/static/icon-check-on.png"></image>
								</view>
							</view>
						</template>
					</template>
				</view>
				<view class="fd">
					<view class="btn" @click="toCreate()">立即充值</view>
				</view>
			</view>
		</u-popup>
		<unverified :isPopup="isPopup" />
	</view>
</template>

<script>
	const systemInfo = uni.getSystemInfoSync();
	export default {
		data() {
			return {
				platform: "ios",
				tabIndex: -1,
				isPopup: false,
				show: false,
				background: {
					// 导航栏背景图
					background: 'url(https://api.lyiyuan.cn/profile/upload/static/task-bg.png) center top no-repeat',
					// 还可以设置背景图size属性
					backgroundSize: '100 auto',
				},
				list: [],
				userInfo: {},
				payInfo: [],
				obj: {}
			}
		},
		onReady() {
			const that = this;
			that.platform = systemInfo.platform;
			this.getList();
			this.getConfigData();
			this.getUserInfo();
			uni.$on("closeSharePopup",res=>{
				that.isPopup = false;
			})
		},
		methods: {
			toOpen(o) {
				this.show = true;
				this.obj = o;
			},
			tabItem(index,a){
				this.tabIndex = index;
				this.payType = a
			},
			//创建订单
			async toCreate() {
				const that = this;
				if(this.userInfo.teenageMode == 1){
					uni.showModal({
						title: "温馨提示",
						content: "已开启青少年模式，不可充值!"
					})
					return ;
				}
				if(!this.payType){
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "请选择支付方式"
					})
					return ;
				}
				if(this.payType == 'integral-1'){
					if(this.obj.giveCoins > this.userInfo.money){
						uni.showToast({
							icon: "none",
							position: "bottom",
							title: "账户余额不足"
						})
						return ;
					}
				}
				if(this.platform == 'ios'){
					if(this.payType == 'ios-1'){
						plus.payment.getChannels((channels) => {
							console.log(channels);
							for (var i in channels) {
								var channel = channels[i];
								if (channel.id == 'appleiap'){
									console.log("苹果")
									let iap = channel;
									//支付参数
									iap.requestOrder(['chunwanProPackage'], function(orderList){
											//必须调用此方法才能进行 iap 支付 
											console.log('requestOrder success: ' + JSON.stringify(orderList))
											// that.applePay(orderList[0])
										},function(e){
											console.log('requestOrder failed: ' + JSON.stringify(e));
											// console.log('暂不支持苹果 iap 支付')
									});
								}	
							}
						}, function(e) {
							console.log("获取iap支付通道失败：" + e.message);
						});
					
						return ;
					}
				} 
				that.show = false;
				that.tabIndex = -1;
				uni.showLoading({
					title: "正在支付"
				})
				const {code, data, msg} = await that.$api.mealsetRechargeCreateOrder({
					userId: uni.getStorageSync("userInfo").id,
					mealsetId: this.obj.id
				})
				if(code == 200){
					that.toPay(msg);
				}else{
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: msg
					})
				}
			},
			//支付
			async toPay(outTradeNo) {
				const that = this;
				const {code, data, msg} = await that.$api.paymentOrder({
					payType: that.payType =='wx-1'?1:(that.payType=='ali-1'?2:3),
					outTradeNo
				})
				if(code == 200){
					uni.hideLoading()
					if(that.payType == 'integral-1'){
						uni.showToast({
							title: "支付成功"
						})
						that.payType = ""
						setTimeout(()=>{
							that.getUserInfo();
							that.getList();
						},2000)
						return ;
					}
					uni.requestPayment({
					   provider: that.payType == 'ali-1'?'alipay':'wxpay',
					   orderInfo: that.payType == 'ali-1'?data.orderInfo:JSON.parse(data.orderInfo), //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
						success: function (res) {
							uni.showToast({
								title: "支付成功"
							})
							setTimeout(()=>{
								that.getUserInfo();
								that.getList();
							},2000)
						},
						fail: function (err) {
							console.log('fail:' + JSON.stringify(err));
						}
					});
				}else{
				uni.hideLoading();
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: msg
					})
				}
			},
			//查询列表
			async getList() {
				const { code, data } = await this.$api.mealsetList({page: 1,limit: 9999});
				if(code == 200){
					this.list = data.list;
				}
			},
			//字典查询
			async getConfigData() {
				const that = this;
				let res1 = await that.$api.getConfigData({dictType: 'CONFIG_PAY_STATUS'});
				if(res1.data.content){
					this.payInfo = res1.data.content.split(",");
					console.log(this.payInfo)
				}
			},
			//查询个人信息
			async getUserInfo() {
				const { code, data , msg } = await this.$api.findUserInfo({userId: uni.getStorageSync("userInfo").id});
				if(code == 200){
					this.userInfo = data;
					uni.setStorageSync("userInfo",data);
				}else{
					uni.showToast({
						title: msg,
						position: "bottom",
						icon: "none",
					})
				}
			},
		}
	}
</script>

<style scoped lang="scss">
.recharge{
	background: url(https://api.lyiyuan.cn/profile/upload/static/task-bg.png) center top no-repeat;
	background-size: 100% auto;
	overflow: hidden;
	min-height: 100vh;
	padding: 0 25rpx 0;
	overflow: hidden;
	.payPopup{
		padding: 40rpx;
		overflow: hidden;
		.title{
			font-weight: 500;
			font-size: 36rpx;
			color: #333333;
			text-align: center;
		}
		.btn{
			width: 100%;
			line-height: 98rpx;
			background: linear-gradient(0deg, #000000, #3D3B38);
			box-shadow: 0px 4rpx 18rpx 0px rgba(42,41,39,0.34);
			border-radius: 98rpx;
			font-weight: 400;
			text-align: center;
			font-size: 30rpx;
			color: #FFFFFF;
			margin-top: 30rpx;
		}
		.content{
			padding: 0 30rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			margin-top: 20rpx;
			.item{
				padding: 30rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-weight: 400;
				font-size: 28rpx;
				color: #333333;
				border-top: 1px solid #EAEAEA;
				&:first-child{
					border-top-color: #FFFFFF;
				}
				.a{
					flex: 1;
					display: flex;
					align-items: center;
					text{
						margin-left: 20rpx;
						font-size: 22rpx;
						color: #999999;
					}
					image{
						width: 60rpx;
						height: 60rpx;
						margin-right: 10rpx;
					}
				}
				.b{
					font-size: 28rpx;
					color: #CCCCCC;
					display: flex;
					align-items: center;
					image{
						width: 32rpx;
						height: 32rpx;
						margin-left: 20rpx;
					}
					.img1{
						display: block;
					}
					.img2{
						display: none;
					}
				}
				&-on{
					.b{
						.img1{
							display: none;
						}
						.img2{
							display: block;
						}
					}
				}
			}
		}
	}
	.tips{
		padding: 45rpx 0;
		text-align: center;
		font-weight: 500;
		font-size: 24rpx;
		color: #999999;
		line-height: 40rpx;
	}
	&-hd{
		width: 100%;
		height: 200rpx;
		background: linear-gradient(90deg, #17150E, #373632);
		border-radius: 20rpx;
		text-align: center;
		font-weight: 500;
		font-size: 24rpx;
		color: #E7D4B8;
		padding-top: 55rpx;
		box-sizing: border-box;
		margin-top: 30rpx;
		text{
			font-size: 44rpx;
			display: block;
			margin-bottom: 20rpx;
		}
	}
	&-bd{
		overflow: hidden;
		.item{
			width: 100%;
			height: 126rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 30rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 20rpx;
			image{
				width: 60rpx;
				height: 60rpx;
				margin-right: 20rpx;
			}
			.btn{
				width: 160rpx;
				line-height: 70rpx;
				background: linear-gradient(0deg, #000000, #3D3B38);
				box-shadow: 0px 4rpx 18rpx 0px rgba(42,41,39,0.34);
				font-weight: 500;
				font-size: 28rpx;
				color: #FFFFFF;
				border-radius: 70rpx;
				text-align: center;
			}
			.info{
				flex: 1;
				font-weight: 500;
				font-size: 24rpx;
				color: #999999;
				.num{
					font-size: 28rpx;
					font-weight: 500;
					color: #222222;
					text{
						
						font-size: 36rpx;
					}
				}
			}
		}
	}
}
</style>
