<?php
session_start();

// Handle logout
if (isset($_GET['logout'])) {
    session_unset();
    session_destroy();
    header('Location: login.php');
    exit;
}

// If the user is not logged in as admin, redirect to the login page.
if (!isset($_SESSION['user']) || $_SESSION['user'] !== 'admin') {
    header('Location: login.php');
    exit;
}

// --- Data Fetching ---
require_once 'db/config.php';
$attendees = [];
try {
    $pdo = db();
    // Updated to select first_name and new fields
    $stmt = $pdo->query('SELECT a.id, w.title AS webinar_title, a.first_name, a.last_name, a.email, a.company, a.how_did_you_hear, a.consented, a.created_at, a.timezone, a.utm_source, a.utm_medium, a.utm_campaign, a.referrer, a.gclid, a.fbclid FROM attendees a JOIN webinars w ON a.webinar_id = w.id ORDER BY a.created_at DESC');
    $attendees = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Could not connect to the database: " . $e->getMessage());
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Board</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #121212;
            color: #e0e0e0;
        }
        .container {
            width: 100%;
            max-width: 1400px;
            padding: 2rem;
        }
        h1 {
            color: #ffd700;
        }
        .table {
            color: #e0e0e0;
        }
        .table-dark {
            --bs-table-bg: #212529;
            border-color: #373b3e;
        }
        .logout-btn {
            position: absolute;
            top: 1rem;
            right: 1rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="admin.php?logout=1" class="btn btn-secondary logout-btn">Logout</a>
        <h1 class="mb-4 text-center">Webinar Attendees</h1>
        <div class="table-responsive">
            <table class="table table-dark table-striped table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Webinar</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Email</th>
                        <th>Company</th>
                        <th>How did you hear?</th>
                        <th>Consented</th>
                        <th>Registered At</th>
                        <th>Timezone</th>
                        <th>UTM Source</th>
                        <th>UTM Medium</th>
                        <th>UTM Campaign</th>
                        <th>Referrer</th>
                        <th>GCLID</th>
                        <th>FBCLID</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($attendees)): ?>
                        <tr>
                            <td colspan="16" class="text-center">No attendees yet.</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($attendees as $attendee): ?>
                            <tr>
                                <td><?= htmlspecialchars($attendee['id']) ?></td>
                                <td><?= htmlspecialchars($attendee['webinar_title']) ?></td>
                                <td><?= htmlspecialchars($attendee['first_name']) ?></td>
                                <td><?= htmlspecialchars($attendee['last_name']) ?></td>
                                <td><?= htmlspecialchars($attendee['email']) ?></td>
                                <td><?= htmlspecialchars($attendee['company']) ?></td>
                                <td><?= htmlspecialchars($attendee['how_did_you_hear']) ?></td>
                                <td><?= $attendee['consented'] ? 'Yes' : 'No' ?></td>
                                <td><?= htmlspecialchars($attendee['created_at']) ?></td>
                                <td><?= htmlspecialchars($attendee['timezone']) ?></td>
                                <td><?= htmlspecialchars($attendee['utm_source']) ?></td>
                                <td><?= htmlspecialchars($attendee['utm_medium']) ?></td>
                                <td><?= htmlspecialchars($attendee['utm_campaign']) ?></td>
                                <td><?= htmlspecialchars($attendee['referrer']) ?></td>
                                <td><?= htmlspecialchars($attendee['gclid']) ?></td>
                                <td><?= htmlspecialchars($attendee['fbclid']) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</body>
</html>
