<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="back-btn" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			<text class="header-title">VIP会员</text>
			<view class="header-right"></view>
		</view>
		
		<scroll-view class="scroll-content" scroll-y>
			<!-- VIP套餐列表 -->
			<view class="vip-plans">
				<view 
					class="plan-item" 
					v-for="(plan, index) in vipPlans" 
					:key="index"
					:class="{ active: selectedPlan === index }"
					@click="selectPlan(index)"
				>
					<view class="plan-header">
						<text class="plan-name">{{ plan.name }}</text>
						<text class="plan-price">¥{{ plan.price }}</text>
					</view>
					<text class="plan-desc">{{ plan.desc }}</text>
					<view class="plan-benefits">
						<text class="benefit-item" v-for="(benefit, i) in plan.benefits" :key="i">✓ {{ benefit }}</text>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<!-- 底部购买栏 -->
		<view class="bottom-bar">
			<view class="price-info">
				<text class="price-text">{{ selectedPlanInfo.price }}元/月</text>
			</view>
			<button class="buy-btn" @click="goToPayment">立即购买</button>
		</view>
	</view>
</template>

<script>
	import { getVipPlans } from '@/utils/api.js'
	
	export default {
		data() {
			return {
				selectedPlan: 0,
				vipPlans: [],
				loading: false
			}
		},
		computed: {
			selectedPlanInfo() {
				return this.vipPlans[this.selectedPlan] || {}
			}
		},
		onLoad() {
			this.loadVipPlans()
		},
		methods: {
			async loadVipPlans() {
				this.loading = true
				try {
					const res = await getVipPlans()
					if (res && res.code === 200 && res.data) {
						this.vipPlans = res.data
					} else {
						// 如果后端返回失败，使用默认数据
						this.vipPlans = [
							{
								type: 'month',
								name: '月卡VIP',
								price: 30,
								desc: '适合短期阅读',
								benefits: ['无限阅读', '免费听书', '专属客服']
							},
							{
								type: 'quarter',
								name: '季卡VIP',
								price: 80,
								desc: '超值优惠',
								benefits: ['无限阅读', '免费听书', '专属客服', '优先更新']
							},
							{
								type: 'year',
								name: '年卡VIP',
								price: 288,
								desc: '最超值选择',
								benefits: ['无限阅读', '免费听书', '专属客服', '优先更新', '专属活动']
							}
						]
					}
				} catch (error) {
					console.error('加载VIP套餐失败:', error)
					// 使用默认数据
					this.vipPlans = [
						{
							type: 'month',
							name: '月卡VIP',
							price: 30,
							desc: '适合短期阅读',
							benefits: ['无限阅读', '免费听书', '专属客服']
						},
						{
							type: 'quarter',
							name: '季卡VIP',
							price: 80,
							desc: '超值优惠',
							benefits: ['无限阅读', '免费听书', '专属客服', '优先更新']
						},
						{
							type: 'year',
							name: '年卡VIP',
							price: 288,
							desc: '最超值选择',
							benefits: ['无限阅读', '免费听书', '专属客服', '优先更新', '专属活动']
						}
					]
				} finally {
					this.loading = false
				}
			},
			goBack() {
				uni.navigateBack()
			},
			selectPlan(index) {
				this.selectedPlan = index
			},
			goToPayment() {
				if (!this.selectedPlanInfo || !this.selectedPlanInfo.type) {
					uni.showToast({
						title: '请选择VIP套餐',
						icon: 'none'
					})
					return
				}
				
				const plan = this.selectedPlanInfo
				uni.navigateTo({
					url: `/pages/payment/payment?planId=${this.selectedPlan}&planType=${plan.type}&planName=${encodeURIComponent(plan.name)}&price=${plan.price}`
				})
			}
		}
	}
</script>

<style scoped>
	.container {
		width: 100%;
		height: 100vh;
		min-height: 100vh;
		background-color: #F5F5F5;
		display: flex;
		padding-top: 30px;
		box-sizing: border-box;
		flex-direction: column;
		box-sizing: border-box;
		overflow: hidden;
	}
	
	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		padding-top: calc(20rpx + env(safe-area-inset-top));
		background-color: #FFFFFF;
		border-bottom: 1rpx solid #F0F0F0;
		position: relative;
		flex-shrink: 0;
		box-sizing: border-box;
	}
	
	.back-btn {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 10;
	}
	
	.back-icon {
		font-size: 40rpx;
		color: #333333;
		font-weight: bold;
	}
	
	.header-title {
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}
	
	.header-right {
		width: 60rpx;
	}
	
	.scroll-content {
		flex: 1;
		width: 100%;
		height: 0;
		overflow: hidden;
		padding: 30rpx;
		padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
		box-sizing: border-box;
	}
	
	.vip-plans {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}
	
	.plan-item {
		background-color: #FFFFFF;
		border-radius: 16rpx;
		padding: 30rpx;
		border: 2rpx solid transparent;
		transition: all 0.3s;
	}
	
	.plan-item.active {
		border-color: #FF6B35;
		box-shadow: 0 4rpx 12rpx rgba(255, 107, 53, 0.2);
	}
	
	.plan-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 15rpx;
	}
	
	.plan-name {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
	}
	
	.plan-price {
		font-size: 36rpx;
		font-weight: bold;
		color: #FF6B35;
	}
	
	.plan-desc {
		font-size: 26rpx;
		color: #999999;
		margin-bottom: 20rpx;
	}
	
	.plan-benefits {
		display: flex;
		flex-direction: column;
		gap: 10rpx;
	}
	
	.benefit-item {
		font-size: 26rpx;
		color: #666666;
	}
	
	.bottom-bar {
		display: flex;
		align-items: center;
		background-color: #FFFFFF;
		border-top: 1rpx solid #F0F0F0;
		padding: 20rpx 30rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		flex-shrink: 0;
		box-sizing: border-box;
	}
	
	.price-info {
		flex: 1;
		background-color: #333333;
		border-radius: 8rpx;
		padding: 25rpx 30rpx;
		margin-right: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.price-text {
		font-size: 32rpx;
		font-weight: bold;
		color: #FFFFFF;
	}
	
	.buy-btn {
		flex: 1;
		height: 88rpx;
		background-color: #FF6B35;
		color: #FFFFFF;
		font-size: 32rpx;
		font-weight: bold;
		border: none;
		border-radius: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.buy-btn::after {
		border: none;
	}
</style>


