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

<?= $this->section('title') ?>Hari libur<?= $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">Hari libur perusahaan</h1>
        <p class="mt-1 text-sm text-gray-500">Atur hari libur perusahaan yang mempengaruhi presensi.</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/presensi/libur/save') ?>" class="grid gap-4 sm:grid-cols-2">
            <?= csrf_field() ?>
            <input type="hidden" name="id_libur" id="f_id" value="">
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Tanggal libur *</label>
                <input type="date" name="tanggal_libur" id="f_tgl" required class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <div class="sm:col-span-2">
                <label class="mb-1 block text-xs font-medium text-gray-600">Keterangan *</label>
                <input name="keterangan_libur" id="f_ket" 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">Tanggal</th><th class="px-5 py-3">Keterangan</th><th class="px-5 py-3"></th></tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    <?php foreach ($rows as $x) : ?>
                        <?php if (! is_array($x)) {
                            continue;
                        } ?>
                        <tr class="hover:bg-gray-50">
                            <td class="whitespace-nowrap px-5 py-3"><?= esc((string) ($x['tanggal_libur'] ?? '')) ?></td>
                            <td class="px-5 py-3"><?= esc((string) ($x['keterangan_libur'] ?? '')) ?></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($x, 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_libur||'';document.getElementById('f_tgl').value=o.tanggal_libur||'';document.getElementById('f_ket').value=o.keterangan_libur||'';">Edit</button>
                                <form method="post" action="<?= site_url('admin/presensi/libur/delete/' . (int) ($x['id_libur'] ?? 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() ?>
