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

<?= $this->section('title') ?>Jadwal presensi<?= $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">Management jadwal</h1>
        <p class="mt-1 text-sm text-gray-500">Atur jam masuk dan pulang standar per hari dalam minggu.</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/jadwal/save') ?>" class="space-y-4">
            <?= csrf_field() ?>
            <input type="hidden" name="id_jadwal" id="f_id" value="">
            <div class="grid gap-4 sm:grid-cols-3">
                <div class="sm:col-span-2">
                    <label class="mb-1 block text-xs font-medium text-gray-600">Nama jadwal *</label>
                    <input name="nama_jadwal" id="f_nama" 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">Toleransi terlambat (mnt) *</label>
                    <input type="number" name="toleransi_terlambat" id="f_tol_t" 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">Toleransi pulang cepat *</label>
                    <input type="number" name="toleransi_pulang_cepat" id="f_tol_p" required class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm shadow-sm">
                </div>
            </div>
            <?php
            $days = [1 => 'Senin', 2 => 'Selasa', 3 => 'Rabu', 4 => 'Kamis', 5 => 'Jumat', 6 => 'Sabtu', 7 => 'Minggu'];
            ?>
            <div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
                <?php foreach ($days as $d => $label) : ?>
                    <div class="rounded-xl border border-gray-100 bg-gray-50/80 p-3">
                        <p class="mb-2 text-xs font-semibold text-gray-700"><?= esc($label) ?></p>
                        <div class="flex gap-2">
                            <div class="flex-1">
                                <label class="text-[10px] text-gray-500">Masuk</label>
                                <input name="<?= $d ?>_in" id="f_<?= $d ?>_in" class="w-full rounded border border-gray-300 px-2 py-1 text-xs" placeholder="08:00">
                            </div>
                            <div class="flex-1">
                                <label class="text-[10px] text-gray-500">Pulang</label>
                                <input name="<?= $d ?>_out" id="f_<?= $d ?>_out" class="w-full rounded border border-gray-300 px-2 py-1 text-xs" placeholder="17:00">
                            </div>
                        </div>
                    </div>
                <?php endforeach ?>
            </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 jadwal</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">Sen–Min (masuk)</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="px-5 py-3 font-medium text-gray-900"><?= esc((string) ($x['nama_jadwal'] ?? '')) ?></td>
                            <td class="px-5 py-3 font-mono text-xs text-gray-600"><?= esc((string) ($x['1_in'] ?? '')) ?> … <?= esc((string) ($x['5_out'] ?? '')) ?></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="(function(o){document.getElementById('f_id').value=o.id_jadwal||'';document.getElementById('f_nama').value=o.nama_jadwal||'';document.getElementById('f_tol_t').value=o.toleransi_terlambat||0;document.getElementById('f_tol_p').value=o.toleransi_pulang_cepat||0;for(var d=1;d<=7;d++){document.getElementById('f_'+d+'_in').value=o[d+'_in']||'';document.getElementById('f_'+d+'_out').value=o[d+'_out']||'';}})(JSON.parse(this.dataset.row))">Edit</button>
                                <form method="post" action="<?= site_url('admin/presensi/jadwal/delete/' . (int) ($x['id_jadwal'] ?? 0)) ?>" class="inline" onsubmit="return confirm('Hapus jadwal? Pastikan tidak dipakai pegawai.');"><?= 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() ?>
