<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cetak Program Semester</title>
    <style>
        /* --- PENGATURAN KERTAS --- */
        @page { 
            size: A4 landscape; 
            margin: 1cm 1.5cm; 
        }
        body { 
            font-family: 'Times New Roman', Times, serif; 
            font-size: 10pt; 
            color: #000;
            margin: 0;
            padding: 0;
        }

        /* --- KOP SURAT --- */
        .kop-surat {
            width: 100%;
            border-bottom: 3px solid #000;
            margin-bottom: 2px;
        }
        .kop-surat td {
            text-align: center;
            padding-bottom: 5px;
        }
        .kop-teks-1 { font-size: 11pt; font-weight: bold; margin: 0; }
        .kop-teks-2 { font-size: 10pt; font-weight: bold; margin: 0; }
        .kop-teks-3 { font-size: 14pt; font-weight: bold; letter-spacing: 1px; margin: 0; }
        .kop-teks-4 { font-size: 9pt; font-style: normal; margin: 0; }
        .garis-ganda {
            border-top: 1px solid #000;
            margin-bottom: 15px;
        }

        /* --- JUDUL DOKUMEN --- */
        .judul-dokumen {
            text-align: center;
            margin-bottom: 15px;
        }
        .judul-dokumen h3 {
            margin: 0;
            font-size: 12pt;
            text-decoration: underline;
            font-weight: bold;
        }
        .judul-dokumen p { margin: 5px 0 0 0; font-size: 10pt; }

        /* --- INFO MAPEL --- */
        .meta { width: 100%; margin-bottom: 15px; border-collapse: collapse; font-size: 10pt; }
        .meta td { padding: 2px 0; vertical-align: top; }

        /* --- TABEL UTAMA --- */
        table.table-data { 
            width: 100%; 
            border-collapse: collapse; 
            margin-bottom: 20px; 
            /* Memaksa browser mencetak warna background cell */
            -webkit-print-color-adjust: exact !important; 
            color-adjust: exact !important;
            print-color-adjust: exact !important;
        }
        table.table-data th, table.table-data td { 
            border: 1px solid #000; 
            padding: 4px; 
            text-align: center; 
            vertical-align: middle; 
            font-size: 9.5pt;
        }
        table.table-data th { 
            background-color: #e2e8f0 !important; 
            font-weight: bold; 
        }
        td.text-start { 
            text-align: left !important; 
            padding-left: 6px !important; 
        }

        /* --- TANDA TANGAN --- */
        .signature-table { 
            border: none; 
            width: 100%; 
            margin-top: 30px; 
            font-size: 10pt;
        }
        .signature-table td { 
            border: none; 
            vertical-align: top; 
            text-align: center; /* Diubah jadi center agar rapi */
        }
        
        /* Warna untuk penanda minggu efektif */
        .cell-active {
            background-color: #94a3b8 !important; /* Warna abu-abu gelap agar jelas saat diprint hitam-putih */
        }
    </style>
</head>
<body>

    <table class="kop-surat">
        <tr>
            <td>
                <div class="kop-teks-1">PEMERINTAH PROVINSI SULAWESI SELATAN</div>
                <div class="kop-teks-2">DINAS PENDIDIKAN</div>
                <div class="kop-teks-3">{{ $school->name ?? 'SMK NEGERI 10 MAKASSAR' }}</div>
                <div class="kop-teks-4">Jl. Bonto Manai No.14, Gn. Sari, Kec. Rappocini, Kota Makassar, Sulawesi Selatan 90221</div>
                <div class="kop-teks-4">Telepon: (0411) 868465 | Website: smkn10makassar.sch.id</div>
            </td>
        </tr>
    </table>
    <div class="garis-ganda"></div>

    <div class="judul-dokumen">
        <h3>PROGRAM SEMESTER {{ strtoupper($semesterFilter) }}</h3>
        <p>Tahun Pelajaran {{ $tahunAjaran ?? '....... / .......' }}</p>
    </div>

    <table class="meta">
        <tr>
            <td width="120"><strong>Mata Pelajaran</strong></td>
            <td width="10">:</td>
            <td>{{ $data->first()->mapel->name ?? $data->first()->subject->name ?? '....................................' }}</td>
        </tr>
        <tr>
            <td><strong>Guru Pengampu</strong></td>
            <td>:</td>
            <td>{{ $guru->name ?? '....................................' }}</td>
        </tr>
    </table>

    <table class="table-data">
        <thead>
            <tr>
                <th rowspan="2" width="3%">No</th>
                <th rowspan="2" width="30%">Kompetensi Dasar / Materi Pokok</th>
                <th rowspan="2" width="4%">JP</th>
                
                @foreach($months as $month)
                    <th colspan="5">{{ $month }}</th>
                @endforeach
                
                <th rowspan="2" width="8%">Ket</th>
            </tr>
            <tr>
                @foreach($months as $month)
                    @for($i=1; $i<=5; $i++)
                        <th width="1.8%">{{ $i }}</th>
                    @endfor
                @endforeach
            </tr>
        </thead>
        <tbody>
            @forelse($data as $item)
            <tr>
                <td>{{ $loop->iteration }}</td>
                
                <td class="text-start">
                    {{ $item->materi_pokok ?? $item->kompetensi_dasar ?? $item->topic ?? '-' }}
                </td>
                
                <td>{{ $item->jp ?? $item->alokasi_waktu ?? $item->allocation_hours ?? 0 }}</td>

                @foreach($months as $month)
                    @for($w=1; $w<=5; $w++)
                        @php
                            $distribusi = $item->monthly_distribution ?? [];
                            $isActive = false;

                            if (isset($distribusi[$month]) && is_array($distribusi[$month])) {
                                if (in_array((string)$w, $distribusi[$month])) {
                                    $isActive = true;
                                }
                            }
                        @endphp
                        
                        <td class="{{ $isActive ? 'cell-active' : '' }}"></td>
                    @endfor
                @endforeach

                <td>{{ $item->keterangan ?? '' }}</td>
            </tr>
            @empty
            <tr>
                <td colspan="{{ 4 + (count($months) * 5) }}" style="padding: 20px;">
                    Data tidak ditemukan. Pastikan data sudah diinput untuk semester ini.
                </td>
            </tr>
            @endforelse
        </tbody>
    </table>

    <table class="signature-table">
        <tr>
            <td width="35%">
                Mengetahui,<br>
                Kepala Sekolah
                <br><br><br><br><br>
                <strong><u>{{ $school->principal_name ?? '............................................' }}</u></strong><br>
                NIP. {{ $school->principal_nip ?? '....................................' }}
            </td>
            
            <td width="30%"></td> <td width="35%">
                Makassar, {{ $tanggalCetak }}<br>
                Guru Mata Pelajaran
                <br><br><br><br><br>
                <strong><u>{{ $guru->name ?? '............................................' }}</u></strong><br>
                NIP. {{ $guru->nip ?? '....................................' }}
            </td>
        </tr>
    </table>

    <script>
        // Otomatis membuka dialog print saat halaman dimuat
        window.print();
    </script>
</body>
</html>
