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

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

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

        .letterhead {
            text-align: center;
            margin-bottom: 12px;
            border-bottom: 3px double #000;
            padding-bottom: 8px;
        }

        .letterhead img {
            height: 65px;
            margin-bottom: 4px;
        }

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

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

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

        .report-info {
            text-align: center;
            margin: 10px 0 14px;
        }

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

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

        .darjah-section {
            margin-bottom: 16px;
            page-break-inside: avoid;
        }

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

        table.report-table {
            width: 100%;
            border-collapse: collapse;
            border: 1.5px solid #000;
        }

        table.report-table th,
        table.report-table td {
            border: 1px solid #999;
            padding: 2px 4px;
            text-align: center;
            font-size: 9px;
        }

        table.report-table th {
            background: #e9ecef;
            font-size: 8px;
            white-space: nowrap;
        }

        table.report-table td.text-left {
            text-align: left;
        }

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

        .val-negative {
            color: #dc3545;
        }

        .val-positive {
            color: #28a745;
        }

        .empty-msg {
            padding: 8px;
            text-align: center;
            color: #999;
            font-style: italic;
            border: 1.5px solid #000;
            border-top: none;
        }

        .footer {
            margin-top: 20px;
            font-size: 9px;
            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 8mm;
            }

            @page {
                size: A4 landscape;
                margin: 8mm;
            }

            .no-print-btn {
                display: none !important;
            }

            .page-wrap {
                box-shadow: none;
            }
        }

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

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

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

            .no-print-btn button {
                padding: 8px 20px;
                font-size: 14px;
                border: none;
                border-radius: 4px;
                cursor: pointer;
                margin: 0 4px;
            }

            .btn-print {
                background: #007bff;
                color: #fff;
            }

            .btn-print:hover {
                background: #0056b3;
            }

            .btn-close {
                background: #6c757d;
                color: #fff;
            }

            .btn-close:hover {
                background: #545b62;
            }
        }
    </style>
</head>

<body>
    <div class="no-print-btn">
        <button class="btn-print" onclick="window.print()">🖨️ Cetak / Simpan PDF</button>
        <button class="btn-close" onclick="window.close()">✖ 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 Lebihan Buku Kelas Kafa Menengah Tahun {{ $tahun + 1 }}</h4>
            <p><strong>Tahun Data:</strong> {{ $tahun }} &nbsp;|&nbsp;
                @if ($daerah)
                    <strong>Daerah:</strong> {{ $daerah }}
                @else
                    <strong>Semua Daerah Selangor</strong>
                @endif
            </p>
        </div>

        {{-- Tables per Tingkatan --}}
        @foreach ($tables as $table)
            <div class="darjah-section">
                <div class="darjah-header">{{ $table['darjah'] }}</div>
                @if ($table['rows']->isNotEmpty())
                    <table class="report-table">
                        <thead>
                            <tr>
                                <th style="width:25px">Bil</th>
                                <th style="width:60px">Kod Buku</th>
                                <th style="min-width:100px">Judul Buku</th>
                                @foreach ($senarai_daerah as $d)
                                    <th>{{ $d }}</th>
                                @endforeach
                                <th style="width:50px">JUMLAH</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($table['rows'] as $idx => $row)
                                <tr>
                                    <td>{{ $idx + 1 }}</td>
                                    <td>{{ $row['kod_buku'] }}</td>
                                    <td class="text-left">{{ $row['judul'] }}</td>
                                    @foreach ($senarai_daerah as $d)
                                        @php $v = $row['daerah'][$d]; @endphp
                                        <td class="{{ $v < 0 ? 'val-negative' : ($v > 0 ? 'val-positive' : '') }}">
                                            {{ $v != 0 ? number_format($v) : '-' }}
                                        </td>
                                    @endforeach
                                    <td
                                        class="{{ $row['jumlah'] < 0 ? 'val-negative' : ($row['jumlah'] > 0 ? 'val-positive' : '') }}">
                                        <strong>{{ number_format($row['jumlah']) }}</strong>
                                    </td>
                                </tr>
                            @endforeach
                            <tr class="total-row">
                                <td colspan="3" style="text-align:right"><strong>JUMLAH:</strong></td>
                                @foreach ($senarai_daerah as $d)
                                    @php $ct = $table['colTotals'][$d]; @endphp
                                    <td class="{{ $ct < 0 ? 'val-negative' : ($ct > 0 ? 'val-positive' : '') }}">
                                        {{ $ct != 0 ? number_format($ct) : '-' }}
                                    </td>
                                @endforeach
                                <td
                                    class="{{ $table['grandTotal'] < 0 ? 'val-negative' : ($table['grandTotal'] > 0 ? 'val-positive' : '') }}">
                                    <strong>{{ number_format($table['grandTotal']) }}</strong>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                @else
                    <div class="empty-msg">Tiada data lebihan buku untuk tingkatan ini.</div>
                @endif
            </div>
        @endforeach

        {{-- Footer --}}
        <div class="footer">
            <div class="signature">
                <div class="line">
                    <strong>Disediakan oleh:</strong><br>
                    Pentadbir Sistem MySpBT
                </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>
</body>

</html>
