<?php
// sales/index.php - Landing page and subscription checkout creator for team-mania.de/pro
require_once __DIR__ . '/config.php';

$checkout_error = '';

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

    if (empty($club_name) || empty($email) || $members < 10) {
        $checkout_error = 'Bitte füllen Sie alle Felder korrekt aus.';
    } else {
        // Create Stripe Checkout Session using raw cURL (zero external dependencies)
        $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']) . '/index.php';

        $post_fields = [
            'mode' => 'subscription',
            'success_url' => $success_url,
            'cancel_url' => $cancel_url,
            'customer_email' => $email,
            'line_items[0][price_data][currency]' => 'eur',
            'line_items[0][price_data][recurring][interval]' => 'month',
            'line_items[0][price_data][unit_amount]' => '160', // 1,60 € in cents
            'line_items[0][price_data][product_data][name]' => 'Team Mania PRO - Lizenz',
            'line_items[0][price_data][product_data][description]' => 'Monatliches Grundabonnement pro Vereinsmitglied für ' . $club_name,
            'line_items[0][quantity]' => $members,
            'metadata[club_name]' => $club_name,
            'metadata[type]' => 'core_license',
            'metadata[max_members]' => $members
        ];

        $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>Team Mania PRO erwerben</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: 550px;
            padding: 40px;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
        }
        h1 {
            font-size: 26px;
            font-weight: 800;
            text-align: center;
            color: var(--primary);
            margin-bottom: 5px;
        }
        .subtitle {
            text-align: center;
            color: var(--text-muted);
            font-size: 14px;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-size: 13px;
            font-weight: 600;
            color: var(--text-muted);
        }
        input[type="text"], input[type="email"] {
            width: 100%;
            padding: 12px;
            background-color: #0f172a;
            border: 1px solid var(--border);
            border-radius: 6px;
            color: #fff;
            font-size: 14px;
        }
        input[type="range"] {
            width: 100%;
            margin: 15px 0;
            accent-color: var(--primary);
        }
        .slider-box {
            background: #0f172a;
            border: 1px solid var(--border);
            padding: 20px;
            border-radius: 8px;
            text-align: center;
            margin-bottom: 25px;
        }
        .price-display {
            font-size: 32px;
            font-weight: 800;
            color: #fff;
            margin-top: 5px;
        }
        .price-sub {
            font-size: 12px;
            color: var(--text-muted);
            margin-top: 3px;
        }
        .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>Team Mania PRO</h1>
    <p class="subtitle">Berechnen Sie Ihr monatliches Grundabo (1,60 € pro Mitglied)</p>

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

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

        <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 Lizenzlieferung</label>
            <input type="email" id="email" name="email" placeholder="vorstand@deinverein.de" required>
        </div>

        <div class="form-group">
            <label for="members_range">Mitgliederanzahl: <span id="members-count" style="color: #fff; font-weight: bold;">50</span></label>
            <input type="range" id="members_range" name="members" min="10" max="500" step="5" value="50" oninput="updatePrice(this.value)">
        </div>

        <div class="slider-box">
            <span>Ihr monatlicher Preis:</span>
            <div class="price-display"><span id="price-val">80,00</span> €</div>
            <div class="price-sub">Berechnet als <span id="count-val">50</span> Mitglieder &times; 1,60 € zzgl. MwSt.</div>
        </div>

        <button type="submit" class="btn">Jetzt abonnieren & downloaden</button>
    </form>
</div>

<script>
function updatePrice(val) {
    document.getElementById('members-count').textContent = val;
    document.getElementById('count-val').textContent = val;
    
    // 1.60 € per member
    let price = val * 1.60;
    document.getElementById('price-val').textContent = price.toFixed(2).replace('.', ',');
}
</script>

</body>
</html>
