<template>
	<view :style="colorStyle">
		<view class='cash-withdrawal'>
			<view class='wrapper'>
				<view class='list'>
					<form @submit="subCash">
						<view class='item acea-row row-between-wrapper'>
							<view class='name'>持卡人</view>
							<view class='input'><input placeholder='请输入持卡人姓名' placeholder-class='placeholder'
									name="name"
									onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;"></input>
							</view>
						</view>
						<view class='item acea-row row-between-wrapper'>
							<view class='name'>卡号</view>
							<view class='input'><input type='number' placeholder='请填写卡号' placeholder-class='placeholder'
									name="cardnum"></input></view>
						</view>
						<view class='item acea-row row-between-wrapper'>
							<view class='name'>开户行</view>
							<view class='input'><input placeholder='请输入开户行名称' placeholder-class='placeholder'
									name="bankname"
									onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;"></input>
							</view>
						</view>
						<view class='item acea-row row-between-wrapper'>
							<view class='name'>交易账号</view>
							<view class='input'><input placeholder='请输入交易账号' placeholder-class='placeholder'
									name="account"></input>
							</view>
						</view>
						<view class='item acea-row row-between-wrapper'>
							<view class='name'>转换能量</view>
							<view class='input'><input v-model="allMoney" @input='inputNum' :maxlength="moneyMaxLeng"
									:placeholder='"最低转换能量值："+minPrice' placeholder-class='placeholder' name="energy"
									type='digit'></input></view>
						</view>
						<view class='tip'>
							当前可转能量值：<text class="price">{{userInfo.energy}}</text>
							时价：<text class="price">{{stock_price}}</text>
						</view>
						<view class='tip'>
							手续费：<text class="price">{{withdraw_fee}}%</text>实际到账：<text
								class="price">{{true_money}}</text>
						</view>
						<button formType="submit" class='bnt bg-color'>立即转换</button>
					</form>
				</view>
			</view>
		</view>
		<home v-if="navigation"></home>
	</view>
</template>

