<template>
	<view class="content">
		<view class="content-money">
			<view class="buttom">
				<text class="text">可提现{{money_type}}</text>
				<view class="icon">
					{{ money }}
				</view>
			</view>
			<view class="interval"></view>
			<view class="buttom">
				<text class="text">申请提现{{money_type}}</text>
				<view class=" icon"><input class="input" type="number" v-model="withdrawal" placeholder="请输入提现金额" placeholder-class="placeholder" /></view>
			</view>
			<view class="interval"></view>
			<view class="buttom">
				<text class="text">支付密码</text>
				<view class=" icon"><input class="input" type="password" v-model="pwd" placeholder="请输入支付密码" placeholder-class="placeholder" /></view>
			</view>
			<view class="interval"></view>
			<view class="buttom" v-if="type == 'address'">
				<text class="text">提现地址</text>
				<view class=" icon"><input class="input" type="text" v-model="address" placeholder="请输入提现地址" placeholder-class="placeholder" /></view>
			</view>
			<view class="interval"></view>
			<view class="tip">
				<view class="tip-text" v-show="showText == false"></view>
				<view class="tip-text" v-show="showText == true">提先数量最低为{{ minPrice }},手续费为{{ data.service*1 }}{{ data._money_type }}</view>
				<view class=" tip-icon" @click="all()">全部提现</view>
			</view>
		</view>

		<!-- #ifndef MP-WEIXIN -->
		<view class="list">
			<radio-group @change="tabRadio">
				<!-- <label>
					<view class="box">
						<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> -->
				<label>
					<view class="box">
						<view class="icon iconfont"><image class="icon-img" src="../../static/img/qianbao.png" mode=""></image></view>
						<view class="title-box">
							<view class="title">
								<text>提现至钱包</text>
							</view>
						</view>
						<view class="right"><radio value="address" color="#5dbc7c" :checked="type == 'address'" /></view>
					</view>
				</label>
				<label>
					<view class="box">
						<view class="icon iconfont iconzhifubao"></view>
						<view class="title-box">
							<view class="title">
								<text v-if="aliData.fullname != null">提现至支付宝</text>
								<text v-else>请创建支付宝账号</text>
							</view>
							<view class="node">
								<text v-if="aliData.fullname != null">真实姓名({{ aliData.fullname }})</text>
							</view>
						</view>
						<view class="right"><radio value="alipay" color="#5dbc7c" :checked="type == 'alipay'" /></view>
					</view>
				</label>
				<label>
					<view class="box">
						<view class="icon iconfont"><image class="icon-img" src="/static/icon/i8.png" mode="aspectFit"></image></view>
						<view class="title-box">
							<view class="title">
								<text v-if="bankData.bankno != null">{{ bankData.bank + ' ' + bankData.bankno }}</text>
								<text v-else>请创建银行账号</text>
							</view>
							<view class="node">
								<text v-if="bankData.fullname != null">真实姓名({{ bankData.fullname }})</text>
							</view>
						</view>
						<view class="right"><radio value="bank" color="#5dbc7c" :checked="type == 'bank'" /></view>
					</view>
				</label>
			</radio-group>
		</view>
		<!-- #endif -->
		<button class="add-btn up" @click="confirm">提交申请</button>
		<button class="add-btn modified" @click="navTo('/pages/collection/collection')">账号管理</button>
	</view>
</template>

