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

<?= $this->section('title') ?>Aktivitas harian<?= $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 text-gray-900">Rekaman aktivitas harian</h1>
        <p class="mt-1 text-sm text-gray-500">Log aktivitas dari aplikasi pegawai. Hanya dapat dilihat atau dihapus di sini.</p>
    </div>
    <?= view('layouts/partials/admin_alert', ['errors' => $errors ?? []]) ?>
    <section class="overflow-hidden rounded-2xl border border-gray-200 bg-white shadow-sm">
        <div class="flex items-center justify-between border-b border-gray-200 px-5 py-4">
            <h2 class="text-base font-semibold text-gray-900">Daftar</h2>
            <span class="text-xs text-gray-500">Total <?= number_format($total, 0, ',', '.') ?></span>
        </div>
        <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">Waktu</th>
                        <th class="px-5 py-3">Pegawai</th>
                        <th class="px-5 py-3">Deskripsi</th>
                        <th class="px-5 py-3">Gambar</th>
                        <th class="px-5 py-3"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    <?php if ($rows === []) : ?>
                        <tr><td colspan="5" class="px-5 py-10 text-center text-gray-500">Tidak ada data.</td></tr>
                    <?php else : ?>
                        <?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 text-xs"><?= esc((string) ($x['waktu_aktifitas'] ?? '')) ?></td>
                                <td class="px-5 py-3">
                                    <div class="font-medium text-gray-900"><?= esc((string) ($x['nama_lengkap'] ?? '')) ?></div>
                                    <div class="text-xs text-gray-500"><?= esc((string) ($x['nip'] ?? '')) ?></div>
                                </td>
                                <td class="max-w-xs truncate px-5 py-3" title="<?= esc((string) ($x['deskripsi'] ?? '')) ?>"><?= esc((string) ($x['deskripsi'] ?? '')) ?></td>
                                <td class="px-5 py-3 text-xs"><?= esc((string) ($x['image'] ?? '')) ?></td>
                                <td class="whitespace-nowrap px-5 py-3">
                                    <form method="post" action="<?= site_url('admin/presensi/aktivitas/delete/' . (int) ($x['id_aktifitas_harian'] ?? 0)) ?>" class="inline" onsubmit="return confirm('Hapus rekaman ini?');">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="page" value="<?= (int) $curPage ?>">
                                        <button type="submit" class="rounded-lg bg-red-50 px-2 py-1 text-xs font-semibold text-red-700 hover:bg-red-100">Hapus</button>
                                    </form>
                                </td>
                            </tr>
                        <?php endforeach ?>
                    <?php endif ?>
                </tbody>
            </table>
        </div>
        <div class="flex items-center justify-between border-t border-gray-200 bg-gray-50/80 px-5 py-3 text-xs text-gray-600">
            <span>Halaman <?= (int) $curPage ?> / <?= (int) $totalPage ?></span>
            <div class="flex 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 shadow-sm hover:bg-gray-50" href="<?= site_url('admin/presensi/aktivitas?' . http_build_query(['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 shadow-sm hover:bg-gray-50" href="<?= site_url('admin/presensi/aktivitas?' . http_build_query(['page' => $curPage + 1])) ?>">Berikutnya</a>
                <?php endif ?>
            </div>
        </div>
    </section>
</div>
<?= $this->endSection() ?>
