<?php
// 1. KONEKSI DATABASE
$koneksi = mysqli_connect("localhost", "root", "", "db_rpl2526");

if (!$koneksi) {
    die("Koneksi gagal : " . mysqli_connect_error());
}

// Query untuk data Admin
$query_adminn = mysqli_query($koneksi, "
    SELECT 
        tbl_admin.id_admin,
        tbl_admin.id_user,
        tbl_admin.nama_admin,
        tbl_admin.telepon_admin,
        tbl_admin.alamat_admin,
        tbl_admin.jenis_kelamin,
        tbl_admin.path_photo_admin
    FROM tbl_admin
    INNER JOIN tbl_users 
        ON tbl_admin.id_user = tbl_users.id_user
") or die("Query admin gagal: " . mysqli_error($koneksi));
?>

<!-- ================= TABEL DATA ADMIN ================= -->
<div class="container-fluid py-4 px-0">
    <div class="card shadow-sm border" style="border-radius: 8px; border-color: #e1e4e8 !important; background-color: #ffffff;">
        <div class="card-body" style="padding: 24px;">

            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="card-title fw-bold text-dark mb-0" style="font-size: 18px;">Users Data Admin</h5>
                <!-- Tombol Tambah Admin -->
                <a href="index.php?pages=admin&aksi=tambah" class="btn btn-primary">+ Tambah Admin</a>
            </div>

            <div style="width: 100%; overflow-x: auto;">
                <table class="table align-middle mb-0 w-100" id="userTable" style="border-collapse: collapse; width: 100%;">
                    <thead style="background-color: #1f242d; color: white;">
                        <tr>
                            <th class="py-3 px-3" style="font-size: 12px; font-weight: 600; background-color: #1f242d; color: white; border: none; text-align: center;">NO</th>
                            <th class="py-3 px-3" style="font-size: 12px; font-weight: 600; background-color: #1f242d; color: white; border: none; text-align: center;">PHOTO</th>
                            <th class="py-3 px-3" style="font-size: 12px; font-weight: 600; background-color: #1f242d; color: white; border: none; text-align: center;">NAMA ADMIN</th>
                            <th class="py-3 px-3" style="font-size: 12px; font-weight: 600; background-color: #1f242d; color: white; border: none; text-align: center;">TELEPON</th>
                            <th class="py-3 px-3" style="font-size: 12px; font-weight: 600; background-color: #1f242d; color: white; border: none; text-align: center;">ALAMAT</th>
                            <th class="py-3 px-3" style="font-size: 12px; font-weight: 600; background-color: #1f242d; color: white; border: none; text-align: center;">JENIS KELAMIN</th>
                            <th class="py-3 px-3" style="font-size: 12px; font-weight: 600; background-color: #1f242d; color: white; border: none; text-align: center;">ID USER</th>
                            <th class="py-3 px-3" style="font-size: 12px; font-weight: 600; background-color: #1f242d; color: white; border: none; text-align: center;">ACTION</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if (mysqli_num_rows($query_adminn) > 0) {
                            $no_a = 1;
                            while ($data = mysqli_fetch_assoc($query_adminn)) {
                        ?>
                        <tr style="border-bottom: 1px solid #f3f4f6;">
                            <td class="py-3 px-3" style="font-size: 14px; color: #374151; text-align: center;">
                                <?= $no_a++; ?>
                            </td>
                            <td class="py-3 px-3" style="font-size: 14px; color: #374151; text-align: center;">
                                <?php
                                $photo = trim($data['path_photo_admin'] ?? '');
                                $filename = basename($photo);

                                // Jalur pengecekan fisik file di server
                                $path_uploads = __DIR__ . '/../uploads/admin/' . $filename;
                                $path_images  = __DIR__ . '/../images_all/' . $filename;

                                // Pengecekan lokasi foto
                                if (!empty($filename) && file_exists($path_uploads)) {
                                    $img_src = '../uploads/admin/' . rawurlencode($filename);
                                } elseif (!empty($filename) && file_exists($path_images)) {
                                    $img_src = '../images_all/' . rawurlencode($filename);
                                } else {
                                    $img_src = 'https://ui-avatars.com/api/?name='
                                        . urlencode($data['nama_admin'] ?? 'Admin')
                                        . '&background=0D9488&color=fff';
                                }
                                ?>
                                <img src="<?= htmlspecialchars($img_src); ?>" alt="Foto Admin" style="width: 45px; height: 45px; object-fit: cover; border-radius: 50%; display: block; margin: 0 auto;">
                            </td>
                            <td class="py-3 px-3" style="font-size: 14px; color: #374151; text-align: center;">
                                <?= htmlspecialchars($data['nama_admin']); ?>
                            </td>
                            <td class="py-3 px-3" style="font-size: 14px; color: #374151; text-align: center;">
                                <?= htmlspecialchars($data['telepon_admin']); ?>
                            </td>
                            <td class="py-3 px-3" style="font-size: 14px; color: #374151; text-align: center;">
                                <?= htmlspecialchars($data['alamat_admin']); ?>
                            </td>
                            <td class="py-3 px-3" style="font-size: 14px; color: #374151; text-align: center;">
                                <?= ($data['jenis_kelamin'] == 'L') ? 'Laki-laki' : 'Perempuan'; ?>
                            </td>
                            <td class="py-3 px-3" style="font-size: 14px; color: #374151; text-align: center;">
                                <?= htmlspecialchars($data['id_user']); ?>
                            </td>
                            <td class="py-3 px-3" style="font-size: 14px; text-align: center; white-space: nowrap;">
                                <!-- Tombol Edit -->
                                <a href="index.php?pages=admin&aksi=edit&id=<?= urlencode($data['id_admin']); ?>" class="btn btn-warning btn-sm text-white">Edit</a>
                                <!-- Tombol Delete -->
                                <a href="index.php?pages=admin&aksi=delete&id=<?= urlencode($data['id_admin']); ?>&id_user=<?= urlencode($data['id_user']); ?>" 
                                   class="btn btn-danger btn-sm" 
                                   onclick="return confirm('Apakah Anda yakin ingin menghapus data admin ini?')">Delete</a>
                            </td>
                        </tr>
                        <?php
                            }
                        } else {
                        ?>
                        <tr>
                            <td colspan="8" class="text-center py-5 text-muted" style="font-size: 14px;">
                                Belum ada data admin di database.
                            </td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>
</div>