<?php
include('session_handler.php'); // Sudah uruskan sesi

// Pastikan hanya Sadmin boleh akses halaman ini
if (!checkRole('superadmin')) {
    header('Location: unauthorized.php');
    exit;
}

// Fetch data from subkategori table
$sql = "SELECT id, NamaStaff, NomborStaff, email
        FROM users 
        ORDER BY id ASC";

// Prepare the SQL query
$stmt = $conn->prepare($sql);

// Check if the statement was prepared successfully
if ($stmt === false) {
    die("Error preparing statement: " . $conn->error);
}

$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<!-- header part -->
<?php
include('header.php');
?>
<!-- end header -->
<body class="skin-blue fixed-layout">
    <div class="preloader">
        <div class="loader">
            <div class="loader__figure"></div>
            <p class="loader__label">SPNR Surat/Memo</p>
        </div>
    </div>

    <div id="main-wrapper">
        <!-- Topbar header -->
        <?php
        include('topbar.php');
        ?>
        <!-- Left Sidebar -->
        <?php
        include('leftsidebar.php');
        ?>
        
        <!-- Page wrapper -->
        <div class="page-wrapper">
            <!-- Container fluid -->
            <div class="container-fluid">
                <div class="row page-titles">
                    <div class="col-md-5 align-self-center">
                        <h4 class="text-themecolor">Senarai Pengguna</h4>
                    </div>
                    <div class="col-md-7 align-self-center text-end">
                        <div class="d-flex justify-content-end align-items-center">
                            <ol class="breadcrumb justify-content-end">
                                <li class="breadcrumb-item"><a href="javascript:void(0)">Home</a></li>
                                <li class="breadcrumb-item active">Senarai Pengguna</li>
                            </ol>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">Senarai Pengguna</h4>
                                <h6 class="card-subtitle">Export data to Copy, CSV, Excel, PDF & Print</h6>
                                <div class="table-responsive m-t-40">
                                    <table id="example23"
                                           class="display nowrap table table-hover table-striped border" cellspacing="0" width="100%">
                                    <thead>
                                        <tr>
                                            <th>Nama Staf</th>
                                            <th>Nombor Staf</th>
                                            <th>Email</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    <?php if ($result->num_rows > 0): ?>
                                    <?php $row_number = 1; ?>
                                    <?php while ($row = $result->fetch_assoc()): ?>
                                        <tr>
                                        <td><?php echo htmlspecialchars($row['NamaStaff']); ?></td>
                                        <td><?php echo htmlspecialchars($row['NomborStaff']); ?></td>
                                        <td><?php echo htmlspecialchars($row['email']); ?></td>
                                        <td>
                                        <div class="d-flex flex-wrap gap-1">
                                            <a href="edit_user.php?id=<?php echo $row['id']; ?>" class="btn btn-success btn-sm">Kemaskini User</a>
                                            <a href="delete_user.php?id=<?php echo $row['id']; ?>"
                                               onclick="return confirm('Adakah anda pasti mahu padam pengguna ini?');"
                                               class="btn btn-success btn-sm">Delete User</a>
                                            <a href="reset_pass.php?id=<?php echo $row['id']; ?>" class="btn btn-success btn-sm">Reset Password</a>
                                        </div>
                                    </td>

                                    </tr>


                                        <?php endwhile; ?>
                                        <?php else: ?>
                                        <tr><td colspan="8">Tiada data</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
        
        <!-- footer -->
        <?php
        include('footer.php');
        ?>
    </div>
    
    <!-- All Jquery -->
    <?php
    include('jquery.php');
    ?>
</body>
</html>