<script>
	import {
		energyExchange,
		energyBank,
		getUserInfo
	} from '@/api/user.js';
	import {
		toLogin
	} from '@/libs/login.js';

	import {
		calculator,
	} from '@/api/new.js';

	import {
		mapGetters
	} from "vuex";
	import colors from '@/mixins/color.js';
	import home from '@/components/home';
	export default {
		components: {
			home
		},
		mixins: [colors],
		data() {
			return {
				minPrice: 0.00, //最低提现金额
				userInfo: [],
				prevent: true, //避免重复提交成功多次
				moneyMaxLeng: 8,
				withdraw_fee: '0',
				true_money: 0,
				allMoney: '', //保存当前提现金额
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUserInfo();
						this.getUserExtractBank();
					}
				},
				deep: true
			}
		},
		onLoad() {
			if (this.isLogin) {
				this.getUserInfo();
				this.getUserExtractBank();
			} else {
				toLogin()
			}
		},
		methods: {
			inputNum: async function(e) {
				let val = e.detail.value;
				let dot = val.indexOf('.');
				if (dot > -1) {
					this.moneyMaxLeng = dot + 3;
				} else {
					this.moneyMaxLeng = 8
				}
				try {
					this.true_money = +((val - val * this.withdraw_fee / 100) / this.stock_price).toFixed(2);
					console.log(this.true_money, 'this.true_money');
				} catch (e) {
					uni.showToast({
						title: e,
						icon: "error"
					});
					//TODO handle the exception
				}

			},
			getUserExtractBank: function() {
				let that = this;
				energyBank().then(res => {
					that.minPrice = res.data.minEnergy;
					that.stock_price = res.data.stock_price;
					that.withdraw_fee = res.data.exchange_fee;
				});
			},
			/**
			 * 获取个人用户信息
			 */
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => {
					that.userInfo = res.data;
				})
			},
			subCash: function(e) {
				let that = this,
					value = e.detail.value;
					console.log(value,'value')
				if (value.account.length == 0) return that.$util.Tips({
					title: '请填写交易账户'
				});
				if (value.name.length == 0) return that.$util.Tips({
					title: '请填写持卡人姓名'
				});
				if (value.cardnum.length == 0) return that.$util.Tips({
					title: '请填写卡号'
				});
				if (value.energy.length == 0) return that.$util.Tips({
					title: '请填写转换能量'
				});
				if (value.bankname.length == 0) return that.$util.Tips({
					title: '请填写开户行名称'
				});
				if (Number(value.energy) < Number(that.minPrice)) return that.$util.Tips({
					title: '转换能量不能低于：' + that.minPrice
				});
				if (that.prevent) {
					that.prevent = false
				} else {
					return
				}
				energyExchange({
					...value,
				}).then(res => {
					return this.$util.Tips({
						title: res.msg,
						icon: 'success'
					}, {
						url: '/pages/users/user_spread_user/index',
						tab: 2
					});
				}).catch(err => {
					setTimeout(e => {
						this.prevent = true
					}, 1500)
					return this.$util.Tips({
						title: err
					});
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff !important;
	}

	.fontcolor {
		color: var(--view-theme) !important;
	}

	.cash-withdrawal .nav {
		height: 130rpx;
		box-shadow: 0 10rpx 10rpx #f8f8f8;
	}

	.cash-withdrawal .nav .item {
		font-size: 26rpx;
		flex: 1;
		text-align: center;
	}

	.cash-withdrawal .nav .item~.item {
		border-left: 1px solid #f0f0f0;
	}

	.cash-withdrawal .nav .item .iconfont {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		border: 2rpx solid var(--view-theme);
		text-align: center;
		line-height: 37rpx;
		margin: 0 auto 6rpx auto;
		font-size: 22rpx;
		box-sizing: border-box;
	}

	.cash-withdrawal .nav .item .iconfont.on {
		background-color: var(--view-theme);
		color: #fff;
		border-color: var(--view-theme);
	}

	.cash-withdrawal .nav .item .line {
		width: 2rpx;
		height: 20rpx;
		margin: 0 auto;
		transition: height 0.3s;
	}

	.cash-withdrawal .nav .item .line.on {
		height: 39rpx;
	}

	.cash-withdrawal .wrapper .list {
		padding: 0 30rpx;
	}

	.cash-withdrawal .wrapper .list .item {
		border-bottom: 1rpx solid #eee;
		min-height: 28rpx;
		font-size: 30rpx;
		color: #333;
		padding: 39rpx 0;
	}

	.cash-withdrawal .wrapper .list .item .name {
		width: 130rpx;
	}

	.cash-withdrawal .wrapper .list .item .input {
		width: 505rpx;
	}

	.cash-withdrawal .wrapper .list .item .input .placeholder {
		color: #bbb;
	}

	.cash-withdrawal .wrapper .list .item .picEwm,
	.cash-withdrawal .wrapper .list .item .pictrue {
		width: 140rpx;
		height: 140rpx;
		border-radius: 3rpx;
		position: relative;
		margin-right: 23rpx;
	}

	.cash-withdrawal .wrapper .list .item .picEwm image {
		width: 100%;
		height: 100%;
		border-radius: 3rpx;
	}

	.cash-withdrawal .wrapper .list .item .picEwm .icon-guanbi1 {
		position: absolute;
		right: -14rpx;
		top: -16rpx;
		font-size: 40rpx;
	}

	.cash-withdrawal .wrapper .list .item .pictrue {
		border: 1px solid rgba(221, 221, 221, 1);
		font-size: 22rpx;
		color: #BBBBBB;
	}

	.cash-withdrawal .wrapper .list .item .pictrue .icon-icon25201 {
		font-size: 47rpx;
		color: #DDDDDD;
		margin-bottom: 3px;
	}

	.cash-withdrawal .wrapper .list .tip {
		font-size: 26rpx;
		color: #999;
		margin-top: 25rpx;
	}

	.cash-withdrawal .wrapper .list .bnt {
		font-size: 32rpx;
		color: #fff;
		width: 690rpx;
		height: 90rpx;
		text-align: center;
		border-radius: 50rpx;
		line-height: 90rpx;
		margin: 64rpx auto;
	}

	.cash-withdrawal .wrapper .list .tip2 {
		font-size: 26rpx;
		color: #999;
		text-align: center;
		margin: 44rpx 0 20rpx 0;
	}

	.cash-withdrawal .wrapper .list .value {
		height: 135rpx;
		line-height: 135rpx;
		border-bottom: 1rpx solid #eee;
		width: 690rpx;
		margin: 0 auto;
	}

	.cash-withdrawal .wrapper .list .value input {
		font-size: 80rpx;
		color: #282828;
		height: 135rpx;
		text-align: center;
	}

	.cash-withdrawal .wrapper .list .value .placeholder2 {
		color: #bbb;
	}

	.price {
		color: var(--view-priceColor);
		margin-right: 20rpx;
	}
</style>