<template>
	<view>
		<lf-nav title="提现" :showIcon="true" bgColor="#fff"></lf-nav>
		<view class="tabs">
			<view class="tab" v-for="(item, index) in tab_list" :key="index">
				<view class="name" 
					:class="{'active-tab': current == index}" 
					@click="switchTab(index)">{{ item }}
				</view>
			</view>
		</view>
		<view class="content">
			<!-- 店铺信息 -->
			<view class="card">
				<view class="lf-flex">
					<image class="shop-avatar" mode="aspectFill" :src="brand.logo"></image>
					<view class="shop-name">{{ brand.name }}</view>
				</view>
			</view>
			<!-- 收款信息 - 提现到支付宝 -->
			<view class="card" v-if="current == 0">
				<view class="lf-font-28 lf-color-222">支付宝姓名</view>
				<view class="list">
					<view class="lf-flex">
						<input class="input" placeholder="请输入您的姓名" v-model="alipay_name" />
					</view>
					<view class="clear" v-if="alipay_name.length" @click="alipay_name = ''">
						<text class="lf-iconfont icon-cuo1"></text>
					</view>
				</view>
				<view class="lf-font-28 lf-color-222 lf-m-t-50">支付宝账号</view>
				<view class="list">
					<view class="lf-flex">
						<input class="input" placeholder="请输入支付宝账号" v-model="alipay_account" />
					</view>
					<view class="clear" v-if="alipay_account.length" @click="alipay_account = ''">
						<text class="lf-iconfont icon-cuo1"></text>
					</view>
				</view>
			</view>
			<!-- 收款信息 - 提现到银行卡 -->
			<view class="card" v-else-if="current == 1">
				<view class="lf-font-28 lf-color-222">银行名称</view>
				<view class="list">
					<view class="lf-flex">
						<input class="input" placeholder="请输入您的银行名称" v-model="bank_name" />
					</view>
					<view class="clear" v-if="bank_name.length" @click="bank_name = ''">
						<text class="lf-iconfont icon-cuo1"></text>
					</view>
				</view>
				<view class="lf-font-28 lf-color-222 lf-m-t-50">所属支行</view>
				<view class="list">
					<view class="lf-flex">
						<input class="input" placeholder="请输入银行卡所属支行" v-model="bank_sub_branch" />
					</view>
					<view class="clear" v-if="bank_sub_branch.length" @click="bank_sub_branch = ''">
						<text class="lf-iconfont icon-cuo1"></text>
					</view>
				</view>
				<view class="lf-font-28 lf-color-222 lf-m-t-50">银行卡号</view>
				<view class="list">
					<view class="lf-flex">
						<input class="input" placeholder="请输入银行卡号码" v-model="bank_number" />
					</view>
					<view class="clear" v-if="bank_number.length" @click="bank_number = ''">
						<text class="lf-iconfont icon-cuo1"></text>
					</view>
				</view>
				<view class="lf-font-28 lf-color-222 lf-m-t-50">开户名</view>
				<view class="list">
					<view class="lf-flex">
						<input class="input" placeholder="请输入银行卡开户名" v-model="bank_account_name" />
					</view>
					<view class="clear" v-if="bank_account_name.length" @click="bank_account_name = ''">
						<text class="lf-iconfont icon-cuo1"></text>
					</view>
				</view>
			</view>
			<!-- 提现金额 -->
			<view class="card">
				<view class="lf-font-28 lf-color-222">
					<text class="lf-m-r-10">提现金额</text>
					<text class="lf-font-24">(当前可用余额{{ balance }}元)</text>
				</view>
				<view class="list money-list">
					<view class="lf-flex">
						<view class="symbol">￥</view>
						<input class="input" type="number" v-model="money" />
					</view>
					<view class="clear" v-if="money.length" @click="money = ''">
						<text class="lf-iconfont icon-cuo1"></text>
					</view>
				</view>
				<view class="lf-font-28 lf-m-t-30" style="color: #F63434;" v-if="isMoney">输入金额超过可用余额</view>
			</view>
			<!-- 提现说明 -->
			<view class="card" v-if="remark.length">
				<view class="lf-font-28 lf-color-555 lf-m-b-20">提现说明</view>
				<view class="lf-font-28 lf-color-222">
					<view v-for="(item, index) in remark" :key="index">{{ item }}</view>
				</view>
			</view>
			<button class="confirm" hover-class="lf-opacity" @click="confirm">确认提现</button>
		</view>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				tab_list: ['支付宝', '银行卡'],
				current: 0, // 支付宝0、银行卡1
				alipay_name: '', // 支付宝姓名
				alipay_account: '', // 支付宝账号
				bank_name: '', // 银行名称
				bank_sub_branch: '', // 银行卡所属支行
				bank_number: '', // 银行卡号
				bank_account_name: '', // 银行卡开户名
				money: '', // 提现金额
				token: '',
				remark: [], // 提现说明
				brand: {}, // 商家信息
				balance: 0 // 当前可用余额
			}
		},
		computed: {
			isMoney(){
				return Number(this.money) > this.balance;
			}
		},
		onLoad(){
			let token = this.$cookieStorage.get('store_token');
			this.token = token;
			this.getApplyInfo();
		},
		methods: {
			// 提现申请信息获取
			getApplyInfo(){
				this.$http.get({
					api: 'api/supplier/apply_info',
					header: {
						token: this.token
					}
				}).then(res => {
					this.balance = res.data.data.balance;
					this.brand = res.data.data.brand;
					this.remark = res.data.data.remark;
				})
			},
			// 切换tab
			switchTab(index){
				this.current = index;
			},
			confirm(){
				if(this.current == 0){
					// 提现到支付宝
					if(!this.alipay_name) return this.$msg('请输入支付宝姓名');
					if(!this.alipay_account) return this.$msg('请输入支付宝账号');
					if(!this.money) return this.$msg('请输入提现金额');
					if(this.isMoney) return this.$msg('提现金额超出当前余额');
					if(this.money <= 0) return this.$msg('提现金额需大于0');
					this.launch('alipay');
				}else if(this.current == 1){
					// 提现到银行卡
					if(!this.bank_name) return this.$msg('请输入银行名称');
					if(!this.bank_sub_branch) return this.$msg('请输入银行卡所属支行');
					if(!this.bank_number) return this.$msg('请输入银行卡号');
					if(!this.bank_account_name) return this.$msg('请输入银行卡开户名');
					if(!this.money) return this.$msg('请输入提现金额');
					if(this.isMoney) return this.$msg('提现金额超出当前余额');
					if(this.money <= 0) return this.$msg('提现金额需大于0');
					this.launch('bankcard');
				}
			},
			launch(type_name){
				uni.showLoading({
					title: '正在提现中'
				})
				let par = {
					type: type_name,
					value: this.money
				};
				if(type_name == 'bankcard'){
					par.bank_name = this.bank_name;
					par.bank_branch = this.bank_sub_branch;
					par.bank_number = this.bank_number;
					par.bank_username = this.bank_account_name;
				}else if(type_name == 'alipay'){
					par.alipay_name = this.alipay_name;
					par.alipay_account = this.alipay_account;
				}
				
				this.$http.post({
					api: 'api/supplier/cash_apply',
					data: par,
					header: {
						token: this.token
					}
				}).then(res => {
					if(res.data.code == 200){
						if(type_name == 'bankcard'){
							this.bank_name = '';
							this.bank_sub_branch = '';
							this.bank_number = '';
							this.bank_account_name = '';
						}else if(type_name == 'alipay'){
							this.alipay_name = '';
							this.alipay_account = '';
						}
						this.money = '';
						this.$msg('提现成功', {icon: 'success'});
						this.getApplyInfo();
					}
					uni.hideLoading();
				}).catch(err => uni.hideLoading());
			}
		}
	}
