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

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

<?php
$cuti = is_array($bundle) ? ($bundle['cuti'] ?? null) : null;
$dok  = is_array($bundle) ? ($bundle['dokumen'] ?? []) : [];
$cuti = is_array($cuti) ? $cuti : [];
$st   = (string) ($cuti['status_cuti'] ?? '');
$badgeClass = 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',
};
?>

<?= $this->section('content') ?>
<div class="mx-auto max-w-3xl space-y-6">
    <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
        <div>
            <h1 class="text-2xl font-semibold tracking-tight text-gray-900">Detail cuti</h1>
            <?php if ($cuti !== []) : ?>
                <p class="mt-2"><span class="inline-flex rounded-full px-3 py-1 text-xs font-semibold <?= $badgeClass ?>"><?= esc($st !== '' ? $st : '—') ?></span></p>
            <?php endif ?>
        </div>
        <a href="<?= site_url('admin/cuti') ?>" class="inline-flex items-center justify-center gap-2 rounded-lg border border-gray-200 bg-white px-4 py-2.5 text-sm font-medium text-gray-800 shadow-sm hover:bg-gray-50">
            <i class="fa-solid fa-arrow-left text-xs text-gray-500"></i> Kembali ke daftar
        </a>
    </div>

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

    <?php if ($cuti !== []) : ?>
        <section class="rounded-2xl border border-gray-200 bg-white p-6 shadow-sm">
            <h2 class="text-xs font-semibold uppercase tracking-wide text-gray-500">Ringkasan</h2>
            <dl class="mt-4 grid gap-4 text-sm sm:grid-cols-2">
                <div class="sm:col-span-2">
                    <dt class="text-xs font-medium text-gray-500">Pegawai</dt>
                    <dd class="mt-1 font-semibold text-gray-900"><?= esc((string) ($cuti['nama_lengkap'] ?? '')) ?> <span class="font-normal text-gray-500">· NIP <?= esc((string) ($cuti['nip'] ?? '')) ?></span></dd>
                </div>
                <div>
                    <dt class="text-xs font-medium text-gray-500">Tanggal cuti</dt>
                    <dd class="mt-1 font-medium text-gray-900"><?= esc(cuti_tanggal_label_from_row($cuti)) ?></dd>
                </div>
                <div>
                    <dt class="text-xs font-medium text-gray-500">Tipe</dt>
                    <dd class="mt-1 text-gray-800"><?= esc((string) ($cuti['tipe_cuti'] ?? '')) ?></dd>
                </div>
                <div class="sm:col-span-2">
                    <dt class="text-xs font-medium text-gray-500">Alasan</dt>
                    <dd class="mt-1 text-gray-800"><?= esc((string) ($cuti['alasan_cuti'] ?? '')) ?></dd>
                </div>
                <?php if ($st === 'Rejected' && ! empty($cuti['alasan_tolak'])) : ?>
                    <div class="sm:col-span-2 rounded-xl border border-red-100 bg-red-50/50 p-3">
                        <dt class="text-xs font-semibold text-red-800">Alasan tolak</dt>
                        <dd class="mt-1 text-sm text-red-900"><?= esc((string) $cuti['alasan_tolak']) ?></dd>
                    </div>
                <?php endif ?>
            </dl>
        </section>

        <?php if ($dok !== []) : ?>
            <section class="rounded-2xl border border-gray-200 bg-white p-6 shadow-sm">
                <h2 class="text-sm font-semibold text-gray-900">Dokumen</h2>
                <ul class="mt-3 space-y-2 text-sm">
                    <?php foreach ($dok as $d) : ?>
                        <?php if (! is_array($d)) {
                            continue;
                        } ?>
                        <?php $fn = (string) ($d['dokumen'] ?? ''); ?>
                        <?php if ($fn !== '') : ?>
                            <li>
                                <a href="<?= esc(site_url('admin/cuti/dokumen/' . rawurlencode($fn))) ?>" target="_blank" rel="noopener noreferrer" class="inline-flex items-center gap-2 font-medium text-blue-600 hover:text-blue-800">
                                    <i class="fa-regular fa-file text-gray-400"></i> <?= esc($fn) ?>
                                </a>
                            </li>
                        <?php endif ?>
                    <?php endforeach ?>
                </ul>
            </section>
        <?php endif ?>

        <?php if ($st === 'Waiting') : ?>
            <section class="rounded-2xl border border-gray-200 bg-white p-6 shadow-sm space-y-5">
                <h2 class="text-sm font-semibold text-gray-900">Persetujuan</h2>
                <form method="post" action="<?= site_url('admin/cuti/approve/' . $id) ?>" class="flex flex-wrap gap-2">
                    <?= csrf_field() ?>
                    <button type="submit" class="inline-flex rounded-lg bg-emerald-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-emerald-700">Setujui (Approve)</button>
                </form>
                <form method="post" action="<?= site_url('admin/cuti/reject/' . $id) ?>" class="space-y-3 border-t border-gray-100 pt-5">
                    <?= csrf_field() ?>
                    <label class="block text-xs font-medium text-gray-600">Tolak — alasan wajib</label>
                    <textarea name="alasan_tolak" rows="3" required class="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" placeholder="Jelaskan alasan penolakan"><?= esc(old('alasan_tolak') ?? '') ?></textarea>
                    <button type="submit" class="inline-flex rounded-lg bg-red-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-700">Tolak (Reject)</button>
                </form>
            </section>
        <?php endif ?>
    <?php else : ?>
        <div class="rounded-2xl border border-gray-200 bg-white p-6 shadow-sm">
            <?= view('layouts/partials/empty_state', [
                'icon'  => 'fa-circle-question',
                'title' => 'Cuti tidak ditemukan',
                'hint'  => 'ID tidak valid atau data sudah tidak tersedia.',
            ]) ?>
        </div>
    <?php endif ?>
</div>
<?= $this->endSection() ?>
