<?php
require_once dirname(__DIR__, 2) . '/config.php';

// 获取订单号
$order_no = $_GET['order_no'] ?? '';
$amount = $_GET['amount'] ?? '';
$type = $_GET['type'] ?? '';

if (empty($order_no)) {
    header('Location: ' . SITE_URL);
    exit;
}

// 查询订单信息
$order = $db->fetchOne("SELECT * FROM orders WHERE order_no = '$order_no'");

if (!$order) {
    header('Location: ' . SITE_URL);
    exit;
}

// 获取用户信息
$user = currentUser();
$site_name = getSiteConfig('site_name') ?? '授权系统';

// 根据订单类型显示不同内容
$type_names = [
    'recharge' => '余额充值',
    'app' => '购买应用',
    'group' => '购买会员'
];
$type_name = $type_names[$order['type']] ?? '支付';

// 如果是应用购买，获取应用名称
$item_name = '';
if ($order['type'] == 'app') {
    $app = $db->fetchOne("SELECT name FROM apps WHERE id = {$order['item_id']}");
    $item_name = $app['name'] ?? '';
} elseif ($order['type'] == 'group') {
    $group = $db->fetchOne("SELECT name FROM user_groups WHERE id = {$order['item_id']}");
    $item_name = $group['name'] ?? '';
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>支付成功 - <?php echo htmlspecialchars($site_name); ?></title>
    <link rel="stylesheet" href="/assets/css/style.css">
    <link rel="stylesheet" href="/assets/css/dark.css" id="dark-theme" disabled>
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        .success-card {
            max-width: 500px;
            width: 100%;
            background: #fff;
            border-radius: 24px;
            padding: 40px 30px;
            text-align: center;
            box-shadow: 0 20px 40px rgba(0,0,0,0.2);
            animation: fadeInUp 0.5s ease;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .success-icon {
            width: 80px;
            height: 80px;
            background: linear-gradient(135deg, #10b981, #059669);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 20px;
            animation: scaleIn 0.3s ease 0.2s both;
        }
        @keyframes scaleIn {
            from {
                transform: scale(0);
            }
            to {
                transform: scale(1);
            }
        }
        .success-icon svg {
            width: 45px;
            height: 45px;
            color: white;
        }
        h1 {
            font-size: 28px;
            color: #1f2937;
            margin-bottom: 10px;
        }
        .amount {
            font-size: 36px;
            font-weight: 700;
            color: #10b981;
            margin: 15px 0;
        }
        .order-info {
            background: #f9fafb;
            border-radius: 16px;
            padding: 20px;
            margin: 25px 0;
            text-align: left;
        }
        .order-info p {
            margin: 8px 0;
            color: #4b5563;
        }
        .order-info strong {
            color: #1f2937;
        }
        .btn-group {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 25px;
            flex-wrap: wrap;
        }
        .btn {
            display: inline-block;
            padding: 12px 24px;
            border-radius: 40px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.2s;
        }
        .btn-primary {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
        }
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102,126,234,0.4);
        }
        .btn-secondary {
            background: #f3f4f6;
            color: #374151;
            border: 1px solid #e5e7eb;
        }
        .btn-secondary:hover {
            background: #e5e7eb;
        }
        .item-name {
            font-size: 18px;
            font-weight: 600;
            color: #667eea;
        }
        .footer {
            margin-top: 25px;
            font-size: 12px;
            color: #9ca3af;
        }
        @media (max-width: 480px) {
            .success-card {
                padding: 30px 20px;
            }
            h1 {
                font-size: 24px;
            }
            .amount {
                font-size: 28px;
            }
            .btn-group {
                gap: 10px;
            }
            .btn {
                padding: 10px 18px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
<div class="success-card">
    <div class="success-icon">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <polyline points="20 6 9 17 4 12"></polyline>
        </svg>
    </div>
    
    <h1>支付成功！</h1>
    <p style="color: #6b7280;">您的订单已成功处理</p>
    
    <?php if ($order['type'] == 'recharge'): ?>
        <div class="amount">¥<?php echo number_format($order['amount'], 2); ?></div>
        <div class="order-info">
            <p><strong>充值类型：</strong> 账户余额充值</p>
            <p><strong>订单号：</strong> <?php echo $order_no; ?></p>
            <p><strong>支付时间：</strong> <?php echo $order['paid_at'] ?? date('Y-m-d H:i:s'); ?></p>
        </div>
    <?php elseif ($order['type'] == 'app'): ?>
        <div class="amount">¥<?php echo number_format($order['amount'], 2); ?></div>
        <div class="order-info">
            <p><strong>购买类型：</strong> 应用授权</p>
            <p><strong>应用名称：</strong> <span class="item-name"><?php echo htmlspecialchars($item_name); ?></span></p>
            <p><strong>订单号：</strong> <?php echo $order_no; ?></p>
            <p><strong>支付时间：</strong> <?php echo $order['paid_at'] ?? date('Y-m-d H:i:s'); ?></p>
        </div>
    <?php elseif ($order['type'] == 'group'): ?>
        <div class="amount">¥<?php echo number_format($order['amount'], 2); ?></div>
        <div class="order-info">
            <p><strong>购买类型：</strong> 会员升级</p>
            <p><strong>会员套餐：</strong> <span class="item-name"><?php echo htmlspecialchars($item_name); ?></span></p>
            <p><strong>订单号：</strong> <?php echo $order_no; ?></p>
            <p><strong>支付时间：</strong> <?php echo $order['paid_at'] ?? date('Y-m-d H:i:s'); ?></p>
        </div>
    <?php endif; ?>
    
    <div class="btn-group">
        <a href="<?php echo SITE_URL; ?>admin.php" class="btn btn-primary">进入用户中心</a>
        <a href="<?php echo SITE_URL; ?>admin.php?page=orders" class="btn btn-secondary">查看订单</a>
    </div>
    
    <div class="footer">
        <p>如有疑问，请联系客服</p>
    </div>
</div>

<script>
    3秒后自动跳转到用户中心（可选）
    setTimeout(function() {
        window.location.href = '<?php echo SITE_URL; ?>admin.php';
    }, 3000);
</script>
</body>
</html>