<?php
session_start();
require_once '../koneksi.php';
require_once '../auth.php';
function cekAdmin() {
    if ($_SESSION['role'] !== 'admin') {
        echo "<script>alert('Akses ditolak! Anda bukan admin.'); window.location='../index.php';</script>";
        exit();
    }
}
include 'layout_header.php';
?>

<div class="container-fluid">
    <h3>Manajemen Pengguna</h3>
    <a href="tambah_user.php" class="btn btn-primary mb-3"><i class="fas fa-plus"></i> Tambah User Baru</a>
    
    <div class="card shadow p-4">
        <table class="table table-bordered table-striped">
            <thead class="table-dark">
                <tr>
                    <th>No</th>
                    <th>Username</th>
                    <th>Role</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $no = 1;
                $q = mysqli_query($koneksi, "SELECT * FROM users");
                while($u = mysqli_fetch_array($q)) {
                    echo "<tr>
                            <td>{$no}</td>
                            <td>{$u['username']}</td>
                            <td>{$u['role']}</td>
                            <td>
                                <a href='edit_user.php?id={$u['id_user']}' class='btn btn-warning btn-sm'>Edit</a>
                                <a href='hapus_user.php?id={$u['id_user']}' class='btn btn-danger btn-sm' onclick='return confirm(\"Yakin ingin hapus?\")'>Hapus</a>
                            </td>
                          </tr>";
                    $no++;
                }
                ?>
            </tbody>
        </table>
    </div>
</div>
</body>
</html>