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

<?= $this->section('title') ?>Data Cuti<?= $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;
$statusOpts = ['' => 'Semua', 'Waiting' => 'Waiting', 'Approve' => 'Approve', 'Rejected' => 'Rejected', 'Cancelled' => 'Cancelled'];
?>

<?= $this->section('content') ?>
<div class="space-y-6">
    <div>
        <h1 class="text-2xl font-semibold tracking-tight text-gray-900">Data cuti pegawai</h1>
        <p class="mt-1 max-w-2xl text-sm text-gray-500">Filter menurut status dan lakukan persetujuan atau penolakan dari sini.</p>
    </div>

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

    <form method="get" action="<?= site_url('admin/cuti') ?>" class="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
        <div class="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between">
            <div>
                <label class="block text-xs font-medium text-gray-600">Status</label>
                <select name="status" class="mt-1 min-w-[12rem] 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">
                    <?php foreach ($statusOpts as $val => $lab) : ?>
                        <option value="<?= esc($val) ?>" <?= ($status === $val) ? 'selected' : '' ?>><?= esc($lab) ?></option>
                    <?php endforeach ?>
                </select>
            </div>
            <div class="flex 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/cuti') ?>" 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">Semua status</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">Daftar permohonan</h2>
            <p class="mt-0.5 text-xs text-gray-500">Hijau = disetujui · kuning = menunggu · merah = ditolak.</p>
        </div>
        <?php if ($rows === []) : ?>
            <div class="p-6">
                <?= view('layouts/partials/empty_state', [
                    'icon'  => 'fa-plane-departure',
                    'title' => 'Tidak ada permohonan cuti',
                    'hint'  => 'Pilih status lain atau kembali ke Semua status untuk melihat seluruh riwayat.',
                ]) ?>
            </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-4 py-3">Pegawai</th>
                            <th class="px-4 py-3">Tanggal</th>
                            <th class="px-4 py-3">Tipe</th>
                            <th class="px-4 py-3">Status</th>
                            <th class="px-4 py-3"></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php foreach ($rows as $c) : ?>
                            <?php if (! is_array($c)) {
                                continue;
                            } ?>
                            <?php
                            $st = (string) ($c['status_cuti'] ?? '');
                            $badge = match ($st) {
                                'Approve' => 'bg-emerald-50 text-emerald-800 ring-1 ring-emerald-100',
                                'Rejected' => 'bg-red-50 text-red-800 ring-1 ring-red-100',
                                'Waiting' => 'bg-amber-50 text-amber-900 ring-1 ring-amber-100',
                                'Cancelled' => 'bg-gray-100 text-gray-600 ring-1 ring-gray-200',
                                default => 'bg-gray-100 text-gray-700 ring-1 ring-gray-200',
                            };
                            ?>
                            <tr class="transition-colors hover:bg-gray-50">
                                <td class="px-4 py-2.5">
                                    <div class="font-medium text-gray-900"><?= esc((string) ($c['nama_lengkap'] ?? '')) ?></div>
                                    <div class="text-xs text-gray-500"><?= esc((string) ($c['nip'] ?? '')) ?></div>
                                </td>
                                <td class="whitespace-nowrap px-4 py-2.5 text-gray-800"><?= esc(cuti_tanggal_label_from_row($c)) ?></td>
                                <td class="px-4 py-2.5 text-gray-700"><?= esc((string) ($c['tipe_cuti'] ?? '')) ?></td>
                                <td class="px-4 py-2.5">
                                    <span class="inline-flex rounded-full px-2.5 py-0.5 text-xs font-semibold <?= $badge ?>"><?= esc($st) ?></span>
                                </td>
                                <td class="whitespace-nowrap px-4 py-2.5">
                                    <a href="<?= site_url('admin/cuti/detail/' . (int) ($c['id_cuti'] ?? 0)) ?>" class="mr-2 text-xs font-semibold text-blue-600 hover:text-blue-800">Detail</a>
                                    <?php if ($st === 'Waiting') : ?>
                                        <form method="post" action="<?= site_url('admin/cuti/approve/' . (int) ($c['id_cuti'] ?? 0)) ?>" class="inline" onsubmit="return confirm('Setujui permohonan ini?');">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="text-xs font-semibold text-emerald-700 hover:text-emerald-900 hover:underline">Approve</button>
                                        </form>
                                    <?php endif ?>
                                </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: <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/cuti?' . http_build_query(['status' => $status, '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/cuti?' . http_build_query(['status' => $status, 'page' => $curPage + 1])) ?>">Berikutnya</a>
                <?php endif ?>
            </div>
        </div>
    </section>
</div>
<?= $this->endSection() ?>
