<template>
	<view class="container">
		<!-- 导航栏 -->
		<view class="navbar">
			<view class="nav-left" @click="goBack">
				<text class="back-icon"><</text>
			</view>
			<view class="nav-title">添加银行卡</view>
			<view class="nav-right">
				<text class="more-icon">...</text>
				<text class="eye-icon">O</text>
			</view>
		</view>
		
		<!-- 表单区域 -->
		<scroll-view class="content" scroll-y>
			<view class="form-section">
				<view class="form-item">
					<view class="form-label">开户行</view>
					<picker mode="selector" :range="bankList" range-key="name" @change="onBankChange">
						<view class="form-input picker-input" :class="{ placeholder: !formData.bank }">
							{{ formData.bank || '请选择开户行' }}
							<text class="arrow-right">></text>
						</view>
					</picker>
				</view>
				<view class="divider"></view>
				
				<view class="form-item">
					<view class="form-label">支行</view>
					<picker mode="selector" :range="branchList" range-key="name" @change="onBranchChange">
						<view class="form-input picker-input" :class="{ placeholder: !formData.branch }">
							{{ formData.branch || '请选择支行' }}
							<text class="arrow-right">></text>
						</view>
					</picker>
				</view>
				<view class="divider"></view>
				
				<view class="form-item">
					<view class="form-label">银行卡号</view>
					<input 
						class="form-input" 
						v-model="formData.cardNumber" 
						placeholder="请输入银行卡号"
						type="number"
						maxlength="19"
						placeholder-class="placeholder"
					/>
				</view>
			</view>
			
			<!-- 注意事项 -->
			<view class="notes-section">
				<view class="notes-title">注意:</view>
				<view class="notes-item">1、只能绑定认证用户本人的银行卡;</view>
				<view class="notes-item">2、每次更换次数不得超过三次。</view>
			</view>
		</scroll-view>
		
		<!-- 底部按钮 -->
		<view class="bottom-bar">
			<view class="submit-btn" @click="handleSubmit">确认添加</view>
		</view>
	</view>
</template>

<script>
import { addBankCard, getBankList, getBranchList } from '@/api-js/bankCard'
export default {
	data() {
		return {
			formData: {
				bank: '',
				branch: '',
				cardNumber: ''
			},
			bankList: [],
			branchList: []
		}
	},
	onLoad() {
		this.loadBankList()
	},
	methods: {
		async loadBankList() {
			try {
				const res = await getBankList()
				if (res.code === 200 && res.data) {
					this.bankList = res.data
				} else {
					// 默认数据
					this.bankList = [
						{ name: '中国工商银行', id: 1 },
						{ name: '中国建设银行', id: 2 },
						{ name: '中国银行', id: 3 },
						{ name: '中国农业银行', id: 4 }
					]
				}
			} catch (e) {
				console.error('加载银行列表失败', e)
				this.bankList = [
					{ name: '中国工商银行', id: 1 },
					{ name: '中国建设银行', id: 2 },
					{ name: '中国银行', id: 3 },
					{ name: '中国农业银行', id: 4 }
				]
			}
		},
		async loadBranchList(bankId) {
			try {
				const res = await getBranchList({ bankId })
				if (res.code === 200 && res.data) {
					this.branchList = res.data
				} else {
					this.branchList = [
						{ name: '北京支行', id: 1 },
						{ name: '上海支行', id: 2 },
						{ name: '广州支行', id: 3 }
					]
				}
			} catch (e) {
				this.branchList = [
					{ name: '北京支行', id: 1 },
					{ name: '上海支行', id: 2 },
					{ name: '广州支行', id: 3 }
				]
			}
		},
		onBankChange(e) {
			const index = e.detail.value
			this.formData.bank = this.bankList[index].name
			this.formData.bankId = this.bankList[index].id
			this.formData.branch = '' // 清空支行选择
			if (this.formData.bankId) {
				this.loadBranchList(this.formData.bankId)
			}
		},
		onBranchChange(e) {
			const index = e.detail.value
			this.formData.branch = this.branchList[index].name
			this.formData.branchId = this.branchList[index].id
		},
		goBack() {
			uni.navigateBack()
		},
		async handleSubmit() {
			// 表单验证
			if (!this.formData.bank) {
				uni.showToast({
					icon: 'none',
					title: '请选择开户行'
				})
				return
			}
			if (!this.formData.branch) {
				uni.showToast({
					icon: 'none',
					title: '请选择支行'
				})
				return
			}
			if (!this.formData.cardNumber) {
				uni.showToast({
					icon: 'none',
					title: '请输入银行卡号'
				})
				return
			}
			// 银行卡号验证（简单验证）
			if (this.formData.cardNumber.length < 16) {
				uni.showToast({
					icon: 'none',
					title: '请输入正确的银行卡号'
				})
				return
			}
			
			try {
				uni.showLoading({ title: '添加中...' })
				const res = await addBankCard(this.formData)
				uni.hideLoading()
				if (res.code === 200) {
					uni.showToast({
						icon: 'success',
						title: '添加成功'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				} else {
					uni.showToast({
						icon: 'none',
						title: res.msg || '添加失败'
					})
				}
			} catch (e) {
				uni.hideLoading()
				uni.showToast({
					icon: 'none',
					title: '添加失败'
				})
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	background: #f5f5f5;
	display: flex;
	flex-direction: column;
}

.navbar {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 20rpx 24rpx;
	background: #fff;
	border-bottom: 1rpx solid #f0f0f0;
	
	.nav-left {
		width: 60rpx;
		
		.back-icon {
			font-size: 36rpx;
			color: #333;
			font-weight: bold;
		}
	}
	
	.nav-title {
		flex: 1;
		text-align: center;
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}
	
	.nav-right {
		display: flex;
		align-items: center;
		gap: 24rpx;
		width: 60rpx;
		justify-content: flex-end;
		
		.more-icon {
			font-size: 32rpx;
			color: #333;
		}
		
		.eye-icon {
			font-size: 32rpx;
			color: #333;
		}
	}
}

.content {
	flex: 1;
	padding-bottom: 140rpx;
}

.form-section {
	background: #fff;
	margin: 24rpx;
	border-radius: 16rpx;
	overflow: hidden;
	
	.form-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 32rpx 24rpx;
		min-height: 100rpx;
		
		.form-label {
			font-size: 28rpx;
			color: #333;
			font-weight: 500;
			width: 150rpx;
		}
		
		.form-input {
			flex: 1;
			font-size: 28rpx;
			color: #333;
			text-align: right;
			margin-left: 24rpx;
			
			&.placeholder {
				color: #C8C9CC;
			}
			
			&.picker-input {
				display: flex;
				align-items: center;
				justify-content: flex-end;
				
				.arrow-right {
					font-size: 32rpx;
					color: #999;
					margin-left: 8rpx;
				}
			}
		}
	}
	
	.divider {
		height: 1rpx;
		background: #EBEDF0;
		margin-left: 24rpx;
	}
}

.notes-section {
	background: #fff;
	border-radius: 16rpx;
	padding: 32rpx;
	margin: 0 24rpx 24rpx;
	
	.notes-title {
		font-size: 28rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 16rpx;
	}
	
	.notes-item {
		font-size: 26rpx;
		color: #666;
		line-height: 1.8;
		margin-bottom: 8rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
	}
}

.bottom-bar {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background: #fff;
	padding: 24rpx;
	border-top: 1rpx solid #f0f0f0;
	z-index: 100;
	
	.submit-btn {
		width: 100%;
		height: 88rpx;
		background: #388BFF;
		border-radius: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		font-weight: bold;
		color: #fff;
	}
}
</style>


