<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.role == 1 ? '个人' : '企业'" size="mini" :type="bank.role == 1 ? 'success' : 'error'" plain plainFill></u-tag>
					</view>
					<view style="width: 30rpx"></view>
					<view style="font-size: 26rpx">{{ bank.realname }}</view>
					<view style="width: 30rpx"></view>
					<view style="font-size: 26rpx">{{ bank.mobile || '' }}</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.role == 2">开户行：{{ bank.subname }}</view>
					<view style="height: 10rpx"></view>
					<view style="font-size: 28rpx">银行卡号：{{ bank.num }}</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.money"></up-input>
				</view>
				<view class="withdraw_mon_input_note" @click="dataFrom.money = getInfo.money">全部提现</view>
			</view>

			<view>
				<up-input :border="false" disabled v-model="getInfo.mobile"></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.money }}</view>
			<view class="withdraw_mon_note">最低提现金额为￥{{ appConfig.chargeMinAmount }}</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">1.现在本软件填写提现账户</view> -->
			<!-- <view class="withdraw_note_s">2.然后扫描二维码填写相应的提现账户</view> -->
			<view class="withdraw_note_s">1.每月1号、11号、21号可提现，到账时间T+(1-3)</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 class="withdraw_note_s" style="flex-direction: column; display: flex; align-items: center">
				<up-image width="350rpx" height="350rpx" src="https://xingdong-app.oss-cn-beijing.aliyuncs.com/hongjiesuan-xingdong.png" mode="widthFix"></up-image>
				<view style="height: 10rpx"></view>
				<view>长按保存二维码</view>
			</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 { userInfoIsInvest, smsCode, getAppConfig, pointsExtract } from '@/api/api.js';
import { onLoad, onShow } from '@dcloudio/uni-app';
let value = ref();
let getInfo = ref({});
let appConfig = ref([]);
let tips = ref('');
let uCode = ref(null);
let dataFrom = reactive({
	money: '',
	smsCode: '',
	accountId: ''
});

let bank = ref();
onLoad(async () => {
	let _res = await userInfoIsInvest();
	getInfo.value = _res;
	const configData = await getAppConfig();
	const walletConfig = configData?.find(item => item.configKey === "APP_WALLET")
	const configValue = walletConfig ? walletConfig.configValue : {}
	appConfig.value = JSON.parse(configValue);

	uni.$on('bank', (e) => {
		bank.value = e;
		dataFrom.accountId = e.id;
		console.log(e);
	});
});

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

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

let getCode = () => {
	console.log(123456);

	if (!getInfo.value.mobile) return uni.showToast({ title: '请输入手机号', icon: 'none' });
	if (uCode.value.canGetCode) {
		// 模拟向后端请求验证码
		uni.showLoading({
			title: '正在获取验证码'
		});
		smsCode({
			phone: getInfo.value.mobile
		}).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 () => {
	console.log(getInfo.value.money, 'getInfo.value.money');
	console.log(dataFrom.money, 'dataFrom.money');
	if (Number(getInfo.value.money) < Number(dataFrom.money)) return uni.showToast({ title: '可提现金额不足', icon: 'none' });
	if (!dataFrom.accountId) return uni.showToast({ title: '请先选择提现银行卡', icon: 'none' });
	if (!dataFrom.money) return uni.showToast({ title: '请输入提现金额', icon: 'none' });
	if (!dataFrom.smsCode) return uni.showToast({ title: '请输入验证码', icon: 'none' });
	let _res = await pointsExtract(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>
