<!doctype html>
<html lang="id">

<head>
    <meta charset="utf-8">
    <title>Detail Absensi {{ $monthLabel }} - {{ $user->name }}</title>
    <style>
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 11px;
            color: #111827;
        }

        .header {
            margin-bottom: 12px;
        }

        .title {
            font-size: 16px;
            font-weight: 800;
            margin: 0;
        }

        .sub {
            margin: 4px 0 0;
            color: #374151;
        }

        .meta {
            margin-top: 6px;
            color: #374151;
            font-size: 10.5px;
        }

        .meta b {
            color: #111827;
        }

        .summary-wrap {
            margin: 12px 0 14px;
        }

        .summary {
            width: 100%;
            border-collapse: collapse;
        }

        .summary td {
            border: 1px solid #e5e7eb;
            padding: 10px 12px;
            vertical-align: middle;
        }

        .summary .box {
            width: 25%;
            text-align: center;
            font-weight: 700;
        }

        .summary .num {
            font-size: 16px;
            font-weight: 800;
            line-height: 1.2;
        }

        .summary .lbl {
            font-size: 10.5px;
            color: #374151;
            font-weight: 700;
            margin-top: 2px;
        }

        .c-hadir {
            background: #eafaf5;
        }

        .c-izin {
            background: #ecfdf5;
        }

        .c-sakit {
            background: #fff7ed;
        }

        .c-alpha {
            background: #fef2f2;
        }

        table.data {
            width: 100%;
            border-collapse: collapse;
        }

        table.data th,
        table.data td {
            border: 1px solid #e5e7eb;
            padding: 8px;
        }

        table.data th {
            background: #f3f4f6;
            text-align: left;
            font-weight: 800;
        }

        .center {
            text-align: center;
        }

        .nowrap {
            white-space: nowrap;
        }

        .badge {
            display: inline-block;
            padding: 3px 9px;
            border-radius: 999px;
            font-size: 10px;
            font-weight: 800;
        }

        .b-hadir {
            background: #d1fae5;
            color: #065f46;
        }

        .b-izin {
            background: #dcfce7;
            color: #166534;
        }

        .b-sakit {
            background: #ffedd5;
            color: #9a3412;
        }

        .b-alpha {
            background: #fee2e2;
            color: #991b1b;
        }

        .b-def {
            background: #e5e7eb;
            color: #374151;
        }

        .footer-note {
            margin-top: 10px;
            font-size: 10px;
            color: #6b7280;
        }
    </style>
</head>

