<?php
session_start();
require_once 'db/config.php'; // Assuming you have a db connection setup

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

// Check for messages
$message = '';
if (isset($_SESSION['message'])) {
    $message = $_SESSION['message'];
    unset($_SESSION['message']);
}

$pdo = db();

// Pagination settings
$records_per_page = 10;
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $records_per_page;

// Get total number of records
$total_stmt = $pdo->query("SELECT COUNT(*) FROM attendees");
$total_records = $total_stmt->fetchColumn();
$total_pages = ceil($total_records / $records_per_page);

// Get records for the current page
$stmt = $pdo->prepare("SELECT id, first_name, last_name, email, created_at FROM attendees ORDER BY first_name ASC, last_name ASC LIMIT :limit OFFSET :offset");
$stmt->bindValue(':limit', $records_per_page, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$attendees = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Dashboard</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <h2>Admin Dashboard</h2>
        <p>Welcome, <?php echo htmlspecialchars($_SESSION['user'] ?? 'Admin'); ?>!</p>

        <?php if ($message): ?>
            <div class="alert alert-info">
                <?php echo htmlspecialchars($message); ?>
            </div>
        <?php endif; ?>

        <div class="d-flex justify-content-between align-items-center">
            <h3 class="mt-4">Registered Attendees</h3>
            <a href="export_csv.php" class="btn btn-success">Download CSV</a>
        </div>

        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Email</th>
                        <th>Registered At</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($attendees)): ?>
                        <tr>
                            <td colspan="6" class="text-center">No attendees found.</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($attendees as $attendee): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($attendee['id']); ?></td>
                                <td><?php echo htmlspecialchars($attendee['first_name']); ?></td>
                                <td><?php echo htmlspecialchars($attendee['last_name']); ?></td>
                                <td><?php echo htmlspecialchars($attendee['email']); ?></td>
                                <td><?php echo htmlspecialchars($attendee['created_at']); ?></td>
                                <td>
                                    <a href="edit_attendee.php?id=<?php echo $attendee['id']; ?>" class="btn btn-sm btn-primary">Edit</a>
                                    <form action="delete_attendee.php" method="POST" style="display: inline-block;">
                                        <input type="hidden" name="id" value="<?php echo $attendee['id']; ?>">
                                        <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">Delete</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <nav aria-label="Page navigation">
            <ul class="pagination justify-content-center">
                <?php if ($page > 1): ?>
                    <li class="page-item"><a class="page-link" href="?page=<?php echo $page - 1; ?>">Previous</a></li>
                <?php endif; ?>

                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                    <li class="page-item <?php if ($i == $page) echo 'active'; ?>"><a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
                <?php endfor; ?>

                <?php if ($page < $total_pages): ?>
                    <li class="page-item"><a class="page-link" href="?page=<?php echo $page + 1; ?>">Next</a></li>
                <?php endif; ?>
            </ul>
        </nav>

    </div>
</body>
</html>