<script>
import { cashmoney_type, calculator, wallet, cash } from '@/api/finance.js';
import { extractCash } from '@/api/wallet.js';
import { mapState, mapMutations } from 'vuex';
export default {
	data() {
		return {
			type: 'alipay', //提现方式
			moneyType:'',
			money: '', //可提现金额
			money_type:'',//提现单位
			withdrawal: '', //提现金额
			minPrice: '', //最少提现金额
			coldMoney: '', //冻结中的金额
			pwd:'',//交易密码
			data:'',
			address:'',//提币地址
			showText: false,
			aliData: { fullname: '', alino: '' },
			bankData: { fullname: '', bank: '', bankno: '' },
			// #ifdef H5
			weichatBsrowser: false
			// #endif
		};
	},
	computed: {
		...mapState('user', ['userInfo', 'orderInfo', 'hasLogin'])
	},
	onLoad(option) {
		this.moneyType = option.type;
	},
	onShow() {
		// #ifdef H5
		this.weichatBsrowser = uni.getStorageSync('weichatBrowser');
		// #endif
		//加载提现信息
		this.loadData();
		// 加载提款账号信息
		this.aliData.fullname = this.userInfo.alipay_name;
		console.log(this.aliData.fullname);
		this.aliData.alino = this.userInfo.alipay_code;
		this.bankData.fullname = this.userInfo.bank_user_name;
		this.bankData.bank = this.userInfo.bank_name;
		this.bankData.bankno = this.userInfo.bank_code;
	},
	watch: {
		withdrawal(newVal, oldVal) {
			this.calculator();
		}
	},
	methods: {
		// 加载余额信息
		async loadData() {
			wallet({}).then(({ data }) => {
				const obj = this
				const arr = Object.keys(data.back);
				console.log(arr);
				let ar = [];
				arr.forEach(e => {
					console.log(e,'11111')
					if(e == obj.moneyType){
						obj.money = data.back[e].money.money*1;
						obj.minPrice = data.back[e].less;
						obj.money_type = data.back[e].name;
					}
				});
			});
		},
		// 跳转
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		},
		all() {
			this.withdrawal = this.money;
		},
		// 切换选中对象
		tabRadio(e) {
			this.type = e.detail.value;
		},
		calculator() {
			let obj = this;
			calculator({
				money_type: obj.moneyType,
				money: obj.withdrawal
			}).then(({ data }) => {
				console.log(data);
				obj.data = data;
				obj.showText = true;
			});
		},
		// 提交
		confirm() {
			const obj = this
			console.log(this.withdrawal);
			if (this.withdrawal === '') {
				this.$api.msg('请填写需要提现的金额');
				return;
			}
			if (this.withdrawal == 0) {
				this.$api.msg('提现金额不能为0');
				return;
			}
			if (this.withdrawal < this.minPrice) {
				this.$api.msg('提现金额不能低于最小金额');
				return;
			}
			if(this.type == 'address'){
				cash({
					money_type: obj.moneyType,
					money: obj.withdrawal,
					address: obj.address,
					password: obj.password
				}).then(data => {
					uni.navigateTo({
						url: '/pages/money/success?type=1'
					});
				});
			}else {
				let data = {
					extract_type: this.type, //bank -银行卡 alipay-支付宝 weixin-微信 地址address
					password: this.pwd,
					money: this.withdrawal, //金额
					money_type: this.moneyType
				};
				if (this.type == 'alipay') {
					data.name = this.aliData.fullname;
					data.alipay_code = this.aliData.alino;
				}
				if (this.type == 'bank') {
					data.name = this.bankData.fullname;
					data.bankname = this.bankData.bank;
					data.cardnum = this.bankData.bankno;
				}
				
				extractCash(data)
					.then(e => {
						uni.navigateTo({
							url: '/pages/money/success?type=1'
						});
						// uni.showToast({
						// 	title: '提交成功',
						// 	duration: 2000,
						// 	position: 'top'
						// });
					})
					.catch(e => {
						console.log();
					});
			}
		}
	}
};
</script>

<style lang="scss">
page {
	height: 100%;
}
.content-money {
	padding: 0 20rpx;
	background-color: #ffffff;
	.buttom {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 110rpx;
	}
	.interval {
		width: 100%;
		height: 1px;
		background: #e6e6e6;
	}
	.icon {
		font-size: 48rpx;
		font-family: SourceHanSansCN;
		font-weight: 500;
		color: #333333;
		
		.input {
			text-align: right;
			flex: 1;
			font-size: 30rpx;
			color: $font-color-dark;
		}
		.iconlocation {
			text-align: right;
			font-size: 36rpx;
			color: $font-color-light;
		}
	}
	.text {
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
	}
	.tip {
		height: 74rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.tip-text {
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
		}
		.tip-icon {
			font-size: 26rpx;
			font-family: SourceHanSansCN;
			font-weight: 400;
			color: #438bed;
		}
	}
}

.add-btn {
	&.modified {
		color: $base-color;
	}
	&.up {
		background-color: $base-color;
		color: #fff;
	}
	display: flex;
	align-items: center;
	justify-content: center;
	width: 690rpx;
	height: 80rpx;
	margin: 0 auto;
	margin-top: 30rpx;
	font-size: $font-lg;
	border-radius: 10rpx;
	// box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
}

.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;
		.icon {
			font-size: 48rpx;
			padding-right: 20rpx;
			.icon-img {
				height: 50rpx;
				width: 50rpx;
			}
		}
		.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;
}
</style>
