<?php
session_start();
include 'db.php';

// --- SECURITY CHECKS ---
// 1. Check if logged in
if (!isset($_SESSION['user_id'])) {
    header("Location: register.php");
    exit();
}

// 2. Check if the user is actually an ADMIN
if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] != 'admin') {
    // If they are not admin, kick them to the regular download page
    header("Location: download.php");
    exit();
}

// --- FETCH ALL USERS ---
$sql = "SELECT id, name, email, role FROM users";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Command Center</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body { background: #050505; color: #fff; min-height: 100vh; font-family: 'Segoe UI', sans-serif; padding: 40px; }
        
        /* Glass Panel */
        .admin-panel {
            background: rgba(255, 255, 255, 0.03);
            backdrop-filter: blur(20px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 20px 50px rgba(0,0,0,0.5);
        }

        /* Table Styling */
        .table { color: #ddd; margin-top: 20px; }
        .table thead { border-bottom: 2px solid #00d4ff; color: #00d4ff; }
        .table td, .table th { background: transparent; border-color: rgba(255,255,255,0.1); padding: 15px; }
        .table tr:hover { background: rgba(0, 212, 255, 0.05); }

        /* Badges */
        .badge-user { background: rgba(255,255,255,0.1); color: #fff; border: 1px solid rgba(255,255,255,0.2); }
        .badge-admin { background: rgba(0, 212, 255, 0.2); color: #00d4ff; border: 1px solid #00d4ff; }

        .header-title { text-transform: uppercase; letter-spacing: 2px; font-weight: 800; }
        .text-neon { color: #00d4ff; text-shadow: 0 0 10px rgba(0, 212, 255, 0.5); }
    </style>
</head>
<body>

<div class="container">
    <div class="admin-panel">
        
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="header-title">Command <span class="text-neon">Center</span></h2>
                <p class="text-white-50">Database Access Level: MASTER</p>
            </div>
            <div>
                <a href="download.php" class="btn btn-outline-light btn-sm me-2">View Files</a>
                <a href="register.php?logout=true" class="btn btn-danger btn-sm">Log Out</a>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Email Address</th>
                        <th>Access Level</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if ($result->num_rows > 0) {
                        while($row = $result->fetch_assoc()) {
                            // Determine badge style
                            $badgeClass = ($row['role'] == 'admin') ? 'badge-admin' : 'badge-user';
                            
                            echo "<tr>";
                            echo "<td>#" . $row['id'] . "</td>";
                            echo "<td><strong>" . htmlspecialchars($row['name']) . "</strong></td>";
                            echo "<td>" . htmlspecialchars($row['email']) . "</td>";
                            echo "<td><span class='badge rounded-pill " . $badgeClass . "'>" . strtoupper($row['role']) . "</span></td>";
                            echo "<td><i class='fas fa-circle text-success small'></i> Active</td>";
                            echo "</tr>";
                        }
                    } else {
                        echo "<tr><td colspan='5' class='text-center'>No users found in database.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>

        <div class="mt-3 text-white-50 small">
            <i class="fas fa-info-circle me-1"></i> Total Personnel: <strong><?php echo $result->num_rows; ?></strong>
        </div>

    </div>
</div>

</body>
</html>