<body>

    @php
        use Carbon\Carbon;

        $badgeClass = function ($status) {
            return match ($status) {
                'hadir' => 'b-hadir',
                'izin' => 'b-izin',
                'sakit' => 'b-sakit',
                'alpha' => 'b-alpha',
                default => 'b-def',
            };
        };

        $formatTime = function ($t) {
            if (!$t) {
                return '-';
            }
            try {
                return Carbon::parse($t)->format('H:i');
            } catch (\Throwable $e) {
                return (string) $t;
            }
        };

        $duration = function ($date, $in, $out) {
            if (!$in || !$out) {
                return '-';
            }
            try {
                $start = Carbon::parse($date . ' ' . $in);
                $end = Carbon::parse($date . ' ' . $out);
                if ($end->lt($start)) {
                    $end->addDay();
                }
                $sec = $start->diffInSeconds($end);
                return gmdate('H:i', $sec);
            } catch (\Throwable $e) {
                return '-';
            }
        };
    @endphp

    <div class="header">
        <p class="title">Detail Absensi Desa</p>
        <p class="sub">
            Periode: <b>{{ $monthLabel }}</b> ({{ $start }} s/d {{ $end }})
        </p>
        <div class="meta">
            Nama: <b>{{ $user->name }}</b> &nbsp;|&nbsp;
            Jabatan: <b>{{ $user->jabatan ?? '-' }}</b>
        </div>
    </div>

    <div class="summary-wrap">
        <table class="summary">
            <tr>
                <td class="box c-hadir">
                    <div class="num">{{ (int) ($summary['hadir'] ?? 0) }}</div>
                    <div class="lbl">Hadir</div>
                </td>
                <td class="box c-izin">
                    <div class="num">{{ (int) ($summary['izin'] ?? 0) }}</div>
                    <div class="lbl">Izin</div>
                </td>
                <td class="box c-sakit">
                    <div class="num">{{ (int) ($summary['sakit'] ?? 0) }}</div>
                    <div class="lbl">Sakit</div>
                </td>
                <td class="box c-alpha">
                    <div class="num">{{ (int) ($summary['alpha'] ?? 0) }}</div>
                    <div class="lbl">Alpha</div>
                </td>
            </tr>
        </table>
    </div>

    <table class="data">
        <thead>
            <tr>
                <th style="width: 130px;">Tanggal</th>
                <th class="center" style="width: 70px;">Datang</th>
                <th class="center" style="width: 70px;">Pulang</th>
                <th class="center" style="width: 70px;">Durasi</th>
                <th class="center" style="width: 90px;">Status</th>
                <th>Keterangan</th>
            </tr>
        </thead>
        <tbody>
            @php

                $badgeClass = function ($status) {
                    return match ($status) {
                        'hadir' => 'b-hadir',
                        'izin' => 'b-izin',
                        'sakit' => 'b-sakit',
                        'alpha' => 'b-alpha',
                        default => 'b-def',
                    };
                };

                // ✅ normalize datetime:
                // - kalau $t sudah ada tanggal (YYYY-MM-DD...), parse langsung
                // - kalau hanya jam (HH:MM:SS), gabungkan dengan tanggal dari $date
                $normalizeDT = function ($date, $t) {
                    if (!$t) {
                        return null;
                    }

                    // kalau sudah Carbon instance
                    if ($t instanceof \Carbon\CarbonInterface) {
                        return $t;
                    }

                    $t = (string) $t;

                    // kalau string mengandung tanggal YYYY-MM-DD
                    if (preg_match('/\d{4}-\d{2}-\d{2}/', $t)) {
                        return Carbon::parse($t);
                    }

                    // date-only dari $date (meski $date ada 00:00:00)
                    $dateOnly = Carbon::parse($date)->toDateString();
                    return Carbon::parse($dateOnly . ' ' . $t);
                };

                $formatTime = function ($date, $t) use ($normalizeDT) {
                    $dt = $normalizeDT($date, $t);
                    return $dt ? $dt->format('H:i') : '-';
                };

                $duration = function ($date, $in, $out) use ($normalizeDT) {
                    $inDT = $normalizeDT($date, $in);
                    $outDT = $normalizeDT($date, $out);

                    if (!$inDT || !$outDT) {
                        return '-';
                    }

                    // aman kalau checkout lewat tengah malam
                    if ($outDT->lt($inDT)) {
                        $outDT = $outDT->copy()->addDay();
                    }

                    $sec = $inDT->diffInSeconds($outDT);
                    return gmdate('H:i', $sec);
                };
            @endphp
            @forelse($items as $it)
                @php
                    $dateLabel = Carbon::parse($it->date)->translatedFormat('d F Y');
                    $st = $it->status ?? '-';
                    $notes = $it->notes ?? '-';

                    $in = $formatTime($it->date, $it->check_in);
                    $out = $formatTime($it->date, $it->check_out);
                    $dur = $duration($it->date, $it->check_in, $it->check_out);

                    $bClass = $badgeClass($st);
                @endphp
                <tr>
                    <td class="nowrap">{{ $dateLabel }}</td>
                    <td class="center nowrap">{{ $in }}</td>
                    <td class="center nowrap">{{ $out }}</td>
                    <td class="center nowrap">{{ $dur }}</td>
                    <td class="center">
                        <span class="badge {{ $bClass }}">{{ ucfirst($st) }}</span>
                    </td>
                    <td>{{ $notes }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="6" class="center" style="padding: 16px; color:#6b7280;">
                        Tidak ada riwayat absensi pada periode ini.
                    </td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <div class="footer-note">
        Catatan: Durasi dihitung dari Datang → Pulang (HH:MM). Jika salah satu kosong, durasi ditampilkan "-".
    </div>

</body>

</html>
