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

<?= $this->section('title') ?>Jadwal lembur<?= $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">Jadwal lembur</h1>
        <p class="mt-1 text-sm text-gray-500">Kelola pencatatan lembur pegawai.</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/lembur/save') ?>" class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
            <?= csrf_field() ?>
            <input type="hidden" name="id_lembur" id="f_id" value="">
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Tanggal *</label>
                <input type="date" name="tanggal_lembur" id="f_tgl" required class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Pegawai *</label>
                <select name="pegawai" id="f_pegawai" required class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
                    <option value="">—</option>
                    <?php foreach ($pegawai as $pg) : ?>
                        <?php if (! is_array($pg)) {
                            continue;
                        } ?>
                        <option value="<?= (int) ($pg['id_pegawai'] ?? 0) ?>"><?= esc((string) ($pg['nama_lengkap'] ?? '')) ?></option>
                    <?php endforeach ?>
                </select>
            </div>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Masuk (HH:MM) *</label>
                <input name="masuk" id="f_masuk" required class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm" placeholder="18:00">
            </div>
            <div>
                <label class="mb-1 block text-xs font-medium text-gray-600">Beres (HH:MM) *</label>
                <input name="beres" id="f_beres" required class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm" placeholder="22:00">
            </div>
            <div class="sm:col-span-2">
                <label class="mb-1 block text-xs font-medium text-gray-600">Keterangan *</label>
                <input name="keterangan" id="f_ket" required class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
            </div>
            <div class="flex items-end gap-2">
                <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>
            </div>
        </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">Pegawai</th><th class="px-5 py-3">Jam</th><th class="px-5 py-3">Ket</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_lembur'] ?? '')) ?></td>
                            <td class="px-5 py-3"><?= esc((string) ($x['nama_lengkap'] ?? '')) ?></td>
                            <td class="px-5 py-3 font-mono text-xs"><?= esc((string) ($x['masuk'] ?? '')) ?>–<?= esc((string) ($x['beres'] ?? '')) ?></td>
                            <td class="px-5 py-3"><?= esc((string) ($x['keterangan'] ?? '')) ?></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_lembur||'';document.getElementById('f_tgl').value=o.tanggal_lembur||'';document.getElementById('f_pegawai').value=String(o.pegawai||'');document.getElementById('f_masuk').value=o.masuk||'';document.getElementById('f_beres').value=o.beres||'';document.getElementById('f_ket').value=o.keterangan||'';">Edit</button>
                                <form method="post" action="<?= site_url('admin/presensi/lembur/delete/' . (int) ($x['id_lembur'] ?? 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() ?>
