<template>
	<view class="withdraw p30">
		<view style="height: 30rpx"></view>
		<view style="background-color: #ffffff; padding: 30rpx; border-radius: 10rpx; font-size: 30rpx; color: #949494">
			<view>当前可提现余额： ￥{{ userInfo.nowMoney }}</view>
		</view>
		<view style="height: 30rpx"></view>
		<view class="withdraw_k" @click="toNav">
			<view class="withdraw_k_tit">提现到</view>
			<view class="withdraw_k_input">请选择银行卡</view>
			<!-- <image class="withdraw_k_img" src="/static/icon/xia.png"></image> -->
		</view>
		<view v-if="bank">
			<view style="background-color: #ffffff; border-radius: 12rpx; padding: 20rpx; margin-bottom: 20rpx">
				<view style="font-size: 28rpx; display: flex; align-items: center">
					<view>
						<u-tag :text="bank.type == 1 ? '个人' : '企业'" size="mini" :type="bank.type == 1 ? 'success' : 'error'" plain plainFill></u-tag>
					</view>
					<view style="width: 30rpx"></view>
					<view style="font-size: 26rpx">{{ bank.name }}</view>
					<view style="width: 30rpx"></view>
					<view style="font-size: 26rpx">{{ bank.phone || '' }}</view>
				</view>
				<view style="height: 20rpx"></view>
				<view style="color: #232323">
					<!-- 					<view style="font-size: 28rpx">开户行：{{ bank.bankName }}</view>
					<view style="height: 10rpx"></view> -->
					<view style="font-size: 28rpx" v-if="bank.type == 2">开户行：{{ bank.subbranchName }}</view>
					<view style="height: 10rpx"></view>
					<view style="font-size: 28rpx">银行卡号：{{ bank.bankCard }}</view>
					<view style="height: 10rpx"></view>
					<!-- 					<view style="font-size: 28rpx" v-if="bank.type == 2">纳税人识别号：{{ bank.taxNo ? maskCardNumber(bank.taxNo) : '' }}</view>
					<view style="height: 10rpx"></view> -->
				</view>
			</view>
		</view>

		<view class="withdraw_mon">
			<view class="withdraw_mon_tit">提现金额</view>
			<view class="withdraw_mon_input">
				<view class="withdraw_mon_input_fu">￥</view>
				<view style="flex: 1">
					<u-input fontSize="36rpx" placeholder="请输入金额" :border="false" v-model="dataFrom.amount"></u-input>
				</view>
				<!-- <view class="withdraw_mon_input_note" @click="dataFrom.points = getInfo.points1">全部提现</view> -->
			</view>

			<view>
				<u-input :border="false" disabled v-model="userInfo.phone"></u-input>
			</view>
			<view class="withdraw_mon_input">
				<u-input placeholder="请输入验证码" :border="false" v-model="dataFrom.smsCode"></u-input>
				<view style="width: 250rpx">
					<u-code ref="uCode" @change="codeChange2" keep-running start-text="获取验证码"></u-code>
					<u-button @click="getCode" color="#E93323">{{ tips }}</u-button>
				</view>
			</view>
			<!-- 			<view class="withdraw_mon_note">可提现金额：￥{{ getInfo.points1 }}</view>
			<view class="withdraw_mon_note">最小可提现金额为￥{{ appConfig.minMoney }}</view>
			<view class="withdraw_mon_note">私户每100.00元提现手续费为{{ appConfig.free }}%</view> -->
		</view>

		<view class="withdraw_note">
			<view class="withdraw_note_tit" style="font-weight: bold; font-size: 26rpx">提现规则：</view>
			<view class="withdraw_note_s" v-for="(item, index) in infoDateRlue" :key="index">{{ item }}</view>
		</view>

		<view style="height: 50rpx"></view>
		<view style="display: flex; justify-content: center; margin-top: 80rpx">
			<view class="affirm_btn" @click="payOrder">立即提现</view>
		</view>
		<view style="height: 50rpx"></view>
	</view>
</template>

