<?php
session_start();
require_once '../includes/config.php';
require_once '../includes/functions.php';

// Cek apakah user sudah login dan memiliki akses admin
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'admin_soal') {
    header('Location: ../login.php');
    exit;
}

// Tentukan nama tabel yang digunakan
$table_users = 'users_preview'; // Ganti dengan 'users' jika ingin menggunakan tabel asli

// Handle aktivasi/nonaktifkan user
if (isset($_POST['toggle_status'])) {
    $user_id = $_POST['user_id'];
    $current_status = $_POST['current_status'];
    $new_status = $current_status == 'active' ? 'inactive' : 'active';
    $action_text = $new_status == 'active' ? 'diaktifkan' : 'dinonaktifkan';
    
    try {
        $stmt = $pdo->prepare("UPDATE $table_users SET status = ? WHERE id = ?");
        $stmt->execute([$new_status, $user_id]);
        $_SESSION['success_message'] = "User berhasil $action_text";
    } catch (PDOException $e) {
        $_SESSION['error_message'] = "Gagal mengubah status user: " . $e->getMessage();
    }
    
    header('Location: users.php');
    exit;
}

// Handle reset password
if (isset($_POST['reset_password'])) {
    $user_id = $_POST['user_id'];
    $default_password = password_hash('password123', PASSWORD_DEFAULT);
    
    try {
        $stmt = $pdo->prepare("UPDATE $table_users SET password = ? WHERE id = ?");
        $stmt->execute([$default_password, $user_id]);
        $_SESSION['success_message'] = "Password berhasil direset ke 'password123'";
    } catch (PDOException $e) {
        $_SESSION['error_message'] = "Gagal reset password: " . $e->getMessage();
    }
    
    header('Location: users.php');
    exit;
}

// Handle update role
if (isset($_POST['update_role'])) {
    $user_id = $_POST['user_id'];
    $new_role = $_POST['new_role'];
    
    try {
        $stmt = $pdo->prepare("UPDATE $table_users SET role = ? WHERE id = ?");
        $stmt->execute([$new_role, $user_id]);
        $_SESSION['success_message'] = "Role user berhasil diperbarui!";
    } catch (PDOException $e) {
        $_SESSION['error_message'] = "Gagal mengupdate role: " . $e->getMessage();
    }
    
    header('Location: users.php');
    exit;
}

// Handle hapus user
if (isset($_POST['delete_user'])) {
    $user_id = $_POST['user_id'];
    $username = $_POST['username'];
    
    try {
        if ($user_id == $_SESSION['user_id']) {
            $_SESSION['error_message'] = "Tidak dapat menghapus akun sendiri";
        } else {
            $stmt = $pdo->prepare("DELETE FROM $table_users WHERE id = ?");
            $stmt->execute([$user_id]);
            $_SESSION['success_message'] = "User {$username} berhasil dihapus";
        }
    } catch (PDOException $e) {
        $_SESSION['error_message'] = "Gagal menghapus user: " . $e->getMessage();
    }
    
    header('Location: users.php');
    exit;
}

// Ambil data users dari database
$users = [];
$error = '';

