{{-- resources/views/admin/laporan/pdf/pemeriksaan.blade.php --}}
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Laporan Pemeriksaan</title>
<style>
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body { font-family: 'DejaVu Sans', Arial, sans-serif; font-size: 11px; color: #1a202c; background: #fff; }

    .header { background: #0f4c81; color: #fff; padding: 20px 28px; display: flex; justify-content: space-between; align-items: center; }
    .header h1 { font-size: 18px; font-weight: 700; }
    .header p  { font-size: 10px; opacity: .8; margin-top: 4px; }
    .logo-area  { font-size: 32px; }

    .meta { padding: 14px 28px; background: #f7fafc; border-bottom: 1px solid #e2e8f0; display: flex; gap: 32px; }
    .meta-item label { font-size: 9px; text-transform: uppercase; letter-spacing: .06em; color: #718096; display: block; }
    .meta-item span  { font-weight: 700; font-size: 12px; }

    .stats-row { display: flex; gap: 0; border-bottom: 2px solid #0f4c81; }
    .stat-box  { flex: 1; padding: 10px 14px; text-align: center; border-right: 1px solid #e2e8f0; }
    .stat-box:last-child { border-right: none; }
    .stat-box .num { font-size: 20px; font-weight: 800; color: #0f4c81; }
    .stat-box .lbl { font-size: 9px; color: #718096; margin-top: 2px; }

    table { width: 100%; border-collapse: collapse; margin-top: 16px; }
    thead tr { background: #0f4c81; color: #fff; }
    thead th { padding: 8px 10px; text-align: left; font-size: 9px; text-transform: uppercase; letter-spacing: .05em; font-weight: 700; }
    tbody tr:nth-child(even) { background: #f7fafc; }
    tbody tr:hover { background: #ebf8ff; }
    tbody td { padding: 7px 10px; border-bottom: 1px solid #e2e8f0; vertical-align: middle; }

    .badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 9px; font-weight: 700; }
    .n  { background: #c6f6d5; color: #22543d; }
    .dr { background: #fefcbf; color: #744210; }
    .ds { background: #feebc8; color: #7b341e; }
    .dt { background: #fed7d7; color: #822727; }
    .hi { background: #bee3f8; color: #2a4365; }
    .sf { background: #e9d8fd; color: #44337a; }

    .footer { margin-top: 24px; padding: 12px 28px; border-top: 1px solid #e2e8f0; text-align: right; font-size: 9px; color: #a0aec0; }
    .content { padding: 16px 28px; }
</style>
</head>
<body>

<div class="header">
    <div>
        <h1>📊 Laporan Pemeriksaan Kesehatan</h1>
        <p>Klinik Kesehatan Digital &mdash; {{ env('APP_NAMA_KLINIK', 'Klinik') }}</p>
    </div>
    <div class="logo-area">🏥</div>
</div>

<div class="meta">
    <div class="meta-item"><label>Periode</label><span>{{ $mulai->format('d M Y') }} s.d. {{ $selesai->format('d M Y') }}</span></div>
    <div class="meta-item"><label>Dicetak</label><span>{{ now()->format('d M Y H:i') }}</span></div>
    <div class="meta-item"><label>Dicetak Oleh</label><span>{{ auth()->user()->name ?? '-' }}</span></div>
    <div class="meta-item"><label>Total Data</label><span>{{ $data->count() }} Pemeriksaan</span></div>
</div>

@php
    $total  = $data->count();
    $normal = $data->where('kategori_suhu','NORMAL')->count();
    $demam  = $data->whereIn('kategori_suhu',['DEMAM_RINGAN','DEMAM_SEDANG','DEMAM_TINGGI'])->count();
    $avgSuhu = round($data->avg('suhu_tubuh'), 1);
@endphp

<div class="stats-row">
    <div class="stat-box"><div class="num">{{ $total }}</div><div class="lbl">Total Pemeriksaan</div></div>
    <div class="stat-box"><div class="num">{{ $normal }}</div><div class="lbl">Normal</div></div>
    <div class="stat-box"><div class="num">{{ $demam }}</div><div class="lbl">Demam</div></div>
    <div class="stat-box"><div class="num">{{ $data->where('kategori_suhu','DEMAM_TINGGI')->count() }}</div><div class="lbl">Demam Tinggi</div></div>
    <div class="stat-box"><div class="num">{{ $avgSuhu }}°C</div><div class="lbl">Rata-rata Suhu</div></div>
</div>

<div class="content">
    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Kode</th>
                <th>Tanggal & Waktu</th>
                <th>Nama Pasien</th>
                <th>No. RM</th>
                <th>Suhu (°C)</th>
                <th>Kategori</th>
                <th>Jantung</th>
                <th>SpO₂</th>
                <th>Keluhan</th>
                <th>Petugas</th>
            </tr>
        </thead>
        <tbody>
            @foreach($data as $i => $p)
            @php
                $catMap = [
                    'NORMAL'=>'n','HIPOTERMIA'=>'hi','SUBFEBRIS'=>'sf',
                    'DEMAM_RINGAN'=>'dr','DEMAM_SEDANG'=>'ds','DEMAM_TINGGI'=>'dt'
                ];
                $cls = $catMap[$p->kategori_suhu] ?? 'n';
            @endphp
            <tr>
                <td>{{ $i+1 }}</td>
                <td style="font-size:9px;color:#4a5568">{{ $p->kode_pemeriksaan ?? '-' }}</td>
                <td>{{ optional($p->waktu_pemeriksaan)->format('d/m/Y H:i') }}</td>
                <td style="font-weight:600">{{ optional($p->pasien)->nama_lengkap ?? '-' }}</td>
                <td>{{ optional($p->pasien)->no_rekam_medis ?? '-' }}</td>
                <td style="font-weight:700">{{ $p->suhu_tubuh }}°</td>
                <td><span class="badge {{ $cls }}">{{ str_replace('_',' ',$p->kategori_suhu) }}</span></td>
                <td>{{ $p->detak_jantung ? $p->detak_jantung.'bpm' : '-' }}</td>
                <td>{{ $p->saturasi_oksigen ? $p->saturasi_oksigen.'%' : '-' }}</td>
                <td>{{ $p->keluhan_utama ? \Str::limit($p->keluhan_utama, 25) : '-' }}</td>
                <td>{{ optional($p->petugas)->name ?? '-' }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
</div>

<div class="footer">
    Dokumen ini digenerate otomatis oleh Sistem Informasi Klinik &bull;
    {{ env('APP_NAMA_KLINIK', 'Klinik Kesehatan Digital') }} &bull;
    {{ now()->format('d M Y H:i:s') }}
</div>

</body>
</html>