<template>
	<view class="content">
		<view class="top_box">
			<text class="yue-tit">当前余额：</text>
			<text class="yue-num">￥{{ now_money }}</text>
		</view>

		<view class="line_box"></view>

		<view class="cz_box">
			<view class="cz_wrap">
				<text class="cz_tit">￥</text>
				<input class="cz_input" type="number" v-model="money" @focus="changeNum()" placeholder="请输入充值金额" placeholder-class="placeholder" />
			</view>
			<view class="zc_list_box">
				<view class="zc_list_price" :class="{ seletPrice: seletNum == item.data.price }" @click="seletChange(item, index)" v-for="(item, index) in list" :key="index">
					{{ item.data.price }}元
				</view>
			</view>
		</view>
		<!-- <view class="line_box"></view> -->

		<!-- <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> -->

		<!-- <view class="rechar-box">
			<view class="rechar-title">注意事项</view>
			<view class="rechar-text" v-for="(item, index) in zhuyi" :key="index">
				<text>{{ index + 1 }}.{{item}}</text>
			</view>
		</view> -->
		<view class="line_box"></view>
		<!-- <view class="cz-type" @click.stop="changePayType('weixin')">
			<view class="type-left">
				<view class="icon iconfont iconweixin"></view>
				<view class="tit">微信充值</view>
			</view>
			<view class="right" v-show="loaded"><radio value="weixin" color="#EB001C" :checked="type == 'weixin'" /></view>
		</view> -->
		<view class="cz-type" @click.stop="changePayType('alipay')">
			<view class="type-left">
				<view class="icon iconfont iconzhifubao"></view>
				<view class="tit">支付宝充值</view>
			</view>
			<view class="right"><radio value="weixin" color="#EB001C" :checked="type == 'alipay'" /></view>
		</view>
		<button class="add-btn up" :class="{ 'active-bg': payLoding }" @click="!payLoding ? submitSub() : ''">立即充值</button>
	</view>
</template>

