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

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

<?= $this->section('content') ?>
<?php
$s = is_array($summary ?? null) ? $summary : [];
$belumList = is_array($s['belum_rekam_pegawai'] ?? null) ? $s['belum_rekam_pegawai'] : [];
?>
<div class="space-y-6">
    <div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
        <div>
            <h1 class="text-2xl font-semibold tracking-tight text-gray-900">Dashboard Presensi</h1>
            <p class="mt-1 max-w-2xl text-sm text-gray-500">Statistik harian dan antrian pengajuan cuti.</p>
        </div>
        <span class="inline-flex w-fit shrink-0 items-center rounded-full border border-gray-200 bg-white px-3 py-1.5 text-xs font-semibold text-gray-700 shadow-sm"><?= esc(date('d F Y, H:i')) ?></span>
    </div>

    <?= view('layouts/partials/admin_alert', ['errors' => $errors ?? []]) ?>
    <?php if (! empty($errors) && empty($token ?? null)) : ?>
        <div class="rounded-2xl border border-gray-200 bg-white p-4 text-sm text-gray-700 shadow-sm">
            <a href="<?= site_url('admin/login') ?>" class="inline-flex items-center rounded-lg bg-blue-600 px-4 py-2 text-xs font-semibold text-white shadow-sm hover:bg-blue-700">Login ulang</a>
        </div>
    <?php endif ?>

    <?php if ($summary !== null) : ?>
        <div class="grid gap-4 md:grid-cols-3">
            <div class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
                <p class="text-xs font-semibold uppercase tracking-wide text-gray-500">Total Pegawai</p>
                <p class="mt-2 text-3xl font-bold text-gray-900"><?= number_format((int) ($s['total_pegawai'] ?? 0), 0, ',', '.') ?></p>
                <p class="mt-2 text-xs text-gray-600">
                    Pria: <?= number_format((int) ($s['pegawai_laki'] ?? 0), 0, ',', '.') ?>
                    <span class="text-gray-400">(<?= esc((string) ($s['persen_laki'] ?? '0')) ?>%)</span>
                    · Wanita: <?= number_format((int) ($s['pegawai_perempuan'] ?? 0), 0, ',', '.') ?>
                    <span class="text-gray-400">(<?= esc((string) ($s['persen_perempuan'] ?? '0')) ?>%)</span>
                </p>
            </div>
            <div class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
                <p class="text-xs font-semibold uppercase tracking-wide text-gray-500">Presensi hari ini</p>
                <p class="mt-2 text-3xl font-bold text-blue-600"><?= number_format((int) ($s['presensi_hari_ini'] ?? 0), 0, ',', '.') ?></p>
                <p class="mt-2 text-xs text-gray-600">Kehadiran <?= esc((string) ($s['persen_presensi'] ?? '0')) ?>%</p>
                <p class="mt-1 text-xs text-amber-700"><?= number_format((int) ($s['belum_rekam'] ?? 0), 0, ',', '.') ?> belum rekam presensi</p>
            </div>
            <div class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
                <p class="text-xs font-semibold uppercase tracking-wide text-gray-500">Cuti hari ini (Approve)</p>
                <p class="mt-2 text-3xl font-bold text-emerald-600"><?= number_format((int) ($s['cuti_hari_ini'] ?? 0), 0, ',', '.') ?></p>
                <p class="mt-2 text-xs text-gray-600">Pegawai cuti <?= esc((string) ($s['persen_cuti'] ?? '0')) ?>% · status disetujui</p>
            </div>
        </div>

        <div class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
            <h2 class="text-sm font-semibold text-gray-900">Ringkasan kehadiran hari ini</h2>
            <p class="mt-0.5 text-xs text-gray-500">Proporsi hadir, cuti disetujui, dan belum rekam.</p>
            <?php
            $hadir  = (int) ($s['presensi_hari_ini'] ?? 0);
            $cuti   = (int) ($s['cuti_hari_ini'] ?? 0);
            $blm    = (int) ($s['belum_rekam'] ?? 0);
            $tot    = max(1, (int) ($s['total_pegawai'] ?? 1));
            $pH     = round(($hadir / $tot) * 100, 1);
            $pC     = round(($cuti / $tot) * 100, 1);
            $pB     = round(($blm / $tot) * 100, 1);
            ?>
            <div class="mt-4 flex h-11 w-full overflow-hidden rounded-xl text-xs font-semibold text-white shadow-inner">
                <span class="flex min-w-0 items-center justify-center bg-emerald-500 px-1" style="width: <?= esc((string) max(0, min(100, $pH))) ?>%"><?= $hadir > 0 ? 'Hadir ' . $hadir : '' ?></span>
                <span class="flex min-w-0 items-center justify-center bg-amber-500 px-1" style="width: <?= esc((string) max(0, min(100, $pC))) ?>%"><?= $cuti > 0 ? 'Cuti ' . $cuti : '' ?></span>
                <span class="flex min-w-0 items-center justify-center bg-gray-400 px-1" style="width: <?= esc((string) max(0, min(100, $pB))) ?>%"><?= $blm > 0 ? 'Belum ' . $blm : '' ?></span>
            </div>
            <div class="mt-4 flex flex-wrap items-baseline gap-x-6 gap-y-2 text-xs text-gray-600">
                <span><strong class="text-gray-900">Hadir</strong> <?= $hadir ?> (<?= $pH ?>%)</span>
                <span><strong class="text-gray-900">Cuti</strong> <?= $cuti ?> (<?= $pC ?>%)</span>
                <?php if ($blm > 0) : ?>
                    <details class="relative max-w-full [&_summary::-webkit-details-marker]:hidden">
                        <summary class="flex cursor-pointer list-none flex-wrap items-baseline gap-x-1 text-gray-600 underline decoration-dotted decoration-gray-400 underline-offset-2 hover:text-blue-700">
                            <strong class="text-gray-900">Belum rekam</strong>
                            <span><?= $blm ?> (<?= $pB ?>%)</span>
                            <span class="text-[10px] font-normal text-blue-600">klik</span>
                        </summary>
                        <div class="absolute left-0 z-20 mt-2 max-h-64 w-[min(calc(100vw-2rem),20rem)] overflow-y-auto rounded-lg border border-gray-200 bg-white p-3 text-left shadow-lg">
                            <p class="mb-2 font-semibold text-gray-800">Yang belum rekam presensi hari ini</p>
                            <p class="mb-2 text-[11px] leading-snug text-gray-500">Hanya informasi; tidak termasuk yang cuti disetujui.</p>
                            <?php if ($belumList === []) : ?>
                                <p class="text-gray-500">Daftar nama tidak tersedia. Coba muat ulang halaman setelah pembaruan server.</p>
                            <?php else : ?>
                                <ol class="list-decimal space-y-1.5 pl-4 text-gray-700">
                                    <?php foreach ($belumList as $bp) : ?>
                                        <?php if (! is_array($bp)) {
                                            continue;
                                        } ?>
                                        <li class="leading-snug">
                                            <span class="font-medium text-gray-900"><?= esc((string) ($bp['nama_lengkap'] ?? '')) ?></span>
                                            <?php if (($bp['nip'] ?? '') !== '') : ?>
                                                <span class="text-gray-500"> · NIP <?= esc((string) $bp['nip']) ?></span>
                                            <?php endif ?>
                                        </li>
                                    <?php endforeach ?>
                                </ol>
                            <?php endif ?>
                        </div>
                    </details>
                <?php else : ?>
                    <span><strong class="text-gray-900">Belum rekam</strong> <?= $blm ?> (<?= $pB ?>%)</span>
                <?php endif ?>
                <span><strong class="text-gray-900">Total</strong> <?= (int) ($s['total_pegawai'] ?? 0) ?></span>
            </div>
        </div>

        <?php
        $pending = is_array($s['permohonan_cuti'] ?? null) ? $s['permohonan_cuti'] : [];
        $nPending = (int) ($s['total_permohonan_cuti'] ?? 0);
        ?>
        <section class="overflow-hidden rounded-2xl border border-amber-200/80 bg-white shadow-sm">
            <div class="border-b border-amber-100 bg-amber-50 px-5 py-4">
                <h2 class="text-sm font-semibold text-amber-950">Permohonan cuti menunggu persetujuan</h2>
                <p class="mt-0.5 text-xs text-amber-900/80"><?= number_format($nPending, 0, ',', '.') ?> permohonan menunggu, seluruhnya pengajuan lihat di Data Cuti.</p>
            </div>
            <?php if ($pending === []) : ?>
                <div class="p-6">
                    <?= view('layouts/partials/empty_state', [
                        'icon'  => 'fa-clipboard-check',
                        'title' => 'Tidak ada antrian Waiting',
                        'hint'  => 'Permohonan baru akan muncul di sini. Kelola semua cuti lewat menu Pegawai → Data Cuti.',
                    ]) ?>
                </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">Pegawai</th>
                                <th class="px-5 py-3">Tanggal cuti</th>
                                <th class="px-5 py-3">Tipe</th>
                                <th class="px-5 py-3">Alasan</th>
                                <?php if (canAccess('cuti')) : ?>
                                    <th class="px-5 py-3">Aksi</th>
                                <?php endif ?>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-100">
                            <?php foreach ($pending as $row) : ?>
                                <?php if (! is_array($row)) {
                                    continue;
                                } ?>
                                <tr class="transition-colors hover:bg-gray-50">
                                    <td class="px-5 py-3">
                                        <div class="font-medium text-gray-900"><?= esc((string) ($row['nama_lengkap'] ?? '')) ?></div>
                                        <div class="text-xs text-gray-500">NIP: <?= esc((string) ($row['nip'] ?? '')) ?></div>
                                    </td>
                                    <td class="whitespace-nowrap px-5 py-3 text-gray-800"><?= esc(cuti_tanggal_label_from_row($row)) ?></td>
                                    <td class="px-5 py-3 text-gray-700"><?= esc((string) ($row['tipe_cuti'] ?? '')) ?></td>
                                    <td class="max-w-xs truncate px-5 py-3 text-gray-600" title="<?= esc((string) ($row['alasan_cuti'] ?? '')) ?>"><?= esc((string) ($row['alasan_cuti'] ?? '')) ?></td>
                                    <?php if (canAccess('cuti')) : ?>
                                        <td class="whitespace-nowrap px-5 py-3">
                                            <div class="flex flex-wrap gap-2">
                                                <form method="post" action="<?= site_url('admin/cuti/approve/' . (int) ($row['id_cuti'] ?? 0)) ?>" class="inline">
                                                    <?= csrf_field() ?>
                                                    <button type="submit" class="inline-flex rounded-lg bg-emerald-600 px-3 py-1.5 text-xs font-semibold text-white shadow-sm hover:bg-emerald-700">Approve</button>
                                                </form>
                                                <a href="<?= site_url('admin/cuti/detail/' . (int) ($row['id_cuti'] ?? 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">Tolak…</a>
                                            </div>
                                        </td>
                                    <?php endif ?>
                                </tr>
                            <?php endforeach ?>
                        </tbody>
                    </table>
                </div>
            <?php endif ?>
            <?php if (canAccess('cuti')) : ?>
                <div class="border-t border-gray-100 bg-gray-50/80 px-5 py-3">
                    <a href="<?= site_url('admin/cuti') ?>" class="text-sm font-semibold text-blue-600 hover:text-blue-800">Kelola semua cuti →</a>
                </div>
            <?php endif ?>
        </section>
    <?php endif ?>

    <div class="grid gap-4 md:grid-cols-2">
        <?= view('admin/dashboard/_profil_ringkas', ['profil' => $profil ?? null]) ?>

        <section class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
            <h2 class="text-xs font-semibold uppercase tracking-wide text-gray-500">Berita terbaru</h2>
            <?php if (! empty($berita) && is_array($berita)) : ?>
                <ul class="mt-4 divide-y divide-gray-100">
                    <?php foreach ($berita as $row) : ?>
                        <li class="py-3 text-sm">
                            <?php if (is_array($row)) : ?>
                                <span class="font-medium text-gray-900"><?= esc((string) ($row['judul'] ?? $row['title'] ?? 'Item')) ?></span>
                                <?php if (! empty($row['tanggal'] ?? $row['tgl'] ?? null)) : ?>
                                    <span class="mt-0.5 block text-xs text-gray-400"><?= esc((string) ($row['tanggal'] ?? $row['tgl'])) ?></span>
                                <?php endif ?>
                            <?php else : ?>
                                <?= esc((string) $row) ?>
                            <?php endif ?>
                        </li>
                    <?php endforeach ?>
                </ul>
            <?php else : ?>
                <div class="mt-4">
                    <?= view('layouts/partials/empty_state', [
                        'icon'  => 'fa-newspaper',
                        'title' => 'Tidak ada berita',
                        'hint'  => 'Pengumuman dari sistem akan tampil di blok ini bila tersedia.',
                    ]) ?>
                </div>
            <?php endif ?>
        </section>
    </div>
</div>
<?= $this->endSection() ?>