<template>
	<view class="content">
		<view class="bg">
		</view>
		<view class="listBox">
			<view class="list flex">
				<view @click="changeMoney(index)" :class="{action:index==actionIndex}" class="item flex"
					v-for="(item,index) in moneyList">
					<text>{{item.price}}元</text>
				</view>
			</view>
			<view class="flex inputBox">
				<text class="text">自定义金额</text>
				<input class="input" type="number" v-model="money" />
				<text>元</text>
			</view>
		</view>

		<view class="yt-list">
			<view class="yt-list-cell b-b" v-if="fx" @click="type='wxpay'">
				<view class="cell-tit flex">
					<image class="orderIcon" src="../../../static/icon/orderWx.png" mode="widthFix"></image>
					<text class="margin-l-10">微信支付</text>
				</view>
				<image class="checked" v-if="type=='wxpay'" src="../../../static/icon/addressIconXz.png"
					mode="widthFix"></image>
				<view v-else class="noChecked"></view>
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="yt-list-cell b-b" @click="type='alipay'">
				<view class="cell-tit flex">
					<image class="orderIcon" src="../../../static/icon/orderAli.png" mode="widthFix"></image>
					<text class="margin-l-10">支付宝</text>
				</view>
				<image class="checked" v-if="type=='alipay'" src="../../../static/icon/addressIconXz.png"
					mode="widthFix">
				</image>
				<view v-else class="noChecked"></view>
			</view>
			<!-- #endif -->
<!-- 			<view class="yt-list-cell b-b" @click="type='commission'">
				<view class="cell-tit flex">
					<image class="orderIcon" src="../../../static/icon/ye.png" mode="widthFix"></image>
					<text class="margin-l-10">佣金({{commissionMoney}})</text>
				</view>
				<image class="checked" v-if="type=='commission'" src="../../../static/icon/addressIconXz.png"
					mode="widthFix">
				</image>
				<view v-else class="noChecked"></view>
			</view> -->
		</view>

		<view class="base-buttom" :class="{ 'active-bg': payLoding }" @click="!payLoding ? confirm() : ''">确认充值</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		commissionPay
	} from '@/api/wallet.js';
	// #ifdef APP
	import {
		aliPay,
		wxPay
	} from '@/api/wallet.js';
	// #endif
	// #ifdef H5
	import weixinObj from "@/plugin/jweixin-module/index.js";
	import {
		rechargeWechat
	} from '@/api/wallet.js';
	// #endif
	// #ifdef MP
	import {
		rechargeRoutine
	} from '@/api/wallet.js';
	// #endif

	import {
		rechargeIndex,
		spreadCommission,
		extractBank
	} from '@/api/wallet.js';


	export default {
		data() {
			return {
				actionIndex: 0, //当前选中的充值金额对象
				type: 'alipay',
				money: '', //充值金额
				payLoding: false, //是否加载中
				moneyList: [],
				commissionMoney: 0, //可提现佣金
			};
		},
		onLoad(options) {
			this.rechargeIndex();
			this.extractBank();
		},
		computed:{
			...mapState(['fx'])
		},
		methods: {
			//获取用户金额信息
			extractBank() {
				extractBank({}).then(({
					data
				}) => {
					// 保存佣金信息
					this.commissionMoney = data.commissionCount;
				});
			},
			// 金额修改
			changeMoney(ind) {
				this.actionIndex = ind;
				this.money = this.moneyList[ind].price
			},
			// 查询金额列表
			rechargeIndex() {
				rechargeIndex().then((e) => {
					this.moneyList = e.data.recharge_quota.map((e) => {
						e.price = +e.price;
						return e
					});
					// 设置默认金额值
					this.money = this.moneyList[0].price
				})
			},
			// 跳转
			navTo(url) {
				uni.navigateTo({
					url: url
				});
			},
			// 提交
			confirm() {
				const that = this;
				if (that.money < (+that.moneyList[0].price)) {
					uni.showModal({
						title: '失败',
						content: '充值金额不可以低于￥' + that.moneyList[0].price,
						showCancel: false
					});
					return
				}
				that.payLoding = true;
				let pushData = {
					price: this.money,
					// #ifdef H5
					from: 'weixin',
					// #endif
					// #ifdef APP
					from: 'app',
					// #endif
				}
				// 佣金转余额
				if (that.type == 'commission') {
					that.commissionPay(pushData)
					return
				}
				// 微信支付
				if (that.type == 'wxpay') {
					that.wxpay(pushData)
					return
				}
				// #ifdef APP
				// 支付宝支付
				if (that.type == 'alipay') {
					console.log('ali');
					that.alipay(pushData)
					return
				}
				// #endif
				// 微信支付

			},
			commissionPay(pushData) {
				const that = this;
				commissionPay(pushData).then((e) => {
					that.payLoding = false;
					if (e.status == 200) {
						that.extractBank()
						uni.redirectTo({
							url: './paySuccess?type=1'
						});
						return
					}
					uni.showToast({
						title: e.msg
					});
				}).catch(() => {
					that.payLoding = false;
				})
			},
			// #ifdef APP-PLUS
			alipay(pushData) {
				const that = this;
				aliPay(pushData).then((e) => {
					let orderInfo={};
					that.payLoding = true;
					uni.requestPayment({
						provider: that.type,
						orderInfo:e.data.data,
						success(e) {
							uni.redirectTo({
								url: './paySuccess?type=1'
							});
						},fail(e) {
							console.log(e);
						}
					})
				}).catch(e => {
					that.payLoding = false;
					console.log(e,'cuowu');
				});
			},
			// #endif
			wxpay(pushData) {
				const that = this;
				// #ifdef H5
				rechargeWechat(pushData)
					.then(e => {
						let da = e.data;
						that.payLoding = false;
						weixinObj.chooseWXPay({
							timestamp: da.timestamp,
							nonceStr: da.nonceStr,
							package: da.package,
							signType: da.signType,
							paySign: da.paySign,
							success: function(res) {
								uni.redirectTo({
									url: './paySuccess?type=1'
								});
							},
						});
					})
					.catch(e => {
						that.payLoding = false;
						console.log(e);
					});
				// #endif
				// #ifdef MP
				rechargeRoutine(pushData)
					.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: './paySuccess?type=1'
								});
							}
						})
						that.payLoding = false;
					})
					.catch(e => {
						that.payLoding = false;
						console.log(e);
					});
				// #endif
				// #ifdef APP
				wxPay(pushData).then((res) => {
					that.payLoding = true;
					uni.requestPayment({
						provider: that.type,
						orderInfo: res.data,
						success(e) {
							uni.redirectTo({
								url: './paySuccess?type=1'
							});
						},
						fail(e) {
							console.log('错误', e);
						}
					})
				}).catch(e => {
					that.payLoding = false;
					console.log(e);
				});
				// #endif
			}
		}
	};
