<?= $this->extend('layouts/main') ?>

<?= $this->section('title') ?>Data Pegawai<?= $this->endSection() ?>

<?= $this->section('content') ?>
<div class="space-y-6">
    <div class="flex flex-col justify-between gap-4 sm:flex-row sm:items-start">
        <div>
            <h1 class="text-2xl font-semibold tracking-tight text-gray-900">Data Pegawai</h1>
            <p class="mt-1 max-w-2xl text-sm text-gray-500">Daftar pegawai. Gunakan pencarian untuk memfilter nama atau NIP.</p>
        </div>
        <?php if (canAccess('pegawai_tambah')) : ?>
            <a href="<?= site_url('admin/pegawai/create') ?>" class="inline-flex shrink-0 items-center justify-center rounded-lg bg-blue-600 px-4 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-blue-700">
                <i class="fa-solid fa-plus mr-2"></i> Tambah pegawai
            </a>
        <?php endif ?>
    </div>

    <?= view('layouts/partials/admin_alert', ['errors' => $errors ?? []]) ?>

    <form method="get" action="<?= site_url('admin/pegawai') ?>" class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
        <div class="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between">
            <div class="min-w-0 flex-1">
                <label class="block text-xs font-medium text-gray-600">Cari (nama / NIP / username)</label>
                <input type="search" name="q" value="<?= esc($q) ?>" placeholder="Ketik lalu cari…" class="mt-1 w-full max-w-xl rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500">
            </div>
            <div class="flex shrink-0 gap-2">
                <button type="submit" class="inline-flex items-center justify-center rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-700">Cari</button>
            </div>
        </div>
    </form>

    <?php
    $rows = is_array($payload) ? ($payload['rows'] ?? []) : [];
    $total = is_array($payload) ? (int) ($payload['total'] ?? 0) : 0;
    $totalPage = is_array($payload) ? max(1, (int) ($payload['total_page'] ?? 1)) : 1;
    $curPage = is_array($payload) ? (int) ($payload['page'] ?? $page) : $page;
    ?>

    <section class="overflow-hidden rounded-2xl border border-gray-200 bg-white shadow-sm">
        <div class="border-b border-gray-200 px-5 py-4">
            <h2 class="text-base font-semibold text-gray-900">Daftar pegawai</h2>
            <p class="mt-0.5 text-xs text-gray-500">Edit, reset password (NIP MD5), atau hapus baris.</p>
        </div>
        <?php if ($rows === []) : ?>
            <div class="p-6">
                <?= view('layouts/partials/empty_state', [
                    'icon'  => 'fa-users',
                    'title' => 'Tidak ada pegawai',
                    'hint'  => 'Sesuaikan kata kunci pencarian atau tambah pegawai baru dengan tombol di atas.',
                ]) ?>
            </div>
        <?php else : ?>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200 text-sm">
                    <thead class="bg-gray-50 text-left text-xs font-semibold uppercase tracking-wide text-gray-500">
                        <tr>
                            <th class="px-4 py-3">NIP</th>
                            <th class="px-4 py-3">Nama</th>
                            <th class="px-4 py-3">JK</th>
                            <th class="px-4 py-3">Jabatan</th>
                            <th class="px-4 py-3">Unit</th>
                            <th class="px-4 py-3">Golongan</th>
                            <th class="px-4 py-3">Kantor</th>
                            <th class="px-4 py-3 w-44">Tindakan</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php foreach ($rows as $p) : ?>
                            <tr class="transition-colors hover:bg-gray-50">
                                <td class="whitespace-nowrap px-4 py-2.5 font-mono text-xs text-gray-800"><?= esc((string) ($p['nip'] ?? '')) ?></td>
                                <td class="px-4 py-2.5 font-medium text-gray-900"><?= esc((string) ($p['nama_lengkap'] ?? '')) ?></td>
                                <td class="px-4 py-2.5 text-gray-700"><?= esc((string) ($p['jenis_kelamin'] ?? '')) ?></td>
                                <td class="px-4 py-2.5 text-gray-700"><?= esc((string) ($p['nama_jabatan'] ?? '')) ?></td>
                                <td class="px-4 py-2.5 text-gray-700"><?= esc((string) ($p['nama_unit_kerja'] ?? '')) ?></td>
                                <td class="px-4 py-2.5 text-gray-700"><?= esc((string) ($p['nama_golongan'] ?? '')) ?></td>
                                <td class="px-4 py-2.5 text-gray-700"><?= esc((string) ($p['nama_kantor'] ?? '')) ?></td>
                                <td class="px-4 py-2.5">
                                    <div class="flex flex-wrap gap-1.5">
                                        <a href="<?= site_url('admin/pegawai/edit/' . (int) ($p['id_pegawai'] ?? 0)) ?>" class="inline-flex rounded-lg border border-gray-200 bg-white px-2 py-1 text-xs font-semibold text-gray-800 shadow-sm hover:bg-gray-50">Edit</a>
                                        <form action="<?= site_url('admin/pegawai/reset/' . (int) ($p['id_pegawai'] ?? 0)) ?>" method="post" class="inline" onsubmit="return confirm('Reset password ke NIP (MD5) dan kosongkan token?');">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="inline-flex rounded-lg border border-amber-200 bg-amber-50 px-2 py-1 text-xs font-semibold text-amber-900 hover:bg-amber-100">Reset</button>
                                        </form>
                                        <form action="<?= site_url('admin/pegawai/delete/' . (int) ($p['id_pegawai'] ?? 0)) ?>" method="post" class="inline" onsubmit="return confirm('Hapus pegawai ini?');">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="inline-flex rounded-lg border border-red-200 bg-red-50 px-2 py-1 text-xs font-semibold text-red-800 hover:bg-red-100">Hapus</button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach ?>
                    </tbody>
                </table>
            </div>
        <?php endif ?>
        <div class="flex flex-col gap-3 border-t border-gray-200 bg-gray-50/80 px-5 py-3 text-xs text-gray-600 sm:flex-row sm:items-center sm:justify-between">
            <span>Total <strong class="text-gray-900"><?= number_format($total, 0, ',', '.') ?></strong> pegawai</span>
            <div class="flex flex-wrap gap-2">
                <?php if ($curPage > 1) : ?>
                    <a class="inline-flex rounded-lg border border-gray-200 bg-white px-3 py-1.5 font-medium text-gray-800 shadow-sm hover:bg-gray-50" href="<?= site_url('admin/pegawai?' . http_build_query(['page' => $curPage - 1, 'q' => $q])) ?>">Sebelumnya</a>
                <?php endif ?>
                <?php if ($curPage < $totalPage) : ?>
                    <a class="inline-flex rounded-lg border border-gray-200 bg-white px-3 py-1.5 font-medium text-gray-800 shadow-sm hover:bg-gray-50" href="<?= site_url('admin/pegawai?' . http_build_query(['page' => $curPage + 1, 'q' => $q])) ?>">Berikutnya</a>
                <?php endif ?>
            </div>
        </div>
    </section>
</div>
<?= $this->endSection() ?>