<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import { spreadInfo, rechargeRoutine, rechargeWechat, getRechargeApi, rechargeBrokerage } from '@/api/user.js';
import { mapGetters } from 'vuex';
import authorize from '@/components/Authorize';
export default {
	components: {
		authorize
	},
	data() {
		let that = this;
		return {
			type: 'alipay',
			otherValue: '其他',
			payLoding: false, //是否加载中
			now_money: 0,
			list: [{ data: { price: '' } }],
			seletNum: '300', // 选中
			navRecharge: ['账户充值', '佣金转入'],
			active: 0,
			number: '',
			userinfo: {},
			placeholder: '0.00',
			from: '',
			isAuto: false, //没有授权的不会自动授权
			isShowAuth: false, //是否隐藏授权
			picList: [],
			activePic: 0,
			money: '',
			numberPic: '',
			rechar_id: 0,
			rechargeAttention: []
		};
	},
	computed: mapGetters(['isLogin', 'viewColor']),
	onLoad(options) {
		// #ifdef H5
		this.from = this.$wechat.isWeixin() ? 'weixin' : 'h5';
		// #endif
		if (this.isLogin) {
			this.getUserInfo();
			this.getRecharge();
		} else {
			this.isAuto = true;
			this.isShowAuth = true;
		}
	},
	methods: {
		//选择支付方式
		changePayType(type) {
			this.type = type;
		},
		/**
		 * 充值额度选择
		 */
		getRecharge() {
			getRechargeApi()
				.then(res => {
					console.log(res);
					this.list = res.data.recharge_quota;
				})
				.catch(res => {
					this.$dialog.toast({
						mes: res
					});
				});
		},

		onLoadFun: function() {
			this.isShowAuth = false;
			this.getUserInfo();
			this.getRecharge();
		},
		// 授权关闭
		authColse: function(e) {
			this.isShowAuth = e;
		},
		navRecharges: function(index) {
			this.active = index;
		},
		seletChange(item, index) {
			this.seletNum = item.data.price;
			this.seletMoney = item.data.price;
			this.money = item.data.price;
		},
		changeNum() {
			this.seletNum = this.money = '';
			console.log(this.seletNum, this.money);
		},
		/**
		 * 获取用户信息
		 */
		getUserInfo: function() {
			let that = this;
			spreadInfo().then(res => {
				this.now_money = res.data.now_money;
			});
		},
		/*
		 * 用户充值
		 */
		submitSub: function(e) {
			let that = this;
			let value = this.money;
			// 转入余额
			if (that.active) {
				if (parseFloat(value) < 0 || parseFloat(value) == NaN || value == undefined || value == '') {
					return that.$util.Tips({
						title: '请输入金额'
					});
				}
				if (parseFloat(value) > that.userinfo.brokerage_price) {
					return that.$util.Tips({
						title: '剩余可用佣金不足' + parseFloat(value)
					});
				}
				uni.showModal({
					title: '转入余额',
					content: '转入余额后无法再次转出，确认是否转入余额',
					success(res) {
						if (res.confirm) {
							rechargeBrokerage({
								brokerage: parseFloat(value)
							})
								.then(res => {
									// that.$set(that, 'userinfo.now_money', that.$util.$h.Add(value, that.userinfo.now_money))
									return that.$util.Tips(
										{
											title: '转入成功',
											icon: 'success'
										},
										{
											tab: 5,
											url: '/pages/users/user_money/index'
										}
									);
								})
								.catch(err => {
									return that.$util.Tips({
										title: err
									});
								});
						} else if (res.cancel) {
							return that.$util.Tips({
								title: '已取消'
							});
						}
					}
				});
			} else {
				// uni.showLoading({
				// 	title: '正在支付',
				// 	mask: true
				// });

				if (this.rechar_id == 0) {
					if (parseFloat(that.money) === 0) {
						return that.$util.Tips({
							title: '充值金额金额不能为0！'
						});
					}
					if (!that.money) {
						return that.$util.Tips({
							title: '请填写充值金额！'
						});
					}
					if (!Number(that.money)) {
						return that.$util.Tips({
							title: '请填写正确的金额！'
						});
					}
				}
				that.payLoding = true;
				// #ifdef MP
				// let money = parseFloat(this.money);
				rechargeWechat({
					price: that.rechar_id == 0 ? that.money : that.numberPic,
					type: 'routine',
					recharge_id: this.rechar_id
				})
					.then(res => {
						uni.hideLoading();
						let jsConfig = res.data.config;
						uni.requestPayment({
							timeStamp: jsConfig.timestamp,
							nonceStr: jsConfig.nonceStr,
							package: jsConfig.package,
							signType: jsConfig.signType,
							paySign: jsConfig.paySign,
							success: function(res) {
								that.$set(that, 'userinfo.now_money', that.$util.$h.Add(value, that.userinfo.now_money));
								return that.$util.Tips(
									{
										title: '支付成功',
										icon: 'success'
									},
									{
										tab: 5,
										url: '/pages/users/user_money/index'
									}
								);
							},
							fail: function() {
								return that.$util.Tips({
									title: '支付失败'
								});
							},
							complete: function(res) {
								if (res.errMsg == 'requestPayment:cancel')
									return that.$util.Tips({
										title: '取消支付'
									});
							}
						});
					})
					.catch(err => {
						uni.hideLoading();
						return that.$util.Tips({
							title: err
						});
					});
				// #endif
				// #ifdef H5
				rechargeWechat({
					price: that.rechar_id == 0 ? that.money : that.numberPic,
					type: that.type,
					recharge_id: that.rechar_id
				})
					.then(res => {
						console.log(res);
						that.payLoding = false;
						window.location.href = res.data.config.jsConfig.expend.pay_info;
						// 	if (res.data.type == 'h5') {
						// 		let host = window.location.protocol + '//' + window.location.host;
						// 		let url = `${host}/pages/users/user_money/index`;
						// 		let eUrl = encodeURIComponent(url);
						// 		let locations = `${res.data.config.mweb_url}&redirect_url=${eUrl}`;
						// 		setTimeout(() => {
						// 			location.href = locations;
						// 		}, 100);
						// 	} else {
						// 		uni.hideLoading();
						// 		const jsConfig = res.data.config;
						// 		jsConfig.timeStamp = jsConfig.timestamp;
						// 		that.$wechat
						// 			.pay(jsConfig)
						// 			.then(res => {
						// 				that.$set(that, 'userinfo.now_money', that.$util.$h.Add(value, that.userinfo.now_money));
						// 				return that.$util.Tips(
						// 					{
						// 						title: '支付成功',
						// 						icon: 'success'
						// 					},
						// 					{
						// 						tab: 5,
						// 						url: '/pages/users/user_money/index'
						// 					}
						// 				);
						// 			})
						// 			.catch(res => {
						// 				if (res.errMsg == 'chooseWXPay:cancel')
						// 					return that.$util.Tips(
						// 						{
						// 							title: '取消支付'
						// 						},
						// 						{
						// 							tab: 5,
						// 							url: goPages + '?status=0'
						// 						}
						// 					);
						// 			});
						// 	}
					})
					.catch(error => {
						that.payLoding = false;
						return that.$util.Tips({
							title: error
						});
					});
				// #endif
				// #ifdef APP-PLUS
				rechargeWechat({
					price: that.rechar_id == 0 ? that.money : that.numberPic,
					recharge_id: that.rechar_id,
					type: 'weixin'
				}).then(res => {
					uni.hideLoading();
					uni.requestPayment({
						orderInfo: {
							...res.data.config,
							timeStamp: res.data.config.timestamp
						},
						provider: 'wxpay',
						success(res) {
							return that.$util.Tips(
								{
									title: '支付成功',
									icon: 'success'
								},
								{
									tab: 5,
									url: '/pages/users/user_money/index'
								}
							);
						},
						fail: function(rej) {
							console.log(rej);
							return that.$util.Tips({
								title: '支付失败'
							});
						},
						complete: function(res) {
							if (res.errMsg == 'requestPayment:cancel')
								return that.$util.Tips({
									title: '取消支付'
								});
						}
					});
				});
				// #endif
			}
		}
	}
};
</script>

