<template>
	<view style="padding: 20rpx;min-height: 95.2vh;background: #f6f6f6;box-sizing: border-box;">
		<view class="bankList" @click="toAddUpdate(item)" v-for="item in dataList" :key="item.id">
			<view class="right">
				<u-icon name="grid" color="#fff" size="22"></u-icon>
			</view>
			<view class="left">
				<view class="bankName">
					{{item.bankCreat}}
				</view>
				<view class="bankCard">
					{{formatBankCard(item.bankNo)}}
				</view>
			</view>
			<view class="defaultd" v-if="item.defaultd==1">
				{{item.defaultdDesc}}
			</view>
		</view>
		<view class="bankList" @click="toAddUpdate(null)" style="background: #fff;padding: 40rpx 20rpx;color: #000;">
			<view class="right">
				<u-icon name="plus" color="#000" size="22"></u-icon>
			</view>添加银行卡
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataList: []
			}
		},
		onShow() {
			this.getDataList()
		},
		methods: {
			toAddUpdate(x) {
				uni.navigateTo({
					url: '/pages/main/set/userinfo/bankAddUpdate?date=' + JSON.stringify(x)
				})
			},
			formatBankCard(cardNumber) {
				// 去掉所有空格
				cardNumber = cardNumber.replace(/\s/g, '');

				// 只保留最后四位，前面的用星号代替
				const lastFour = cardNumber.slice(-4);
				const maskedSection = cardNumber.slice(0, -4).replace(/\d/g, '*');

				// 将每四位加一个空格
				const formattedCard = maskedSection + lastFour;
				return formattedCard.replace(/(.{4})/g, '$1 ').trim();
			},
			getDataList() {
				let user = uni.getStorageSync('userInfo')
				this.$api.to_http(`/employees/banklist/getBankListByWork?workId=${user.userProfileId}`, "GET").then((res) => {
					if (res.data.code != 0) {
						return uni.$u.toast(res.data.msg)
					}
					this.dataList = res.data.data
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.bankList {
		position: relative;
		padding: 20rpx;
		background: #5486d5;
		color: #fff;
		margin-bottom: 15rpx;
		border-radius: 20rpx;
		box-sizing: border-box;
		display: flex;

		.right {
			margin-right: 10rpx;
		}

		.bankCard {
			margin-top: 10rpx;
		}

		.defaultd {
			position: absolute;
			right: 0;
			top: 0;
			font-size: 28rpx;
			padding: 10rpx;
			background-color: #ffaa00;
			border-radius: 10rpx;
			box-sizing: border-box;
		}
	}
</style>