<template>
	<view>
		<form @submit="submitSub" :style="colorStyle">
			<view class="payment-top acea-row row-column row-center-wrapper">
				<span class="name">我的佣金</span>
				<view class="pic">
					￥<span class="pic-font">{{userinfo.commissionCount || 0.00}}</span>
				</view>
			</view>
			<view class="payment">
				<view class="nav acea-row row-around row-middle">
					<view class="item" :class="active==index?'on':''" v-if="showBroker[item.key]"
						v-for="(item,index) in navRecharge" :key="index" @click="navRecharges(index)">{{item.title}}
					</view>
				</view>
				<!-- <view class='tip picList' v-if='!active'>
					<view class='input'>
						<text>￥</text>
						<input @input='inputNum' :maxlength="moneyMaxLeng" placeholder="0.00" type='digit'
							placeholder-class='placeholder' :value="number" name="number"></input>
					</view>
				</view> -->
				<!-- <view class="tip picList" v-else> -->
				<view class="tip picList">
					<view class='input'>
						<text>￥</text>
						<input @input='inputNum' :maxlength="moneyMaxLeng" placeholder="0.00" type='digit'
							placeholder-class='placeholder' :value="number" name="number"></input>
					</view>
					<view class='to_uid acea-row row-between' v-if="active==2">
						<view class="title">用户UID</view>
						<input class="uidbox" placeholder-class='placeholderUid' placeholder="请输入转入的用户uid" type='number' :value="uid" name="uid"></input>
					</view>
				</view>

				<view class="tips-title">
					<view style="font-weight: bold; font-size: 26rpx;">提示：</view>
					<view style="margin-top: 10rpx;">当前可转佣金为 <text
							class='font-color'>￥{{userinfo.commissionCount || 0}}</text>,冻结佣金为<text
							class='font-color'>￥{{userinfo.broken_commission}}</text></view>
				</view>
				<button class='but bg-color' formType="submit"> 立即转换</button>
			</view>
		</form>
		<home v-if="navigation"></home>
		<view v-show="false" v-html="formContent"></view>
		<!-- #ifdef MP -->
		<!-- <authorize v-if="isShowAuth" @authColse="authColse" @onLoadFun="onLoadFun"></authorize> -->
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		getUserInfo,
		rechargeRoutine,
		rechargeWechat,
		energyImport,
		extractBank,
		brokerageTrade
	} from '@/api/user.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		orderOfflinePayType
	} from '@/api/order.js';
	import {
		mapGetters
	} from "vuex";
	import home from '@/components/home';
	import colors from "@/mixins/color";
	import {
		openPaySubscribe
	} from '@/utils/SubscribeMessage.js';
	export default {
		components: {
			home,
		},
		mixins: [colors],
		data() {
			let that = this;
			return {
				now_money: 0,
				navRecharge: [{
					title: '佣金转能量',
					key: "brokerage_to_energy_switch",
					bool: true,
				}, {
					title: '佣金转余额',
					key: "brokerage_to_money_switch",
					bool: true,
				}, {
					title: '佣金互转',
					key: "brokerage_trade_switch",
					bool: true,
				}],
				active: 0,
				number: '',
				uid: '', //转入用户UID
				userinfo: {},
				placeholder: "0.00",
				from: '',
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				picList: [],
				activePic: 0,
				money: "",
				numberPic: '',
				rechar_id: 0,
				password: '',
				goodsList: [],
				pay_order_id: '',
				payMode: [{
						name: '微信支付',
						icon: 'icon-weixinzhifu',
						value: 'weixin',
						title: '微信快捷支付',
						payStatus: true
					}
					// #ifdef H5 ||APP-PLUS
					,
					{
						name: '支付宝支付',
						icon: 'icon-zhifubao',
						value: 'alipay',
						title: '支付宝支付',
						payStatus: true
					}
					// #endif
				],
				pay_close: false,
				payType: 'alipay',
				totalPrice: '0',
				formContent: '',
				// #ifdef H5
				isWeixin: this.$wechat.isWeixin(),
				// #endif
				type: '',
				rechargeAttention: [],
				moneyMaxLeng: 8,
				showBroker: {
					brokerage_to_energy_switch: false,
					brokerage_to_money_switch: false,
					brokerage_trade_switch: false,
				}
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUserInfo();
					}
				},
				deep: true
			}
		},
		onLoad(options) {
			// #ifdef H5
			this.from = this.$wechat.isWeixin() ? "weixinh5" : "alipay"
			// #endif
			if (this.isLogin) {
				this.getUserInfo();
			} else {
				toLogin();
			}
		},
		onShow() {
			uni.removeStorageSync('form_type_cart');
		},
		methods: {
			onLunch() {
				this.getUserInfo();
			},
			inputNum: function(e) {
				let val = e.detail.value;
				let dot = val.indexOf('.');
				if (dot > -1) {
					this.moneyMaxLeng = dot + 3;
				} else {
					this.moneyMaxLeng = 8
				}
			},
			/**
			 * 选择金额
			 */
			picCharge(idx, item) {
				this.activePic = idx;
				if (item === undefined) {
					this.rechar_id = 0;
					this.numberPic = "";
				} else {
					this.money = "";
					this.rechar_id = item.id;
					this.numberPic = item.price;
				}
			},
			onLoadFun: function() {
				this.getUserInfo();
				this.isShowAuth = false;
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			navRecharges: function(index) {
				this.active = index;
			},
			/**
			 * 获取用户信息
			 */
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => {
					that.$set(that, 'userinfo', res.data);

				})
				extractBank().then(({
					data
				}) => {
					that.showBroker.brokerage_to_energy_switch = !!(+data.brokerage_to_energy_switch);
					that.showBroker.brokerage_to_money_switch = !!(+data.brokerage_to_money_switch);
					that.showBroker.brokerage_trade_switch = !!(+data.brokerage_trade_switch);
				})
			},
			submitSub: function(e) {
				let that = this
				let value = e.detail.value.number;
				if (parseFloat(value) < 0 || parseFloat(value) == NaN || value == undefined || value == "") {
					return that.$util.Tips({
						title: '请输入转换金额'
					});
				}
				const type = that.navRecharge[that.active].key;
				// 转入余额
				if (type == "brokerage_to_money_switch") {
					uni.showModal({
						title: '转入余额',
						content: '转入余额后无法再次转出，确认是否转入余额',
						success(res) {
							if (res.confirm) {
								// #ifdef MP || APP-PLUS
								rechargeRoutine({
									price: parseFloat(value),
									type: 1
								})
								// #endif
								// #ifdef H5
								rechargeWechat({
										price: parseFloat(value),
										from: that.from,
										type: 1
									})
									// #endif
									.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 if (type == "brokerage_to_energy_switch") {
					uni.showModal({
						title: '转入能量',
						content: '转入能量后无法再次转出，确认是否转入能量',
						success(res) {
							if (res.confirm) {
								energyImport({
									price: 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: 3,
										url: '/pages/user/index'
									});
								}).catch(err => {
									return that.$util.Tips({
										title: err
									})
								});

							} else if (res.cancel) {
								return that.$util.Tips({
									title: '已取消'
								});
							}
						},
					})
				} else if (type == "brokerage_trade_switch") {
					uni.showModal({
						title: '佣金互转',
						content: '佣金转出后无法撤回，确认是否转出佣金',
						success(res) {
							if (res.confirm) {
								brokerageTrade({
									to_uid: '',
									num: 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: 3,
										url: '/pages/user/index'
									});
								}).catch(err => {
									return that.$util.Tips({
										title: err
									})
								});

							} else if (res.cancel) {
								return that.$util.Tips({
									title: '已取消'
								});
							}
						},
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		width: 100%;
		height: 100%;
		background-color: #fff;
	}

	.bgcolor {
		background-color: var(--view-theme)
	}

	.payment {
		position: relative;
		width: 100%;
		background-color: #fff;
		border-radius: 10rpx;
		padding-top: 25rpx;
		border-top-right-radius: 39rpx;
		border-top-left-radius: 39rpx;
	}

	.payment .nav {
		height: 75rpx;
		line-height: 75rpx;
		padding: 0 100rpx;
	}

	.payment .nav .item {
		font-size: 30rpx;
		color: #333;
	}

	.payment .nav .item.on {
		font-weight: bold;
		border-bottom: 4rpx solid var(--view-theme);
	}

	.payment .input {
		display: flex;
		align-items: center;
		justify-content: center;
		border-bottom: 1px dashed #dddddd;
		margin: 60rpx auto 0 auto;
		padding-bottom: 20rpx;
		font-size: 56rpx;
		color: #333333;
		flex-wrap: nowrap;

	}

	.payment .input text {
		padding-left: 106rpx;
	}

	.payment .input input {
		padding-right: 106rpx;
		width: 300rpx;
		height: 94rpx;
		text-align: center;
		font-size: 70rpx;
	}

	.payment .placeholder {
		color: #d0d0d0;
		height: 100%;
		line-height: 94rpx;
	}

	.payment .tip {
		font-size: 26rpx;
		color: #888888;
		padding: 0 30rpx;
		margin-top: 25rpx;
	}

	.payment .but {
		color: #fff;
		font-size: 30rpx;
		width: 700rpx;
		height: 86rpx;
		border-radius: 50rpx;
		margin: 46rpx auto 0 auto;
		line-height: 86rpx;
	}

	.payment-top {
		width: 100%;
		height: 350rpx;
		background-color: var(--view-theme);

		.name {
			font-size: 26rpx;
			color: rgba(255, 255, 255, 0.8);
			margin-top: -38rpx;
			margin-bottom: 30rpx;
		}

		.pic {
			font-size: 32rpx;
			color: #fff;
		}

		.pic-font {
			font-size: 78rpx;
			color: #fff;
		}
	}

	.picList {
		margin: 30rpx 0;
	}

	.tips-box {
		.tips {
			font-size: 28rpx;
			color: #333333;
			font-weight: 800;
			margin-bottom: 14rpx;
			margin-top: 20rpx;
		}

		.tips-samll {
			font-size: 24rpx;
			color: #333333;
			margin-bottom: 14rpx;
		}

		.tip-box {
			margin-top: 30rpx;
		}
	}

	.tips-title {
		padding-left: 30rpx;
		margin-top: 20rpx;
		font-size: 24rpx;
		color: #333;
	}

	.to_uid {
		padding: 20rpx;
		width: 100%;
		line-height: 1;
		font-size: 28rpx;
		align-items: center;

			border-bottom:1px solid #dddddd;
		.title {
			font-weight: bold;
		}
		.uidbox{
			flex-grow: 1;
			text-align: right;
		}
		.placeholderUid{
			text-align: right;
		}
	}
</style>