<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Logbook Limbah B3</title>
    <style>
        @page { size: A4 landscape; margin: 12mm; }
        * { box-sizing: border-box; }
        body { font-family: Arial, Helvetica, sans-serif; color: #000; font-size: 11px; margin: 0; padding: 16px; }
        h1 { text-align: center; font-size: 15px; margin: 0 0 4px; }
        .periode { text-align: center; font-size: 11px; margin-bottom: 10px; }
        table { width: 100%; border-collapse: collapse; }
        th, td { border: 1px solid #000; padding: 4px 6px; vertical-align: top; }
        th { background: #f0f0f0; text-align: center; font-weight: bold; }
        td.num { text-align: right; white-space: nowrap; }
        td.ctr { text-align: center; white-space: nowrap; }
        thead { display: table-header-group; }   /* header berulang tiap halaman */
        tr { page-break-inside: avoid; }
        .toolbar { margin-bottom: 12px; }
        button { padding: 8px 14px; font-size: 13px; cursor: pointer; }
        @media print {
            .toolbar { display: none; }
            body { padding: 0; }
        }
    </style>
</head>
<body>
    <div class="toolbar">
        <button onclick="window.print()">Cetak / Simpan PDF</button>
    </div>

    <h1>LOGBOOK LIMBAH BAHAN BERBAHAYA BERACUN ( B3 )</h1>
    <div class="periode">Periode: {{ $periodeMulai }} s/d {{ $periodeSelesai }}</div>

    @php
        // Format angka ala Indonesia, buang desimal nol (2,00 -> 2 ; 1,50 -> 1,5).
        $fmt = fn ($v) => $v === null ? '' : rtrim(rtrim(number_format($v, 2, ',', '.'), '0'), ',');
    @endphp

    <table>
        <thead>
            <tr>
                <th rowspan="2">No</th>
                <th colspan="5">MASUKNYA LIMBAH B3 KE TEMPAT PENYIMPANAN</th>
                <th colspan="4">KELUARNYA LIMBAH B3 DARI TEMPAT PENYIMPANAN</th>
                <th>SISA</th>
            </tr>
            <tr>
                <th>Jenis Limbah B3 Masuk</th>
                <th>Tanggal</th>
                <th>Sumber Limbah B3</th>
                <th>Jumlah Limbah B3 Masuk</th>
                <th>Maksimal penyimpanan s/d tanggal (t=0 + 90 hr) atau *(t=0 + 365 hari)</th>
                <th>Tanggal Keluar Limbah</th>
                <th>Jumlah Limbah B3</th>
                <th>Tujuan Penyerahan</th>
                <th>Bukti No Dokumen</th>
                <th>Sisa Limbah B3</th>
            </tr>
        </thead>
        <tbody>
            @forelse ($rows as $r)
                <tr>
                    <td class="ctr">{{ $r['no'] }}</td>
                    <td>{{ $r['jenis'] }}</td>
                    <td class="ctr">{{ $r['tgl_masuk'] }}</td>
                    <td>{{ $r['sumber'] }}</td>
                    <td class="num">{{ $fmt($r['jml_masuk']) }} {{ $r['jml_masuk'] !== null ? $r['satuan'] : '' }}</td>
                    <td class="ctr">{{ $r['maks_simpan'] }}</td>
                    <td class="ctr">{{ $r['tgl_keluar'] }}</td>
                    <td class="num">{{ $fmt($r['jml_keluar']) }} {{ $r['jml_keluar'] !== null ? $r['satuan'] : '' }}</td>
                    <td>{{ $r['tujuan'] }}</td>
                    <td>{{ $r['nodokumen'] }}</td>
                    <td class="num">{{ $fmt($r['sisa']) }} {{ $r['satuan'] }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="11" class="ctr">Tidak ada data pada periode ini.</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    @if ($autoprint)
        <script>window.addEventListener('load', () => window.print());</script>
    @endif
</body>
</html>