<!DOCTYPE html>
<html lang="ms">

<head>
    <meta charset="UTF-8">
    <title>Laporan Pesanan Buku — {{ $daerah }} — {{ $tahun }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            font-size: 11px;
            color: #000;
            padding: 15mm 15mm 10mm 15mm;
        }

        /* ── Letterhead ── */
        .letterhead {
            text-align: center;
            margin-bottom: 15px;
            border-bottom: 3px double #000;
            padding-bottom: 10px;
        }

        .letterhead img {
            height: 70px;
            margin-bottom: 5px;
        }

        .letterhead h1 {
            font-size: 16px;
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 2px;
        }

        .letterhead h2 {
            font-size: 13px;
            text-transform: uppercase;
            margin-bottom: 1px;
        }

        .letterhead h3 {
            font-size: 12px;
            text-transform: uppercase;
            font-weight: normal;
        }

        /* ── Report Info ── */
        .report-info {
            text-align: center;
            margin: 12px 0;
        }

        .report-info h4 {
            font-size: 13px;
            text-transform: uppercase;
            text-decoration: underline;
            margin-bottom: 4px;
        }

        .report-info p {
            font-size: 11px;
        }

        /* ── Darjah Grid ── */
        .darjah-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 12px;
            margin-top: 10px;
        }

        .darjah-box {
            border: 1.5px solid #000;
            page-break-inside: avoid;
        }

        .darjah-header {
            background: #333;
            color: #fff;
            padding: 5px 8px;
            font-size: 12px;
            font-weight: bold;
            text-align: center;
        }

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

        .darjah-box th,
        .darjah-box td {
            border: 1px solid #999;
            padding: 3px 6px;
            text-align: left;
        }

        .darjah-box th {
            background: #eee;
            font-size: 10px;
            text-align: center;
        }

        .darjah-box td {
            font-size: 10px;
        }

        .darjah-box td.text-center {
            text-align: center;
        }

        .darjah-box td.text-right {
            text-align: right;
        }

        .total-row td {
            font-weight: bold;
            background: #f5f5f5;
        }

        .empty-msg {
            padding: 10px;
            text-align: center;
            color: #999;
            font-style: italic;
        }

        /* ── Footer ── */
        .footer {
            margin-top: 20px;
            font-size: 10px;
            display: flex;
            justify-content: space-between;
        }

        .footer .signature {
            margin-top: 30px;
            width: 45%;
        }

        .signature .line {
            border-top: 1px solid #000;
            margin-top: 40px;
            padding-top: 3px;
        }

        @media print {
            body {
                padding: 5mm 10mm;
            }

            @page {
                size: A4 portrait;
                margin: 10mm;
            }
        }

        @media screen {
            body {
                max-width: 210mm;
                margin: 0 auto;
                background: #f0f0f0;
                padding: 20mm;
            }

            .page-wrap {
                background: #fff;
                padding: 15mm;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
            }

            .no-print-btn {
                text-align: center;
                margin-bottom: 15px;
            }

            .no-print-btn button {
                padding: 8px 20px;
                font-size: 14px;
                background: #007bff;
                color: #fff;
                border: none;
                border-radius: 4px;
                cursor: pointer;
            }

            .no-print-btn button:hover {
                background: #0056b3;
            }
        }
    </style>
</head>

<body>
    <div class="no-print-btn" style="@media print { display:none; }">
        <button onclick="window.print()">🖨️ Cetak / Simpan PDF</button>
        <button onclick="window.close()" style="background:#6c757d">✖ Tutup</button>
    </div>

    <div class="page-wrap">
        {{-- Letterhead --}}
        <div class="letterhead">
            <img src="{{ asset('image/JAIS-Logo (3).png') }}" alt="Logo JAIS">
            <h1>Skim Pinjaman Buku Teks</h1>
            <h2>Bahagian Pendidikan Islam</h2>
            <h3>Jabatan Agama Islam Selangor</h3>
        </div>

        {{-- Report Info --}}
        <div class="report-info">
            <h4>Laporan Pesanan Buku Tahun {{ $tahun + 1 }}</h4>
            <p><strong>Daerah:</strong> {{ $daerah }} &nbsp;|&nbsp;
                <strong>Tahun Data:</strong> {{ $tahun }} &nbsp;|&nbsp;
                <strong>Penyelia BOSS:</strong> {{ $supervisor->name }}
            </p>
        </div>

        {{-- 6 Darjah Boxes --}}
        <div class="darjah-grid">
            @foreach ($charts as $chart)
                <div class="darjah-box">
                    <div class="darjah-header">{{ $chart['darjah'] }}</div>
                    @if ($chart['data']->isNotEmpty())
                        <table>
                            <thead>
                                <tr>
                                    <th style="width:30px">Bil</th>
                                    <th style="width:80px">Kod Buku</th>
                                    <th>Judul Buku</th>
                                    <th style="width:70px">Jumlah Pesanan</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($chart['data'] as $idx => $row)
                                    <tr>
                                        <td class="text-center">{{ $idx + 1 }}</td>
                                        <td class="text-center">{{ $row->kod_buku }}</td>
                                        <td>{{ $row->judul }}</td>
                                        <td class="text-center">{{ number_format($row->total_pesanan) }}</td>
                                    </tr>
                                @endforeach
                                <tr class="total-row">
                                    <td colspan="3" class="text-right" style="text-align:right">Jumlah:</td>
                                    <td class="text-center">{{ number_format($chart['total']) }}</td>
                                </tr>
                            </tbody>
                        </table>
                    @else
                        <div class="empty-msg">Tiada data pesanan</div>
                    @endif
                </div>
            @endforeach
        </div>

        {{-- Footer / Signature --}}
        <div class="footer">
            <div class="signature">
                <div class="line">
                    <strong>Disediakan oleh:</strong><br>
                    {{ $supervisor->name }}<br>
                    Penyelia BOSS Daerah {{ $daerah }}
                </div>
            </div>
            <div class="signature" style="text-align:right">
                <div class="line">
                    <strong>Tarikh Cetak:</strong><br>
                    {{ now()->format('d/m/Y H:i') }}
                </div>
            </div>
        </div>
    </div>

    <script>
        // Hide print buttons when printing
        const style = document.createElement('style');
        style.textContent =
            '@media print { .no-print-btn { display: none !important; } .page-wrap { box-shadow: none; } body { background: #fff; padding: 0; } }';
        document.head.appendChild(style);
    </script>
</body>

</html>
