<?php require "../includes/header.php"; ?>
<?php require "../../config/config.php"; ?>

<?php
if(!isset($_SESSION['admin_username'])){ header("location: ../login.php"); exit; }

$admins = $conn->query("SELECT * FROM admins ORDER BY id DESC")->fetchAll(PDO::FETCH_OBJ);
?>

<div class="topbar">
    <h4><i class="fa fa-users mr-2"></i> Admins</h4>
    <div class="admin-badge">
        <div class="avatar"><?php echo strtoupper(substr($_SESSION['admin_username'],0,1)); ?></div>
        <span><?php echo htmlspecialchars($_SESSION['admin_username']); ?></span>
        <a href="../logout.php"><i class="fa fa-sign-out"></i> Logout</a>
    </div>
</div>

<div class="main-content">
    <div class="admin-table">
        <div class="table-section-header">
            <h5><i class="fa fa-users mr-2"></i> All Admins</h5>
            <a href="create.php" class="btn btn-admin-primary"><i class="fa fa-plus mr-1"></i> Create Admin</a>
        </div>
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr><th>#</th><th>Username</th><th>Email</th><th>Created At</th></tr>
                </thead>
                <tbody>
                    <?php foreach($admins as $a): ?>
                    <tr>
                        <td><?php echo $a->id; ?></td>
                        <td><?php echo htmlspecialchars($a->username); ?></td>
                        <td><?php echo htmlspecialchars($a->email); ?></td>
                        <td><?php echo $a->created_at; ?></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(count($admins) === 0): ?>
                    <tr><td colspan="4" class="text-center text-muted py-4">No admins found. <a href="create.php">Create one</a></td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require "../includes/footer.php"; ?>
