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

<?= $this->section('title') ?>Jabatan<?= $this->endSection() ?>
<?php $rows = is_array($payload) ? ($payload['rows'] ?? []) : []; ?>

<?= $this->section('content') ?>
<div class="space-y-6">
    <div>
        <h1 class="text-2xl font-semibold text-gray-900">Jabatan</h1>
        <p class="mt-1 text-sm text-gray-500">Master jabatan.</p>
    </div>
    <?= view('layouts/partials/admin_alert', ['errors' => $errors ?? []]) ?>
    <section class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
        <form method="post" action="<?= site_url('admin/perusahaan/jabatan/save') ?>" class="flex flex-wrap items-end gap-3">
            <?= csrf_field() ?>
            <input type="hidden" name="id_jabatan" id="f_id" value="">
            <div class="min-w-[16rem] flex-1">
                <label class="mb-1 block text-xs font-medium text-gray-600">Nama jabatan *</label>
                <input name="nama_jabatan" id="f_nama" required class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <button type="submit" class="rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-700">Simpan</button>
        </form>
    </section>
    <section class="overflow-hidden rounded-2xl border border-gray-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200 text-sm">
                <thead class="border-y border-gray-200 bg-gray-50 text-left text-xs font-semibold uppercase text-gray-500">
                    <tr><th class="px-5 py-3">Nama</th><th class="px-5 py-3"></th></tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    <?php foreach ($rows as $r) : ?>
                        <?php if (! is_array($r)) {
                            continue;
                        } ?>
                        <tr class="hover:bg-gray-50">
                            <td class="px-5 py-3 font-medium text-gray-900"><?= esc((string) ($r['nama_jabatan'] ?? '')) ?></td>
                            <td class="whitespace-nowrap px-5 py-3">
                                <button type="button" class="rounded-lg border border-gray-200 px-2 py-1 text-xs font-semibold" data-row="<?= esc(json_encode($r, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT), 'attr') ?>" onclick="var o=JSON.parse(this.dataset.row);document.getElementById('f_id').value=o.id_jabatan||'';document.getElementById('f_nama').value=o.nama_jabatan||'';">Edit</button>
                                <form method="post" action="<?= site_url('admin/perusahaan/jabatan/delete/' . (int) ($r['id_jabatan'] ?? 0)) ?>" class="inline" onsubmit="return confirm('Hapus?');"><?= csrf_field() ?><button type="submit" class="ml-1 rounded-lg bg-red-50 px-2 py-1 text-xs font-semibold text-red-700">Hapus</button></form>
                            </td>
                        </tr>
                    <?php endforeach ?>
                </tbody>
            </table>
        </div>
    </section>
</div>
<?= $this->endSection() ?>