<style lang="scss">
page {
	height: 100%;
	background: #ffffff;
}
.top_box {
	padding: 20rpx 26rpx 20rpx 47rpx;
	display: flex;
	align-items: center;
	justify-content: space-between;
	.yue-tit {
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
	}
	.yue-num {
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #ff6f0f;
	}
}

.line_box {
	width: 100%;
	height: 20rpx;
	background: #f8f8f8;
}
.cz_box {
	padding: 40rpx 20rpx 20rpx;
	.cz_wrap {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-bottom: 20rpx;
		border-bottom: 1px solid #e6e6e6;
		.cz_tit {
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
		}
		.cz_input {
			text-align: right;
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
			// color: #BFBFBF;
		}
	}
	.zc_list_box {
		display: flex;
		flex-wrap: wrap;
		.zc_list_price {
			width: 202rpx;
			height: 60rpx;
			background: #f0f0f0;
			border-radius: 8rpx;
			margin-top: 36rpx;
			margin-right: 50rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			&:nth-child(3n) {
				margin-right: 0;
			}
		}
		.seletPrice {
			background: linear-gradient(143.2747deg, #ff6a00, #ee0979);
			color: #ffffff;
		}
	}
}
.recha-box {
	padding: 50rpx 30rpx 0;
	display: flex;
	flex-wrap: wrap;
	.recha-frame {
		width: 210rpx;
		height: 181rpx;
		border: 1px solid #dbdede;
		border-radius: 20rpx;
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: center;
		margin-right: 30rpx;
		margin-bottom: 30rpx;
		&:nth-child(3n) {
			margin-right: 0;
		}
		.recha-top {
			display: flex;
			align-items: center;
			.recha-img {
				width: 64rpx;
				height: 62rpx;
			}
			.recha-tit {
				margin-left: 10rpx;
				font-size: 36rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #333333;
			}
		}
		.recha-song {
			margin-top: 20rpx;
			font-size: $font-base;
			font-family: PingFang SC;
			font-weight: bold;
			color: #ff9900;
		}
	}
	.select-frame {
		border: 1px solid #ef041f;
	}
}

.rechar-box {
	padding: 30rpx 20rpx 0;
	.rechar-title {
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
		margin-bottom: 10rpx;
	}
	.rechar-text {
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #333333;
	}
}
.cz-type {
	margin-top: 22rpx;
	display: flex;
	justify-content: space-between;
	padding: 32rpx;
	align-items: center;
	.type-left {
		display: flex;
		align-items: center;
		.icon {
			height: 38rpx;
			image {
				width: 48rpx;
				height: 38rpx;
			}
		}
		.iconweixin {
			color: #36cb59;
		}
		.tit {
			margin-left: 12rpx;
			font-size: 30rpx;
			color: #333333;
		}
	}
	.type-right {
		image {
			width: 36rpx;
			height: 36rpx;
		}
	}
}
.add-btn {
	&.modified {
	}
	&.up {
		color: #fff;
	}
	margin: 100rpx auto 0;
	display: flex;
	align-items: center;
	justify-content: center;
	width: 604rpx;
	height: 90rpx;
	// border: 2rpx solid #F21F5D;
	border-radius: 10rpx;
	font-size: 36rpx;
	font-family: PingFang SC;
	font-weight: 500;
	color: #fff;
	line-height: 90rpx;
	text-align: center;
	background-color: #ff4c4c;
}
.row-box {
	margin-top: 30rpx;
	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-left: 30rpx;
	margin-top: 30rpx;
	background-color: #ffffff;
	.box {
		display: flex;
		align-items: center;
		width: 100%;
		height: 120rpx;
		border-bottom: 1px solid $border-color-light;
		padding-right: 25rpx;
		.icon {
			font-size: 48rpx;
			padding-right: 20rpx;
			display: flex;
		}
		.yongjing {
			width: 48rpx;
			height: 48rpx;
		}
		.iconweixin1 {
			color: #18bf16;
		}
		.iconzhifubao {
			color: #08aaec;
		}
		.title-box {
			flex-grow: 1;
			text-align: left;
			.title {
				font-size: $font-base + 2rpx;
				color: $font-color-base;
			}
			.node {
				font-size: $font-sm;
				color: $font-color-light;
			}
		}
	}
}
/deep/ .uni-radio-input {
	width: 45rpx;
	height: 45rpx;
}

.active-bg {
	background: #999 !important;
	color: #ffffff !important;
}
.iconzhifubao {
	color: #08aaec;
}
</style>