</script>

<style>
	page{
		background-color: #F8F8F8;
	}
</style>
<style lang="scss" scoped="scoped">
	.tabs{
		height: 80rpx;
		width: 750rpx;
		display: flex;
		border-bottom: 1rpx solid #e5e5e5;
		background-color: #FFFFFF;
		align-items: center;
		.tab{
			width: 50%;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			.name{
				width: max-content;
				height: 100%;
				font-size: 28rpx;
				color: #777777;
				display: flex;
				align-items: center;
			}
			.active-tab{
				color: #0D2E9A;
				font-weight: bold;
				position: relative;
				&::after{
					content: '';
					position: absolute;
					bottom: 0;
					left: calc(50% - 40rpx);
					width: 80rpx;
					height: 10rpx;
					background: #0D2E9A;
					border-radius: 8rpx 8rpx 0rpx 0rpx;
				}
			}
		}
	}
	
	.content{
		padding: 30rpx 32rpx;
	}
	.card{
		padding: 30rpx;
		box-sizing: border-box;
		width: 686rpx;
		height: max-content;
		background: #FFFFFF;
		border-radius: 20rpx;
		transition: all 1s;
		&:nth-child(n+2){
			margin-top: 30rpx;
		}
		.shop-avatar{
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			margin-right: 20rpx;
		}
		.shop-name{
			width: 502rpx;
			height: max-content;
			font-size: 36rpx;
			color: #222222;
			font-weight: bold;
		}
		.list{
			height: 90rpx;
			width: 100%;
			border-bottom: 1rpx solid #e5e5e5;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 10rpx;
			.input{
				width: 540rpx;
				height: 80rpx;
				font-size: 28rpx;
			}
			.input-code{
				width: 430rpx;
			}
			.clear{
				padding: 20rpx;
			}
			.code{
				min-width: 180rpx;
				max-width: 220rpx;
				height: 64rpx;
				padding: 0 4rpx;
				font-size: 24rpx;
				color: #15716E;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 32rpx;
				border: 2rpx solid #15716E;
			}
			.active-bg{
				background: #efefef;
			}
			.symbol{
				width: 30rpx;
				height: 90rpx;
				font-size: 36rpx;
				color: #222222;
				display: flex;
				align-items: flex-end;
			}
		}
		.money-list{
			height: 140rpx;
			.input{
				width: 500rpx;
				height: 130rpx;
				font-size: 72rpx;
				font-weight: bold;
				margin-left: 20rpx;
			}
		}
	}
	
	.confirm{
		width: 550rpx;
		height: 100rpx;
		background: #0D2E9A;
		border-radius: 50rpx;
		color: #FFFFFF;
		line-height: 100rpx;
		margin-top: 50rpx;
		font-size: 32rpx;
	}
	
	
</style>
