<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="back-btn" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			<text class="header-title">支付</text>
			<view class="header-right"></view>
		</view>
		
		<scroll-view class="scroll-content" scroll-y>
			<!-- 支付金额 -->
			<view class="amount-section">
				<text class="amount-label">支付金额</text>
				<text class="amount-value">¥ {{ paymentAmount.toFixed(2) }}</text>
			</view>
			
			<!-- 支付方式选择 -->
			<view class="payment-card">
				<view 
					class="payment-item" 
					v-for="(method, index) in paymentMethods" 
					:key="index"
					@click="selectPaymentMethod(index)"
				>
					<view class="payment-left">
						<view class="payment-icon" :style="{ backgroundColor: method.iconBg }">
							<text class="payment-icon-text">{{ method.icon }}</text>
						</view>
						<text class="payment-name">{{ method.name }}</text>
					</view>
					<view class="payment-radio" :class="{ checked: selectedMethod === index }">
						<view class="radio-dot" v-if="selectedMethod === index"></view>
					</view>
				</view>
			</view>
		</scroll-view>
		
		<!-- 确认支付按钮 -->
		<view class="confirm-section">
			<button class="confirm-btn" @click="confirmPayment">确认支付</button>
		</view>
	</view>
</template>

<script>
	import { purchaseVip } from '@/utils/api.js'
	
	export default {
		data() {
			return {
				paymentAmount: 30.00,
				selectedMethod: 0,
				planName: '',
				planType: 'month',
				planId: 0,
				isProcessing: false,
				paymentMethods: [
					{
						name: '微信支付',
						icon: '💬',
						iconBg: '#07C160',
						type: 'wechat'
					},
					{
						name: '支付宝支付',
						icon: '💰',
						iconBg: '#1677FF',
						type: 'alipay'
					},
					{
						name: '其他支付',
						icon: '💳',
						iconBg: '#999999',
						type: 'other'
					}
				]
			}
		},
		onLoad(options) {
			if (options.price) {
				this.paymentAmount = parseFloat(options.price)
			}
			if (options.planName) {
				this.planName = decodeURIComponent(options.planName)
			}
			if (options.planType) {
				this.planType = options.planType
			}
			if (options.planId) {
				this.planId = parseInt(options.planId)
			}
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			selectPaymentMethod(index) {
				this.selectedMethod = index
			},
			async confirmPayment() {
				// 获取用户信息
				const userInfo = uni.getStorageSync('userInfo')
				if (!userInfo || !userInfo.id) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 1500)
					return
				}
				
				if (this.isProcessing) {
					return
				}
				
				const method = this.paymentMethods[this.selectedMethod]
				
				uni.showModal({
					title: '确认支付',
					content: `使用${method.name}支付 ¥${this.paymentAmount.toFixed(2)}？`,
					success: async (res) => {
						if (res.confirm) {
							await this.processPayment(method, userInfo.id)
						}
					}
				})
			},
			async processPayment(method, userId) {
				this.isProcessing = true
				uni.showLoading({
					title: '支付中...',
					mask: true
				})
				
				try {
					// 调用购买VIP接口
					const purchaseData = {
						userId: userId,
						vipType: this.planType,
						vipName: this.planName,
						price: this.paymentAmount,
						paymentMethod: method.type
					}
					
					const res = await purchaseVip(purchaseData)
					
					uni.hideLoading()
					this.isProcessing = false
					
					if (res && res.code === 200) {
						uni.showToast({
							title: '支付成功',
							icon: 'success'
						})
						
						// 更新本地用户信息中的VIP状态
						if (userInfo) {
							userInfo.isVip = true
							if (res.data && res.data.expireTime) {
								// 可以更新过期时间（如果返回了）
							}
							uni.setStorageSync('userInfo', userInfo)
						}
						
						// 支付成功后跳转
						setTimeout(() => {
							uni.navigateBack({
								delta: 2 // 返回VIP页面再返回上一页
							})
						}, 1500)
					} else {
						uni.showToast({
							title: res.message || '支付失败，请重试',
							icon: 'none'
						})
					}
				} catch (error) {
					uni.hideLoading()
					this.isProcessing = false
					console.error('支付失败:', error)
					uni.showToast({
						title: error.message || '支付失败，请重试',
						icon: 'none'
					})
				}
			}
		}
	}
</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: 40rpx 30rpx;
		padding-bottom: calc(40rpx + env(safe-area-inset-bottom));
		box-sizing: border-box;
	}
	
	.amount-section {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 60rpx 0;
		margin-bottom: 40rpx;
	}
	
	.amount-label {
		font-size: 28rpx;
		color: #999999;
		margin-bottom: 20rpx;
	}
	
	.amount-value {
		font-size: 64rpx;
		font-weight: bold;
		color: #333333;
	}
	
	.payment-card {
		background-color: #FFFFFF;
		border-radius: 16rpx;
		overflow: hidden;
	}
	
	.payment-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx;
		border-bottom: 1rpx solid #F0F0F0;
	}
	
	.payment-item:last-child {
		border-bottom: none;
	}
	
	.payment-left {
		display: flex;
		align-items: center;
		flex: 1;
	}
	
	.payment-icon {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 30rpx;
	}
	
	.payment-icon-text {
		font-size: 40rpx;
	}
	
	.payment-name {
		font-size: 30rpx;
		color: #333333;
	}
	
	.payment-radio {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		border: 2rpx solid #CCCCCC;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #FFFFFF;
	}
	
	.payment-radio.checked {
		border-color: #4FC3F7;
		background-color: #4FC3F7;
	}
	
	.radio-dot {
		width: 16rpx;
		height: 16rpx;
		border-radius: 50%;
		background-color: #FFFFFF;
	}
	
	.confirm-section {
		padding: 30rpx;
		background-color: #FFFFFF;
		border-top: 1rpx solid #F0F0F0;
		padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
		flex-shrink: 0;
		box-sizing: border-box;
	}
	
	.confirm-btn {
		width: 100%;
		height: 88rpx;
		background-color: #4FC3F7;
		color: #FFFFFF;
		font-size: 32rpx;
		font-weight: bold;
		border: none;
		border-radius: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.confirm-btn::after {
		border: none;
	}
</style>


