<?php
// sales/forum.php - Checkout creator for the Forum Addon
require_once __DIR__ . '/config.php';

$checkout_error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'buy_forum') {
    $club_name = trim($_POST['club_name'] ?? '');
    $email = trim($_POST['email'] ?? '');

    if (empty($club_name) || empty($email)) {
        $checkout_error = 'Bitte füllen Sie alle Felder aus.';
    } else {
        $success_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/success.php?session_id={CHECKOUT_SESSION_ID}';
        $cancel_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/forum.php';

        $post_fields = [
            'mode' => 'payment',
            'success_url' => $success_url,
            'cancel_url' => $cancel_url,
            'customer_email' => $email,
            'line_items[0][price_data][currency]' => 'eur',
            'line_items[0][price_data][unit_amount]' => '4900', // 49,00 € in cents
            'line_items[0][price_data][product_data][name]' => 'Team Mania PRO - Forum Modul (Zusatzlizenz)',
            'line_items[0][price_data][product_data][description]' => 'Vollwertiges Diskussionsboard für den TSV / SV Vereinsauftritt',
            'line_items[0][quantity]' => 1,
            'metadata[club_name]' => $club_name,
            'metadata[type]' => 'addon_license',
            'metadata[addon_name]' => 'forum'
        ];

        $ch = curl_init('https://api.stripe.com/v1/checkout/sessions');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERPWD, STRIPE_SECRET_KEY . ':');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_fields));
        
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($http_code === 200) {
            $session = json_decode($response, true);
            if (isset($session['url'])) {
                header("Location: " . $session['url']);
                exit;
            }
        }
        $checkout_error = 'Stripe-Verbindung fehlgeschlagen. Bitte überprüfen Sie Ihre API-Schlüssel.';
    }
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forum Modul freischalten - Team Mania PRO</title>
    <style>
        :root {
            --bg: #0f172a;
            --card: #1e293b;
            --primary: #0284c7;
            --primary-hover: #0369a1;
            --text: #f8fafc;
            --text-muted: #94a3b8;
            --border: #334155;
        }
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', system-ui, sans-serif;
        }
        body {
            background-color: var(--bg);
            color: var(--text);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }
        .container {
            background-color: var(--card);
            border: 1px solid var(--border);
            border-radius: 12px;
            width: 100%;
            max-width: 500px;
            padding: 40px;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
        }
        h1 {
            font-size: 24px;
            font-weight: 800;
            text-align: center;
            color: var(--primary);
            margin-bottom: 5px;
        }
        .subtitle {
            text-align: center;
            color: var(--text-muted);
            font-size: 13px;
            margin-bottom: 30px;
            line-height: 1.4;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-size: 13px;
            font-weight: 600;
            color: var(--text-muted);
        }
        input {
            width: 100%;
            padding: 12px;
            background-color: #0f172a;
            border: 1px solid var(--border);
            border-radius: 6px;
            color: #fff;
            font-size: 14px;
        }
        .price-badge {
            background: #0f172a;
            border: 1px solid var(--border);
            padding: 15px;
            border-radius: 6px;
            text-align: center;
            font-size: 20px;
            font-weight: 800;
            margin-bottom: 25px;
        }
        .btn {
            display: block;
            width: 100%;
            padding: 14px;
            background-color: var(--primary);
            color: #fff;
            border: none;
            border-radius: 6px;
            font-size: 15px;
            font-weight: 600;
            cursor: pointer;
            text-align: center;
            transition: background-color 0.2s;
        }
        .btn:hover {
            background-color: var(--primary-hover);
        }
        .alert {
            background-color: rgba(239, 68, 68, 0.1);
            border: 1px solid #ef4444;
            color: #fca5a5;
            padding: 12px;
            border-radius: 6px;
            font-size: 13px;
            margin-bottom: 20px;
            text-align: center;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>Forum-Modul freischalten</h1>
    <p class="subtitle">Schalten Sie das vollwertige Diskussionsboard für Ihren Verein frei.</p>

    <?php if (!empty($checkout_error)): ?>
        <div class="alert"><?= htmlspecialchars($checkout_error) ?></div>
    <?php endif; ?>

    <div class="price-badge">Einmalig: 49,00 €</div>

    <form method="POST" action="">
        <input type="hidden" name="action" value="buy_forum">

        <div class="form-group">
            <label for="club_name">Vereinsname</label>
            <input type="text" id="club_name" name="club_name" placeholder="z. B. TSV Musterstadt e.V." required>
        </div>

        <div class="form-group">
            <label for="email">E-Mail-Adresse für die Lizenzfreischaltung</label>
            <input type="email" id="email" name="email" placeholder="vorstand@deinverein.de" required>
        </div>

        <button type="submit" class="btn">Modul kostenpflichtig bestellen</button>
    </form>
</div>

</body>
</html>