try {
    $query = "SELECT id, username, role, nama_lengkap, status, email, penguji_id, created_at 
              FROM $table_users 
              ORDER BY created_at DESC";
    $stmt = $pdo->query($query);
    $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $error = 'Gagal mengambil data users: ' . $e->getMessage();
    error_log("Database error: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kelola Users - SOCA OSCE System</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* ===== RESET & BASE ===== */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #d4d8dc;
            display: flex;
            min-height: 100vh;
        }

        /* ===== SIDEBAR ===== */
        .sidebar {
            width: 260px;
            background: linear-gradient(180deg, #1a2332 0%, #0f1724 100%);
            color: #fff;
            padding: 0;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
            z-index: 100;
            transition: all 0.3s ease;
            box-shadow: 4px 0 20px rgba(0,0,0,0.1);
        }

        .sidebar::-webkit-scrollbar {
            width: 4px;
        }

        .sidebar::-webkit-scrollbar-track {
            background: transparent;
        }

        .sidebar::-webkit-scrollbar-thumb {
            background: rgba(255,255,255,0.2);
            border-radius: 10px;
        }

        .sidebar-header {
            padding: 25px 20px 20px;
            border-bottom: 1px solid rgba(255,255,255,0.08);
            text-align: center;
        }

        .sidebar-header h2 {
            font-size: 20px;
            font-weight: 700;
            letter-spacing: 1px;
            background: linear-gradient(135deg, #60a5fa, #a78bfa);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .sidebar-header p {
            font-size: 12px;
            color: rgba(255,255,255,0.5);
            margin-top: 4px;
            letter-spacing: 2px;
            text-transform: uppercase;
        }

        .sidebar-menu {
            list-style: none;
            padding: 15px 0;
        }

        .sidebar-menu li {
            margin: 2px 12px;
        }

        .sidebar-menu li a {
            display: flex;
            align-items: center;
            gap: 14px;
            padding: 12px 16px;
            color: rgba(255,255,255,0.6);
            text-decoration: none;
            border-radius: 12px;
            transition: all 0.3s ease;
            font-size: 14px;
            font-weight: 500;
        }

        .sidebar-menu li a i {
            width: 20px;
            font-size: 16px;
            text-align: center;
        }

        .sidebar-menu li a:hover {
            background: rgba(255,255,255,0.08);
            color: #fff;
            transform: translateX(4px);
        }

        .sidebar-menu li a.active {
            background: linear-gradient(135deg, rgba(96,165,250,0.2), rgba(167,139,250,0.2));
            color: #60a5fa;
            box-shadow: inset 3px 0 0 #60a5fa;
        }

        .sidebar-menu .menu-divider {
            height: 1px;
            background: rgba(255,255,255,0.06);
            margin: 12px 20px;
        }

        .sidebar-menu .menu-label {
            padding: 10px 20px 6px;
            font-size: 10px;
            text-transform: uppercase;
            letter-spacing: 1.5px;
            color: rgba(255,255,255,0.25);
            font-weight: 600;
        }

        /* ===== MAIN CONTENT ===== */
        .main-content {
            margin-left: 260px;
            flex: 1;
            padding: 30px 35px 40px;
            min-height: 100vh;
        }

        /* ===== HEADER ===== */
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            flex-wrap: wrap;
            gap: 15px;
        }

        .header-left h1 {
            font-size: 28px;
            font-weight: 700;
            color: #1a2332;
            letter-spacing: -0.5px;
        }

        .header-left .subtitle {
            font-size: 14px;
            color: #6b7a8f;
            margin-top: 4px;
        }

        .header-right {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .header-right .user-badge {
            display: flex;
            align-items: center;
            gap: 10px;
            background: #fff;
            padding: 8px 16px 8px 12px;
            border-radius: 50px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.06);
        }

        .header-right .user-badge .avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background: linear-gradient(135deg, #60a5fa, #a78bfa);
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            font-weight: 600;
            font-size: 16px;
        }

        .header-right .user-badge .user-name {
            font-weight: 600;
            font-size: 14px;
            color: #1a2332;
        }

        .header-right .user-badge .user-role {
            font-size: 12px;
            color: #6b7a8f;
        }

        .btn-logout {
            background: #fee2e2;
            color: #dc2626;
            border: none;
            padding: 8px 16px;
            border-radius: 50px;
            font-size: 13px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .btn-logout:hover {
            background: #fecaca;
            transform: scale(1.02);
        }

        /* ===== ALERT ===== */
        .alert {
            padding: 16px 20px;
            border-radius: 12px;
            margin-bottom: 20px;
            font-weight: 500;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .alert-success {
            background: #d1fae5;
            color: #065f46;
            border: 1px solid #a7f3d0;
        }

        .alert-error {
            background: #fee2e2;
            color: #991b1b;
            border: 1px solid #fca5a5;
        }

        .alert-warning {
            background: #fef3c7;
            color: #92400e;
            border: 1px solid #fcd34d;
        }

        .alert i {
            font-size: 18px;
        }

        /* ===== USERS TABLE ===== */
        .users-card {
            background: #fff;
            border-radius: 16px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.05);
            border: 1px solid rgba(0,0,0,0.04);
            overflow: hidden;
        }

        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20px 24px;
            border-bottom: 1px solid #f1f5f9;
            flex-wrap: wrap;
            gap: 10px;
        }

        .card-header h2 {
            font-size: 18px;
            font-weight: 700;
            color: #1a2332;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .card-header h2 i {
            color: #60a5fa;
        }

        .card-header .badge-total {
            padding: 4px 14px;
            background: #f1f5f9;
            border-radius: 50px;
            font-size: 13px;
            color: #475569;
            font-weight: 500;
        }

        .table-wrapper {
            overflow-x: auto;
            padding: 0 4px;
        }

        .data-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 13px;
        }

        .data-table thead {
            background: #f8fafc;
        }

        .data-table th {
            padding: 12px 16px;
            text-align: left;
            font-weight: 600;
            color: #475569;
            border-bottom: 2px solid #e2e8f0;
            white-space: nowrap;
        }

        .data-table td {
            padding: 12px 16px;
            border-bottom: 1px solid #f1f5f9;
            color: #1a2332;
            vertical-align: middle;
        }

        .data-table tbody tr {
            transition: all 0.2s ease;
        }

        .data-table tbody tr:hover {
            background: #f8fafc;
        }

        .data-table tbody tr.current-user {
            background: #eff6ff;
        }

        /* ===== BADGES ===== */
        .badge {
            display: inline-block;
            padding: 3px 10px;
            border-radius: 50px;
            font-size: 11px;
            font-weight: 600;
        }

        .badge-role-admin {
            background: #fee2e2;
            color: #dc2626;
        }

        .badge-role-admin_soal {
            background: #fef3c7;
            color: #d97706;
        }

        .badge-role-penguji {
            background: #dbeafe;
            color: #2563eb;
        }

        .badge-role-reviewer {
            background: #e0e7ff;
            color: #4f46e5;
        }

        .badge-role-user {
            background: #e2e8f0;
            color: #475569;
        }

        .badge-status-active {
            background: #d1fae5;
            color: #059669;
        }

        .badge-status-inactive {
            background: #fee2e2;
            color: #dc2626;
        }

        .badge-you {
            background: #dbeafe;
            color: #2563eb;
            font-size: 10px;
            padding: 2px 8px;
            margin-left: 6px;
        }

        /* ===== ACTION BUTTONS ===== */
        .action-buttons {
            display: flex;
            gap: 4px;
            flex-wrap: wrap;
        }

        .btn-action {
            padding: 4px 10px;
            border-radius: 6px;
            font-size: 12px;
            text-decoration: none;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 4px;
            border: none;
            cursor: pointer;
        }

        .btn-action:hover {
            transform: translateY(-1px);
        }

        .btn-action:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            transform: none;
        }

        .btn-toggle-active {
            background: #d1fae5;
            color: #059669;
        }

        .btn-toggle-active:hover:not(:disabled) {
            background: #a7f3d0;
        }

        .btn-toggle-inactive {
            background: #fee2e2;
            color: #dc2626;
        }

        .btn-toggle-inactive:hover:not(:disabled) {
            background: #fecaca;
        }

        .btn-reset {
            background: #fef3c7;
            color: #d97706;
        }

        .btn-reset:hover:not(:disabled) {
            background: #fde68a;
        }

        .btn-delete {
            background: #fee2e2;
            color: #dc2626;
        }

        .btn-delete:hover:not(:disabled) {
            background: #fecaca;
        }

        .btn-role {
            background: #eff6ff;
            color: #2563eb;
        }

        .btn-role:hover:not(:disabled) {
            background: #dbeafe;
        }

        /* ===== ROLE SELECT ===== */
        .role-select {
            padding: 2px 6px;
            border: 1px solid #e2e8f0;
            border-radius: 4px;
            font-size: 12px;
            background: #fff;
            color: #1a2332;
            font-family: inherit;
        }

        .role-select:focus {
            outline: none;
            border-color: #60a5fa;
        }

        .role-form {
            display: inline-flex;
            gap: 4px;
            align-items: center;
        }

        /* ===== EMPTY STATE ===== */
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #6b7a8f;
        }

        .empty-state i {
            font-size: 56px;
            color: #d1d5db;
            margin-bottom: 16px;
            display: block;
        }

        .empty-state h3 {
            font-size: 18px;
            color: #1a2332;
            margin-bottom: 6px;
        }

        /* ===== INFO CARD ===== */
        .info-card {
            margin-top: 25px;
            background: #eff6ff;
            border-radius: 16px;
            padding: 20px 24px;
            border-left: 4px solid #60a5fa;
        }

        .info-card h3 {
            font-size: 16px;
            font-weight: 600;
            color: #1a2332;
            margin-bottom: 12px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .info-card h3 i {
            color: #60a5fa;
        }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
            gap: 16px;
        }

        .info-item {
            background: #fff;
            padding: 12px 16px;
            border-radius: 8px;
            border: 1px solid #bfdbfe;
        }

        .info-item strong {
            display: block;
            font-size: 13px;
            color: #1a2332;
            margin-bottom: 4px;
        }

        .info-item ul {
            list-style: none;
            padding: 0;
            margin: 0;
            font-size: 13px;
            color: #475569;
        }

        .info-item ul li {
            padding: 2px 0;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .info-item ul li i {
            color: #60a5fa;
            font-size: 12px;
        }

        .info-item .badge {
            font-size: 10px;
        }

        /* ===== RESPONSIVE ===== */
        @media (max-width: 992px) {
            .sidebar {
                width: 220px;
            }
            .main-content {
                margin-left: 220px;
                padding: 20px;
            }
        }

        @media (max-width: 768px) {
            .sidebar {
                width: 0;
                transform: translateX(-100%);
            }
            .sidebar.open {
                width: 280px;
                transform: translateX(0);
            }
            .main-content {
                margin-left: 0;
                padding: 15px;
            }
            .header {
                flex-direction: column;
                align-items: flex-start;
            }
            .data-table {
                font-size: 12px;
            }
            .data-table th,
            .data-table td {
                padding: 8px 10px;
            }
            .action-buttons {
                flex-direction: column;
                gap: 3px;
            }
            .info-grid {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 480px) {
            .header-left h1 {
                font-size: 22px;
            }
            .card-header {
                flex-direction: column;
                align-items: flex-start;
            }
            .role-form {
                flex-direction: column;
                align-items: stretch;
            }
        }

        .hamburger {
            display: none;
            background: none;
            border: none;
            font-size: 24px;
            color: #1a2332;
            cursor: pointer;
            padding: 8px;
            margin-bottom: 10px;
        }

        @media (max-width: 768px) {
            .hamburger {
                display: block;
            }
        }

        .sidebar-overlay {
            display: none;
            position: fixed;
            inset: 0;
            background: rgba(0,0,0,0.4);
            z-index: 99;
        }

        .sidebar-overlay.active {
            display: block;
        }

        .scroll-top {
            position: fixed;
            bottom: 30px;
            right: 30px;
            background: #2563eb;
            color: #fff;
            width: 44px;
            height: 44px;
            border-radius: 50%;
            border: none;
            cursor: pointer;
            font-size: 18px;
            box-shadow: 0 4px 16px rgba(37,99,235,0.3);
            transition: all 0.3s ease;
            opacity: 0;
            visibility: hidden;
            z-index: 50;
        }

        .scroll-top.visible {
            opacity: 1;
            visibility: visible;
        }

        .scroll-top:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 24px rgba(37,99,235,0.4);
        }
    </style>
</head>
<body>
    <!-- Overlay untuk mobile -->
    <div class="sidebar-overlay" id="sidebarOverlay"></div>

    <!-- Sidebar -->
    <div class="sidebar" id="sidebar">
        <div class="sidebar-header">
            <h2>SOCA/OSCE</h2>
            <p>Admin Panel</p>
        </div>
        <ul class="sidebar-menu">
            <li class="menu-label">Main Menu</li>
            <li><a href="dashboard.php"><i class="fas fa-th-large"></i> Dashboard</a></li>
            
            <li class="menu-divider"></li>
            <li class="menu-label">Soal Teori S1</li>
            <li><a href="bank_soal_teori_s1.php"><i class="fas fa-plus-circle"></i> Tambah Soal</a></li>
            <li><a href="list_soal_teori_s1.php"><i class="fas fa-list"></i> Daftar Soal</a></li>
            
            <li class="menu-divider"></li>
            <li class="menu-label">Soal Teori</li>
            <li><a href="bank_soal_teori.php"><i class="fas fa-plus-circle"></i> Tambah Soal</a></li>
            <li><a href="list_soal_teori.php"><i class="fas fa-list"></i> Daftar Soal</a></li>
            
            <li class="menu-divider"></li>
            <li class="menu-label">Soal Praktik</li>
            <li><a href="bank_soal_praktik.php"><i class="fas fa-plus-circle"></i> Tambah Soal</a></li>
            <li><a href="list_soal_praktik.php"><i class="fas fa-list"></i> Daftar Soal</a></li>
            
            <li class="menu-divider"></li>
            <li class="menu-label">Lainnya</li>
            <li><a href="aspek_penilaian.php"><i class="fas fa-clipboard-check"></i> Aspek Penilaian</a></li>
            <li><a href="preview_aspek_penilaian.php"><i class="fas fa-eye"></i> Preview Aspek</a></li>
            <li><a href="users.php" class="active"><i class="fas fa-users"></i> Kelola Users</a></li>
            <li><a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
        </ul>
    </div>

    <!-- Main Content -->
    <div class="main-content">
        <!-- Header -->
        <div class="header">
            <div class="header-left">
                <button class="hamburger" id="hamburgerBtn">
                    <i class="fas fa-bars"></i>
                </button>
                <h1>Kelola Users</h1>
                <div class="subtitle">Kelola pengguna yang memiliki akses ke sistem</div>
            </div>
            <div class="header-right">
                <div class="user-badge">
                    <div class="avatar">
                        <?php echo strtoupper(substr($_SESSION['user_name'] ?? 'A', 0, 1)); ?>
                    </div>
                    <div>
                        <div class="user-name"><?php echo $_SESSION['user_name'] ?? 'Admin'; ?></div>
                        <div class="user-role">Admin Soal</div>
                    </div>
                </div>
                <a href="../logout.php" class="btn-logout">
                    <i class="fas fa-sign-out-alt"></i> Logout
                </a>
            </div>
        </div>

        <!-- Alert -->
        <?php if (isset($_SESSION['success_message'])): ?>
            <div class="alert alert-success">
                <i class="fas fa-check-circle"></i>
                <?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?>
            </div>
        <?php endif; ?>
        
        <?php if (isset($_SESSION['error_message'])): ?>
            <div class="alert alert-error">
                <i class="fas fa-exclamation-circle"></i>
                <?php echo $_SESSION['error_message']; unset($_SESSION['error_message']); ?>
            </div>
        <?php endif; ?>
        
        <?php if (!empty($error)): ?>
            <div class="alert alert-error">
                <i class="fas fa-exclamation-circle"></i>
                <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <!-- Users Table -->
        <div class="users-card">
            <div class="card-header">
                <h2>
                    <i class="fas fa-users"></i>
                    Daftar Users
                </h2>
                <span class="badge-total">
                    <i class="fas fa-user"></i> Total: <?php echo count($users); ?> users
                </span>
            </div>

            <div class="table-wrapper">
                <?php if (empty($users)): ?>
                    <div class="empty-state">
                        <i class="fas fa-users"></i>
                        <h3>Belum ada user terdaftar</h3>
                        <p>Data users tidak ditemukan di database.</p>
                    </div>
                <?php else: ?>
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>Username</th>
                                <th>Nama Lengkap</th>
                                <th>Role</th>
                                <th>Status</th>
                                <th>Email</th>
                                <th>Tanggal Dibuat</th>
                                <th>Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($users as $user): 
                                $is_current_user = ($user['id'] == $_SESSION['user_id']);
                            ?>
                                <tr class="<?php echo $is_current_user ? 'current-user' : ''; ?>">
                                    <td>
                                        <?php echo htmlspecialchars($user['username']); ?>
                                        <?php if ($is_current_user): ?>
                                            <span class="badge badge-you"><i class="fas fa-user-check"></i> Anda</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo htmlspecialchars($user['nama_lengkap']); ?></td>
                                    <td>
                                        <span class="badge badge-role-<?php echo htmlspecialchars($user['role']); ?>">
                                            <?php 
                                            $role_labels = [
                                                'admin' => 'Admin',
                                                'admin_soal' => 'Admin Soal',
                                                'penguji' => 'Penguji',
                                                'reviewer' => 'Reviewer',
                                                'user' => 'User'
                                            ];
                                            echo $role_labels[$user['role']] ?? $user['role']; 
                                            ?>
                                        </span>
                                        <?php if ($user['role'] == 'reviewer'): ?>
                                            <span style="font-size:10px; color:#4f46e5; display:block;">
                                                <i class="fas fa-check-circle"></i> Dapat mereview
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <?php if (isset($user['status'])): ?>
                                            <span class="badge badge-status-<?php echo $user['status']; ?>">
                                                <i class="fas fa-<?php echo $user['status'] == 'active' ? 'circle' : 'times-circle'; ?>"></i>
                                                <?php echo $user['status'] == 'active' ? 'Aktif' : 'Nonaktif'; ?>
                                            </span>
                                        <?php else: ?>
                                            <span class="badge badge-na">N/A</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><?php echo htmlspecialchars($user['email'] ?? '-'); ?></td>
                                    <td><?php echo date('d/m/Y', strtotime($user['created_at'])); ?></td>
                                    <td>
                                        <div class="action-buttons">
                                            <!-- Update Role -->
                                            <form method="POST" class="role-form">
                                                <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                                                <select name="new_role" class="role-select" <?php echo $is_current_user ? 'disabled' : ''; ?>>
                                                    <option value="admin" <?php echo $user['role'] == 'admin' ? 'selected' : ''; ?>>Admin</option>
                                                    <option value="admin_soal" <?php echo $user['role'] == 'admin_soal' ? 'selected' : ''; ?>>Admin Soal</option>
                                                    <option value="penguji" <?php echo $user['role'] == 'penguji' ? 'selected' : ''; ?>>Penguji</option>
                                                    <option value="reviewer" <?php echo $user['role'] == 'reviewer' ? 'selected' : ''; ?>>Reviewer</option>
                                                    <option value="user" <?php echo $user['role'] == 'user' ? 'selected' : ''; ?>>User</option>
                                                </select>
                                                <button type="submit" name="update_role" 
                                                        class="btn-action btn-role"
                                                        <?php echo $is_current_user ? 'disabled' : ''; ?>
                                                        title="Update role user">
                                                    <i class="fas fa-sync"></i>
                                                </button>
                                            </form>

                                            <!-- Toggle Status -->
                                            <?php if (isset($user['status'])): ?>
                                                <form method="POST" style="display:inline;">
                                                    <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                                                    <input type="hidden" name="current_status" value="<?php echo $user['status']; ?>">
                                                    <button type="submit" name="toggle_status" 
                                                            class="btn-action <?php echo $user['status'] == 'active' ? 'btn-toggle-inactive' : 'btn-toggle-active'; ?>"
                                                            <?php echo $is_current_user ? 'disabled' : ''; ?>
                                                            title="<?php echo $user['status'] == 'active' ? 'Nonaktifkan user' : 'Aktifkan user'; ?>"
                                                            onclick="return confirm('<?php echo $user['status'] == 'active' ? 'Nonaktifkan' : 'Aktifkan'; ?> user <?php echo htmlspecialchars($user['username']); ?>?')">
                                                        <i class="fas fa-<?php echo $user['status'] == 'active' ? 'ban' : 'check-circle'; ?>"></i>
                                                    </button>
                                                </form>
                                            <?php endif; ?>
                                            
                                            <!-- Reset Password -->
                                            <form method="POST" style="display:inline;">
                                                <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                                                <button type="submit" name="reset_password" 
                                                        class="btn-action btn-reset"
                                                        <?php echo $is_current_user ? 'disabled' : ''; ?>
                                                        title="Reset password ke default"
                                                        onclick="return confirm('Reset password user <?php echo htmlspecialchars($user['username']); ?>? Password akan direset ke: password123')">
                                                    <i class="fas fa-key"></i>
                                                </button>
                                            </form>
                                            
                                            <!-- Hapus User -->
                                            <form method="POST" style="display:inline;">
                                                <input type="hidden" name="user_id" value="<?php echo $user['id']; ?>">
                                                <input type="hidden" name="username" value="<?php echo htmlspecialchars($user['username']); ?>">
                                                <button type="submit" name="delete_user" 
                                                        class="btn-action btn-delete"
                                                        <?php echo $is_current_user ? 'disabled' : ''; ?>
                                                        title="Hapus user"
                                                        onclick="return confirm('Hapus user <?php echo htmlspecialchars($user['username']); ?>? Tindakan ini tidak dapat dibatalkan!')">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
            </div>
        </div>

        <!-- Info Card -->
        <div class="info-card">
            <h3><i class="fas fa-info-circle"></i> Informasi Role & Fitur</h3>
            <div class="info-grid">
                <div class="info-item">
                    <strong><i class="fas fa-user-shield"></i> Role</strong>
                    <ul>
                        <li><span class="badge badge-role-admin">Admin</span> - Akses penuh</li>
                        <li><span class="badge badge-role-admin_soal">Admin Soal</span> - Kelola soal</li>
                        <li><span class="badge badge-role-penguji">Penguji</span> - Ujian praktik</li>
                        <li><span class="badge badge-role-reviewer">Reviewer</span> - Review soal <i class="fas fa-check-circle" style="color:#4f46e5;"></i></li>
                        <li><span class="badge badge-role-user">User</span> - Akses dasar</li>
                    </ul>
                </div>
                <div class="info-item">
                    <strong><i class="fas fa-toggle-on"></i> Fitur</strong>
                    <ul>
                        <li><i class="fas fa-check-circle" style="color:#059669;"></i> Aktif/Nonaktifkan user</li>
                        <li><i class="fas fa-undo"></i> Reset password</li>
                        <li><i class="fas fa-sync"></i> Update role</li>
                        <li><i class="fas fa-trash"></i> Hapus user</li>
                        <li><i class="fas fa-lock"></i> Proteksi akun sendiri</li>
                    </ul>
                </div>
                <div class="info-item">
                    <strong><i class="fas fa-star"></i> Reviewer</strong>
                    <ul>
                        <li><i class="fas fa-check"></i> Dapat mereview soal teori & praktik</li>
                        <li><i class="fas fa-check"></i> Memberikan status review</li>
                        <li><i class="fas fa-check"></i> Memberikan catatan revisi</li>
                        <li><i class="fas fa-check"></i> Melihat riwayat review</li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <!-- Scroll to Top -->
    <button class="scroll-top" id="scrollTopBtn" title="Kembali ke atas">
        <i class="fas fa-arrow-up"></i>
    </button>

    <script>
        // ===== SIDEBAR TOGGLE =====
        const hamburger = document.getElementById('hamburgerBtn');
        const sidebar = document.getElementById('sidebar');
        const overlay = document.getElementById('sidebarOverlay');

        function toggleSidebar() {
            sidebar.classList.toggle('open');
            overlay.classList.toggle('active');
        }

        hamburger.addEventListener('click', toggleSidebar);
        overlay.addEventListener('click', toggleSidebar);

        // ===== CLOSE SIDEBAR ON RESIZE =====
        window.addEventListener('resize', function() {
            if (window.innerWidth > 768) {
                sidebar.classList.remove('open');
                overlay.classList.remove('active');
            }
        });

        // ===== SCROLL TO TOP =====
        const scrollBtn = document.getElementById('scrollTopBtn');
        window.addEventListener('scroll', function() {
            if (window.scrollY > 300) {
                scrollBtn.classList.add('visible');
            } else {
                scrollBtn.classList.remove('visible');
            }
        });

        scrollBtn.addEventListener('click', function() {
            window.scrollTo({ top: 0, behavior: 'smooth' });
        });
    </script>
</body>
</html>