</script>

<style lang="scss">
	.yt-list {
		background: #fff;
		margin: 0 $page-row-spacing;
		margin-top: 30rpx;
		border-radius: 20rpx;

		.yt-list-cell {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 10rpx 30rpx 10rpx 40rpx;
			line-height: 70rpx;
			position: relative;

			.checked,
			.noChecked {
				width: 36rpx;
				height: 36rpx;
			}

			.noChecked {
				border: 1px solid $font-color-light;
				border-radius: 100rpx;
			}

			&.cell-hover {
				background: #fafafa;
			}

			&.b-b:after {
				left: 30rpx;
			}

			.cell-icon {
				height: 32rpx;
				width: 32rpx;
				font-size: 22rpx;
				color: #fff;
				text-align: center;
				line-height: 32rpx;
				background: #f85e52;
				border-radius: 4rpx;
				margin-right: 12rpx;

				&.hb {
					background: #ffaa0e;
				}

				&.lpk {
					background: #3ab54a;
				}
			}

			.cell-more {
				align-self: center;
				font-size: 24rpx;
				color: $font-color-light;
				margin-left: 8rpx;
				margin-right: -10rpx;
			}

			.cell-tit {
				font-size: 26rpx;
				color: $font-color-light;
				margin-right: 10rpx;

				.orderIcon {
					width: 48rpx;
				}
			}

			.cell-tip {
				font-size: 26rpx;
				color: $font-color-dark;

				&.disabled {
					color: $font-color-light;
				}

				&.active {
					color: $base-color;
				}

				&.red {
					color: $base-color;
				}
			}

			&.desc-cell {
				.cell-tit {
					max-width: 90rpx;
				}
			}

			.desc {
				text-align: right;
				font-size: $font-base;
				color: $font-color-light;
			}
		}
	}



	page {
		height: 100%;
	}

	.content {
		padding-top: 30rpx;
	}

	.active-bg {
		background-color: $color-gray !important;
	}

	.listBox {
		margin: 30rpx;
		margin-top: 0;
		border-radius: 20rpx;
		padding: 20rpx;
		background-color: #FFFFFF;
		position: relative;
		padding-bottom: 30rpx;

		.inputBox {
			font-size: 28rpx;
			font-weight: bold;
			color: $font-color-base;
			margin: 0 20rpx;

			.text {
				flex-shrink: 0;
			}

			.input {
				border-bottom: 1px solid $font-color-disabled;
				flex-grow: 1;
				margin: 0 10rpx;
				height: 30rpx;
				text-align: center;
			}
		}
	}

	.list {
		padding-top: 10rpx;
		flex-wrap: wrap;

		.item {
			justify-content: center;
			width: 210rpx;
			height: 100rpx;
			margin-bottom: 20rpx;
			background-color: #F5F5F5;
			border-radius: 20rpx;
			font-size: 28rpx;
			font-weight: bold;
			color: $font-color-base;
			border: 1px solid #F5F5F5;

			&.action {
				border: 1px solid $color-green;
				color: $color-green;
			}
		}
	}

	.bg {
		background-color: $color-green;
		width: 950rpx;
		position: absolute;
		left: -100rpx;
		border-bottom-left-radius: 1000rpx;
		border-bottom-right-radius: 1000rpx;
		top: 0rpx;
		height: 300rpx;
	}

	.base-buttom {
		position: relative;
		left: auto;
		bottom: auto;
		right: auto;
	}
</style>
