<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="save-btn" @click="handleSave">保存</view>
			<view class="unbind-btn" @click="handleUnbind">解除绑定</view>
		</view>
	</view>
</template>

<script>
import { updateBankCard, deleteBankCard, getBankCardDetail, getBankList, getBranchList } from '@/api-js/bankCard'
export default {
	data() {
		return {
			cardId: '',
			formData: {
				bank: '',
				branch: '',
				cardNumber: ''
			},
			bankList: [],
			branchList: []
		}
	},
	onLoad(options) {
		if (options.id) {
			this.cardId = options.id
			this.loadBankCardDetail()
		}
		this.loadBankList()
	},
	methods: {
		async loadBankCardDetail() {
			try {
				uni.showLoading({ title: '加载中...' })
				const res = await getBankCardDetail({ id: this.cardId })
				uni.hideLoading()
				if (res.code === 200 && res.data) {
					this.formData = {
						bank: res.data.bankName || '',
						branch: res.data.branchName || '',
						cardNumber: res.data.cardNumber || ''
					}
					if (res.data.bankId) {
						this.loadBranchList(res.data.bankId)
					}
				}
			} catch (e) {
				uni.hideLoading()
				console.error('加载银行卡详情失败', e)
			}
		},
		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) {
				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 handleSave() {
			// 表单验证
			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 updateBankCard({
					id: this.cardId,
					...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: '保存失败'
				})
			}
		},
		handleUnbind() {
			uni.showModal({
				title: '提示',
				content: '确定要解除绑定该银行卡吗？',
				success: async (res) => {
					if (res.confirm) {
						try {
							uni.showLoading({ title: '解除绑定中...' })
							const result = await deleteBankCard({ id: this.cardId })
							uni.hideLoading()
							if (result.code === 200) {
								uni.showToast({
									icon: 'success',
									title: '解除绑定成功'
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 1500)
							} else {
								uni.showToast({
									icon: 'none',
									title: result.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;
	display: flex;
	flex-direction: column;
	gap: 16rpx;
	
	.save-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;
	}
	
	.unbind-btn {
		width: 100%;
		height: 88rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		color: #388BFF;
	}
}
</style>