<script>
import { registerVerify, apply, extractRule } from '@/api/user';
import { mapGetters } from 'vuex';
export default {
	data() {
		return {
			value: '',
			getInfo: {},
			tips: '',
			dataFrom: {
				amount: '',
				smsCode: '',
				bankCardId: ''
			},
			bank: null,
			infoDateRlue: []
		};
	},
	computed: mapGetters(['userInfo']),
	onLoad() {
		uni.$on('bank', (e) => {
			console.log(e);
			this.bank = e;
			this.dataFrom.bankCardId = e.id;
		});

		extractRule().then((res) => {
			this.infoDateRlue = res.data.ruleInfo;
			console.log(res);
		});
	},
	methods: {
		toNav(e) {
			uni.navigateTo({
				url: '/pages/users/bank/cradList?type=xz'
			});
		},
		codeChange2(text) {
			this.tips = text;
		},
		getCode() {
			if (!this.userInfo.phone) return uni.showToast({ title: '请输入手机号', icon: 'none' });
			if (this.$refs.uCode.canGetCode) {
				// 模拟向后端请求验证码
				uni.showLoading({
					title: '正在获取验证码'
				});
				registerVerify(this.userInfo.phone).then((res) => {
					uni.hideLoading();
					uni.$u.toast('验证码已发送');
					this.$refs.uCode.start();
				});
			} else {
				uni.$u.toast('倒计时结束后再发送');
			}
		},
		maskCardNumber(cardNumber) {
			if (!cardNumber || typeof cardNumber !== 'string') {
				throw new Error('Invalid card number');
			}
			const sanitizedNumber = cardNumber.replace(/\D/g, '');
			if (sanitizedNumber.length < 4) {
				throw new Error('Card number is too short');
			}
			const lastFourDigits = sanitizedNumber.slice(-4);
			const maskedNumber = '*'.repeat(sanitizedNumber.length - 4) + lastFourDigits;
			return maskedNumber;
		},
		async payOrder() {
			console.log(this.dataFrom);
			if (!this.dataFrom.bankCardId) return uni.showToast({ title: '请先选择提现银行卡', icon: 'none' });
			if (!this.dataFrom.amount) return uni.showToast({ title: '请输入提现金额', icon: 'none' });
			if (!this.dataFrom.smsCode) return uni.showToast({ title: '请输入验证码', icon: 'none' });
			let _res = await apply(this.dataFrom);
			uni.showModal({
				title: '提示',
				content: '提交成功',
				showCancel: false,
				success() {
					uni.navigateBack();
				}
			});
		}
	}
};
</script>

<style scoped lang="scss">
.withdraw {
	&_k {
		width: 100%;
		height: 74rpx;
		padding: 0 20rpx;
		background: #ffffff;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 30rpx;
		&_tit {
			font-weight: bold;
			font-size: 24rpx;
			color: #232323;
			margin-right: 100rpx;
		}
		&_input {
			flex: 1;
			text-align: right;
			font-size: 28rpx;
		}
		&_img {
			width: 44rpx;
			height: 44rpx;
		}
	}
	&_mon {
		padding: 20rpx;
		background: #ffffff;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		&_tit {
			font-weight: bold;
			font-size: 24rpx;
			color: #232323;
		}
		&_input {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #cccccc;
			padding: 15rpx 0;
			margin-bottom: 20rpx;
			&_fu {
				font-weight: bold;
				font-size: 66rpx;
				color: #232323;
			}
			&_note {
				font-weight: bold;
				font-size: 24rpx;
				color: #ff7777;
			}
		}

		&_note {
			font-weight: 400;
			font-size: 24rpx;
			color: #232323;
			margin-bottom: 10rpx;
		}
	}
	&_note {
		margin-top: 30rpx;
		padding: 0 15rpx;
		view {
			margin-bottom: 15rpx;
			font-weight: 400;
			font-size: 26rpx;
			color: #232323;
			line-height: 40rpx;
		}
	}
}

.affirm_btn {
	width: 650rpx;
	height: 78rpx;
	background: #e93323;
	border-radius: 12rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	font-weight: bold;
	font-size: 24rpx;
	color: #ffffff;
}
</style>
