<template>
	<view class="content">
		<view class="row-box">
			<view class="title">充值金额</view>
			<view class="row">
				<text class="tit">￥</text>
				<input class="input" type="number" v-model="money" placeholder="请输入充值金额" placeholder-class="placeholder" />
			</view>
		</view>
		<!-- #ifndef MP -->
		<view class="list">
			<radio-group @change="tabRadio">
				<label>
					<view class="box">
						<view class="icon iconfont iconzhifubao"></view>
						<view class="title-box">
							<view class="title"><text>支付宝充值</text></view>
						</view>
						<view class="right"><radio value="alipay" color="#5dbc7c" :checked="type == 'alipay'" /></view>
					</view>
				</label>
				<label>
					<view class="box1">
						<view class="icon iconfont iconweixin1"></view>
						<view class="title-box">
							<view class="title"><text>微信充值</text></view>
						</view>
						<view class="right"><radio value="weixin" color=" #5dbc7c" :checked="type == 'weixin'" /></view>
					</view>
				</label>
			</radio-group>
		</view>
		<!-- #endif -->
		<button class="add-btn up" :class="{ 'active-bg': payLoding }" @click="!payLoding ? confirm() : ''">确认</button>
	</view>
</template>

<script>
import { getMoneyStyle } from '@/utils/rocessor.js';
// #ifdef H5
import { rechargeWechat } from '@/api/wallet.js';
// #endif
// #ifdef MP
import { rechargeRoutine } from '@/api/wallet.js';
// #endif
import { mapState } from 'vuex';
export default {
	filters: {
		getMoneyStyle
	},
	data() {
		return {
			type: 'weixin',
			money: '', //充值金额
			payLoding: false //是否加载中
		};
	},
	onLoad(options) {},
	computed: {
		// #ifdef H5
		...mapState(['weichatObj'])
		// #endif
	},
	methods: {
		// 跳转
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		},
		// 切换选中对象
		tabRadio(e) {
			this.type = e;
			console.log(this.type,'this.type')
		},
		// 提交
		confirm() {
			let obj = this;
			obj.payLoding = true;
			console.log(obj.money);
			if ( obj.money == '') {
				this.$api.msg('金额不能为空');
				obj.payLoding = false;
				return
			}
			// #ifdef APP-PLUS
			if(obj.type == 'alipay')
			{
				uni.requestPayment({
				    provider: 'alipay',
				    orderInfo: 'orderInfo', //微信、支付宝订单数据
				    success: function (res) {
				        console.log('success:' + JSON.stringify(res));
				    },
				    fail: function (err) {
				        console.log('fail:' + JSON.stringify(err));
				    }
				});
			}else{
				uni.requestPayment({
				    provider: 'weixin',
				    orderInfo: 'orderInfo', //微信、支付宝订单数据
				    success: function (res) {
				        console.log('success:' + JSON.stringify(res));
				    },
				    fail: function (err) {
				        console.log('fail:' + JSON.stringify(err));
				    }
				});
			}
			// #endif
			
			// #ifdef H5
			rechargeWechat({ price: this.money, from: this.type })
				.then(e => {
					let da = e.data.data;
					obj.weichatObj.chooseWXPay({
						timestamp: da.timestamp,
						nonceStr: da.nonceStr,
						package: da.package,
						signType: da.signType,
						paySign: da.paySign,
						success: function(res) {
							uni.navigateTo({
								url: '/pages/money/rechargeSuccess'
							});
							/* uni.showToast({
								title: '充值成功',
								duration: 2000,
								position: 'top'
							}); */
						}
					});
					obj.payLoding = false;
				})
				.catch(e => {
					obj.payLoding = false;
					console.log(e);
				});
			// #endif
			
			// #ifdef MP
			rechargeRoutine({ price: this.money})
				.then(e => {
					let da = e.data;
						wx.requestPayment({
								timeStamp: da.timestamp,
								nonceStr: da.nonceStr,
								package: da.package,
								signType: da.signType,
								paySign: da.paySign,
								success: function(res) {
									uni.redirectTo({
										url: '/pages/money/paySuccess'
									});
								},
								fail:function(res){
									uni.showToast({
									    title: '支付失败',
									    duration: 3000
									});
								},
								complete:function(){}
							})
					
					obj.payLoding = false;
				})
				.catch(e => {
					obj.payLoding = false;
					console.log(e);
				});
			// #endif
		}
	}
};
</script>

<style lang="scss">
page {
	height: 100%;
}

.add-btn {
	&.modified {
		color: $base-color;
	}
	&.up {
		background:$base-color;
		color: #fff;
	}
	display: flex;
	align-items: center;
	justify-content: center;
	width: 560rpx;
	height: 80rpx;
	margin: 0 auto;
	margin-top: 60rpx;
	font-size: $font-lg;
	border-radius: 40rpx;
}
.row-box {
	border-top: 1px solid #EEEEEE;
	padding: 20rpx 30rpx;
	background: #fff;
	.title {
		font-size: $font-base + 2rpx;
		color: $font-color-dark;
	}
	.row {
		display: flex;
		align-items: center;
		position: relative;
		height: 80rpx;
		.tit {
			flex-shrink: 0;
			width: 40rpx;
			font-size: 30rpx;
			color: $font-color-dark;
		}
		.input {
			flex: 1;
			font-size: 30rpx;
			color: $font-color-dark;
		}
		.iconlocation {
			font-size: 36rpx;
			color: $font-color-light;
		}

		.buttom {
			color: $font-color;
			font-size: $font-base;
		}
	}
}
.list {
	padding:0 30rpx;
	margin-top: 20rpx;
	background-color: #ffffff;
	.box {
		display: flex;
		align-items: center;
		width: 100%;
		height: 100rpx;
		border-bottom: 1px solid $border-color-light;
	}
	.box1{
		display: flex;
		align-items: center;
		width: 100%;
		height: 100rpx;
	}
	.icon {
		font-size: 48rpx;
		padding-right: 20rpx;
	}
	.iconweixin1 {
		color: #18bf16;
	}
	.iconzhifubao {
		color: #08aaec;
	}
	.title-box {
		flex-grow: 1;
		text-align: left;
		.title {
			font-size: $font-base + 2rpx;
			color: #333333;
		}
		/* .node {
			font-size: $font-base;
			color: #333333;
		} */
	}
	image {
		width: 25rpx;
		height: 25rpx;
	}
}
/deep/ .uni-radio-input {
	width: 45rpx;
	height: 45rpx;
}
.active-bg {
	background-color: $color-gray !important;
}
</style>
