<?php
session_start();
if (!isset($_SESSION['id_admin'])) {
    header('Location: login_admin.php');
    exit;
}

include 'includes/config.php';
include 'includes/header_admin.php'; 
include 'includes/sidebar_admin.php';

// Dapatkan semua admin
$sql = "SELECT id, nama, no_ic, jawatan, no_telefon, unit_jabatan, jenis_admin FROM admin ORDER BY jenis_admin DESC, nama ASC";
$result = mysqli_query($conn, $sql);
?>

<div style="margin-left:200px; padding:100px 20px;">
    <h2>Senarai Admin</h2>
    <a href="daftar_admin.php" class="btn btn-primary mb-3">Daftar Admin Baru</a>

    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Nama</th>
                <th>No. Kad Pengenalan</th>
                <th>Jawatan</th>
                <th>No. Telefon</th>
                <th>Unit / Jabatan</th>
                <th>Jenis Admin</th>
                <th>Tindakan</th>
            </tr>
        </thead>
        <tbody>
            <?php if ($result && mysqli_num_rows($result) > 0): ?>
                <?php while ($admin = mysqli_fetch_assoc($result)): ?>
                    <tr>
                        <td><?= htmlspecialchars($admin['nama']) ?></td>
                        <td><?= htmlspecialchars($admin['no_ic']) ?></td>
                        <td><?= htmlspecialchars($admin['jawatan']) ?></td>
                        <td><?= htmlspecialchars($admin['no_telefon']) ?></td>
                        <td><?= htmlspecialchars($admin['unit_jabatan']) ?></td>
                        <td><?= ($admin['jenis_admin'] == 'utama') ? '<strong>Admin Utama</strong>' : 'Admin' ?></td>
                        <td>
                            <a href="edit_admin.php?id=<?= $admin['id'] ?>" class="btn btn-sm btn-warning">Edit</a>
                            <?php if ($admin['jenis_admin'] != 'utama'): ?>
                            <a href="padam_admin.php?id=<?= $admin['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Padam admin ini?')">Padam</a>
                            <?php else: ?>
                            <em>Tidak boleh padam</em>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="7">Tiada admin didaftarkan.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

<?php include 'includes/footer.php'; ?>
