<template>
	<view class="withdraw p30">
		<!-- #ifdef MP-ALIPAY -->
		<view style="height: 30rpx;"></view>
		<!-- #endif -->
		<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 class="u-flex u-flex-y-center" style="font-size: 28rpx">
					<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>
					<up-input fontSize="36rpx" placeholder="请输入金额" :border="false" v-model="dataFrom.amount"></up-input>
				</view>
				<view class="withdraw_mon_input_note" @click="dataFrom.amount = getInfo.balance">全部提现</view>
			</view>

			<view>
				<up-input :border="false" disabled v-model="getInfo.phone"></up-input>
			</view>
			<view class="withdraw_mon_input">
				<up-input placeholder="请输入验证码" :border="false" v-model="dataFrom.smsCode"></up-input>
				<view style="width: 250rpx">
					<up-code ref="uCode" @change="codeChange2" keep-running start-text="点我获取验证码"></up-code>
					<up-button @click="getCode" color="#4874e5">{{ tips }}</up-button>
				</view>
			</view>
			<view class="withdraw_mon_note">可提现金额：￥{{ getInfo.balance }}</view>
		</view>

<!-- 		<view class="withdraw_note">
			<view class="withdraw_note_tit" style="font-weight: bold; font-size: 26rpx">提现规则：</view>
			<view class="withdraw_note_s">1.每月1号、11号、21号可提现，到账时间T+1</view>
			<view class="withdraw_note_s">2.提现年龄是18-65岁</view>
			<view class="withdraw_note_s">3.提现银行卡是常用的一类银行，否则会有限额</view>
			<view class="withdraw_note_s">4.一月不能超{{ appConfig.monthQuota }}元，如遇超出 提现后管理人员会联系您</view>
			<view class="withdraw_note_s">5.提现次数不限、公户无限额、无手续费</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 setup>
import { ref, reactive } from 'vue';
import { operatorInfo, getAppConfig, smsCode, userExtract } from '@/api/api.js';
import { onLoad, onShow } from '@dcloudio/uni-app';
let value = ref();
let getInfo = ref({});
let tips = ref('');
let uCode = ref(null);
let dataFrom = reactive({
	amount: '',
	smsCode: '',
	accountId: ''
});

let bank = ref();
onLoad(async () => {
	let _res = await operatorInfo();
	getInfo.value = _res;
	uni.$on('bank', (e) => {
		bank.value = e;
		dataFrom.accountId = e.id;
		console.log(e);
	});
});

let toNav = () => {
	uni.navigateTo({
		url: '/pages/mine/bankCard?type=xz'
	});
};

let codeChange2 = (text) => {
	tips.value = text;
};

let getCode = () => {
	console.log(123456);
	
	
	if (!getInfo.value.phone) return uni.showToast({ title: '请输入手机号', icon: 'none' });
	if (uCode.value.canGetCode) {
		// 模拟向后端请求验证码
		uni.showLoading({
			title: '正在获取验证码'
		});
		smsCode({
			phone: getInfo.value.phone
		}).then((res) => {
			uni.hideLoading();
			uni.$u.toast('验证码已发送');
			uCode.value.start();
		});
	} else {
		uni.$u.toast('倒计时结束后再发送');
	}
};

function 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;
}

let payOrder = async () => {
	if (Number(getInfo.value.balance) < Number(dataFrom.amount)) return uni.showToast({ title: '可提现金额不足', icon: 'none' });
	if (!dataFrom.accountId) return uni.showToast({ title: '请先选择提现银行卡', icon: 'none' });
	if (!dataFrom.amount) return uni.showToast({ title: '请输入提现金额', icon: 'none' });
	if (!dataFrom.smsCode) return uni.showToast({ title: '请输入验证码', icon: 'none' });
	let _res = await userExtract(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: #4874e5;
	border-radius: 12rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	font-weight: bold;
	font-size: 24rpx;
	color: #ffffff;
}
</style>
