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

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

<?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;
?>

<?= $this->section('content') ?>
<div class="space-y-6">
    <div>
        <h1 class="text-2xl font-semibold tracking-tight text-gray-900">Data presensi pegawai</h1>
        <p class="mt-1 max-w-2xl text-sm text-gray-500">Filter rentang tanggal (default hari ini) dan pencarian nama atau NIP.</p>
    </div>

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

    <form method="get" action="<?= site_url('admin/presensi') ?>" class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
        <div class="flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between">
            <div class="flex flex-wrap items-end gap-3">
                <div>
                    <label class="block text-xs font-medium text-gray-600">Tanggal dari</label>
                    <input type="date" name="tanggal_dari" value="<?= esc($dari) ?>" class="mt-1 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>
                    <label class="block text-xs font-medium text-gray-600">Tanggal sampai</label>
                    <input type="date" name="tanggal_sampai" value="<?= esc($sampai) ?>" class="mt-1 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="min-w-[min(100%,14rem)] flex-1 sm:min-w-[16rem]">
                    <label class="block text-xs font-medium text-gray-600">Cari nama / NIP</label>
                    <input type="search" name="q" value="<?= esc($q ?? '') ?>" placeholder="Opsional" class="mt-1 w-full 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>
            <div class="flex flex-shrink-0 flex-wrap items-center 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">Terapkan</button>
                <a href="<?= site_url('admin/presensi?' . http_build_query(['tanggal_dari' => date('Y-m-d'), 'tanggal_sampai' => date('Y-m-d')])) ?>" class="inline-flex items-center justify-center rounded-lg border border-gray-200 bg-white px-4 py-2 text-sm font-medium text-gray-800 shadow-sm hover:bg-gray-50">Hari ini</a>
                <?php $exportQs = array_filter(['tanggal_dari' => $dari, 'tanggal_sampai' => $sampai, 'q' => $q ?? '']); ?>
                <a href="<?= site_url('admin/presensi/export-xlsx?' . http_build_query($exportQs)) ?>" class="inline-flex items-center justify-center rounded-lg border border-emerald-200 bg-emerald-50 px-4 py-2 text-sm font-semibold text-emerald-900 shadow-sm hover:bg-emerald-100">
                    <i class="fa-solid fa-file-excel mr-2"></i> Export Excel
                </a>
                <a href="<?= site_url('admin/presensi/export?' . http_build_query($exportQs)) ?>" class="inline-flex items-center justify-center rounded-lg border border-gray-200 bg-white px-3 py-2 text-xs font-medium text-gray-600 shadow-sm hover:bg-gray-50" title="Format CSV untuk aplikasi lain">CSV</a>
            </div>
        </div>
    </form>

    <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">Rekaman presensi</h2>
            <p class="mt-0.5 text-xs text-gray-500">Hanya menampilkan pegawai yang sudah punya minimal satu rekam (masuk / pulang / istirahat atau foto). Baris kosong dari aplikasi (buka app tanpa absen) tidak ditampilkan. Status: lengkap (hijau), sebagian (kuning), belum rekam (abu) untuk baris yang tampil. <strong>Export Excel</strong> (dan CSV) memakai filter tanggal dan pencarian yang sama (maks. 20.000 baris); file .xlsx sudah berheader, filter, dan lebar kolom.</p>
        </div>
        <?php if ($rows === []) : ?>
            <div class="p-6">
                <?= view('layouts/partials/empty_state', [
                    'icon'  => 'fa-calendar-xmark',
                    'title' => 'Tidak ada data presensi',
                    'hint'  => 'Ubah rentang tanggal atau kata kunci pencarian, lalu klik Terapkan.',
                ]) ?>
            </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-5 py-3">Tanggal</th>
                            <th class="px-5 py-3">Pegawai</th>
                            <th class="px-5 py-3">Status</th>
                            <th class="px-5 py-3">Masuk</th>
                            <th class="px-5 py-3">Pulang</th>
                            <th class="px-5 py-3">Istirahat</th>
                            <th class="px-5 py-3"></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php foreach ($rows as $pr) : ?>
                            <?php
                            $jm = $pr['jam_masuk'] ?? null;
                            $jp = $pr['jam_pulang'] ?? null;
                            $mi = $pr['mulai_istirahat'] ?? null;
                            $bi = $pr['beres_istirahat'] ?? null;
                            $hadirMasuk = ! empty($jm);
                            $hadirPulang = ! empty($jp);
                            if ($hadirMasuk && $hadirPulang) {
                                $stLabel = 'Lengkap';
                                $stClass = 'bg-emerald-50 text-emerald-800 ring-1 ring-emerald-100';
                            } elseif ($hadirMasuk || $hadirPulang) {
                                $stLabel = 'Sebagian';
                                $stClass = 'bg-amber-50 text-amber-900 ring-1 ring-amber-100';
                            } else {
                                $stLabel = 'Belum rekam';
                                $stClass = 'bg-gray-100 text-gray-600 ring-1 ring-gray-200';
                            }
                            ?>
                            <tr class="transition-colors hover:bg-gray-50">
                                <td class="whitespace-nowrap px-5 py-3 text-gray-800"><?= esc((string) ($pr['tanggal'] ?? '')) ?></td>
                                <td class="px-5 py-3">
                                    <div class="font-medium text-gray-900"><?= esc((string) ($pr['nama_lengkap'] ?? '')) ?></div>
                                    <div class="text-xs text-gray-500"><?= esc((string) ($pr['nip'] ?? '')) ?></div>
                                </td>
                                <td class="whitespace-nowrap px-5 py-3">
                                    <span class="inline-flex rounded-full px-2.5 py-0.5 text-xs font-semibold <?= $stClass ?>"><?= esc($stLabel) ?></span>
                                </td>
                                <td class="px-5 py-3">
                                    <?php if (! empty($jm)) : ?>
                                        <span class="font-mono text-xs text-gray-900"><?= esc(date('H:i', strtotime((string) $jm))) ?></span>
                                        <div class="text-xs text-gray-500"><?= esc((string) ($pr['ket_masuk'] ?? '')) ?></div>
                                    <?php else : ?>
                                        <span class="text-gray-400">—</span>
                                    <?php endif ?>
                                </td>
                                <td class="px-5 py-3">
                                    <?php if (! empty($jp)) : ?>
                                        <span class="font-mono text-xs text-gray-900"><?= esc(date('H:i', strtotime((string) $jp))) ?></span>
                                        <div class="text-xs text-gray-500"><?= esc((string) ($pr['ket_pulang'] ?? '')) ?></div>
                                    <?php else : ?>
                                        <span class="text-gray-400">—</span>
                                    <?php endif ?>
                                </td>
                                <td class="px-5 py-3 text-xs text-gray-700">
                                    <?php if (! empty($mi) || ! empty($bi)) : ?>
                                        <?php
                                        $s = (empty($mi) ? '—' : date('H:i', strtotime((string) $mi))) . ' → ' . (empty($bi) ? '—' : date('H:i', strtotime((string) $bi)));
                                        ?>
                                        <?= esc($s) ?>
                                    <?php else : ?>
                                        <span class="text-gray-400">—</span>
                                    <?php endif ?>
                                </td>
                                <td class="whitespace-nowrap px-5 py-3">
                                    <a href="<?= site_url('admin/presensi/detail/' . (int) ($pr['id_presensi'] ?? 0)) ?>" class="inline-flex rounded-lg border border-gray-200 bg-white px-3 py-1.5 text-xs font-semibold text-gray-800 shadow-sm hover:bg-gray-50">Detail</a>
                                </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 baris: <strong class="text-gray-900"><?= number_format($total, 0, ',', '.') ?></strong></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/presensi?' . http_build_query(array_filter(['tanggal_dari' => $dari, 'tanggal_sampai' => $sampai, 'q' => $q ?? '', 'page' => $curPage - 1]))) ?>">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/presensi?' . http_build_query(array_filter(['tanggal_dari' => $dari, 'tanggal_sampai' => $sampai, 'q' => $q ?? '', 'page' => $curPage + 1]))) ?>">Berikutnya</a>
                <?php endif ?>
            </div>
        </div>
    </section>
</div>
<?= $this->endSection() ?>
