<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cetak Alur Tujuan Pembelajaran (ATP)</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: 20px;
        }
        .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 header */
            -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: 8px; 
            text-align: left; 
            vertical-align: top; 
            font-size: 10pt;
        }
        table.table-data th { 
            background-color: #e2e8f0 !important; 
            text-align: center;
            font-weight: bold; 
        }

        /* Utility Classes */
        .text-center { text-align: center !important; }
        .align-middle { vertical-align: middle !important; }
        .fw-bold { font-weight: bold !important; }

        /* --- TANDA TANGAN --- */
        .signature-table { 
            border: none; 
            width: 100%; 
            margin-top: 40px; 
            font-size: 10pt;
        }
        .signature-table td { 
            border: none; 
            vertical-align: top; 
            text-align: left; /* DIUBAH MENJADI RATA KIRI */
        }
        
        @media print {
            .no-print { display: none; }
        }
    </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>ALUR TUJUAN PEMBELAJARAN (ATP)</h3>
        <p>Tahun Pelajaran {{ date('Y') }}/{{ date('Y')+1 }}</p>
    </div>

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

    @php
        // Group by Fase dan urutkan
        $groupedAtp = $atp->sortBy('phase')->groupBy('phase');
        $nomor = 1;
    @endphp

    <table class="table-data">
        <thead>
            <tr>
                <th width="4%" class="text-center align-middle">No</th>
                <th width="8%" class="text-center align-middle">Fase</th>
                <th width="38%" class="text-center align-middle">Capaian Pembelajaran (CP)</th>
                <th width="40%" class="text-center align-middle">Tujuan Pembelajaran (TP)</th>
                <th width="10%" class="text-center align-middle">Alokasi (JP)</th>
            </tr>
        </thead>
        <tbody>
            @forelse($groupedAtp as $phase => $items)
                @php
                    $items = $items->values();
                    $itemCount = $items->count();
                    $totalJp = 0;
                    
                    // Menghitung Rowspan untuk Kompetensi Inti / CP
                    $rowspansCp = [];
                    $currentCp = null;
                    $startIndex = 0;
                    
                    foreach ($items as $index => $item) {
                        // Ambil angka saja dari input alokasi waktu untuk ditotalkan
                        $totalJp += (int) filter_var($item->alokasi_waktu, FILTER_SANITIZE_NUMBER_INT);
                        
                        if ($item->learning_outcome === $currentCp) {
                            $rowspansCp[$startIndex]++;
                            $rowspansCp[$index] = 0; // Tandai agar td tidak dirender
                        } else {
                            $currentCp = $item->learning_outcome;
                            $startIndex = $index;
                            $rowspansCp[$startIndex] = 1;
                        }
                    }
                @endphp

                @foreach($items as $index => $item)
                    <tr>
                        <td class="text-center align-middle">{{ $nomor++ }}</td>
                        
                        {{-- MERGE FASE UNTUK SATU GRUP --}}
                        @if($index === 0)
                            <td rowspan="{{ $itemCount }}" class="text-center align-middle fw-bold" style="font-size: 14pt;">
                                {{ $phase }}
                            </td>
                        @endif

                        {{-- MERGE CAPAIAN PEMBELAJARAN (CP) --}}
                        @if($rowspansCp[$index] > 0)
                            <td rowspan="{{ $rowspansCp[$index] }}" class="align-middle" style="white-space: pre-line; background-color: #fafafa;">
                                {{ $item->learning_outcome ?? '-' }}
                            </td>
                        @endif

                        {{-- TUJUAN PEMBELAJARAN (TP) --}}
                        <td class="align-middle" style="white-space: pre-line;">
                            {{ $item->objective_description ?? '-' }}
                        </td>

                        {{-- ALOKASI WAKTU --}}
                        <td class="text-center align-middle">
                            {{ $item->alokasi_waktu ?? 0 }} JP
                        </td>
                    </tr>
                @endforeach
                
                {{-- BARIS SUBTOTAL ALOKASI WAKTU --}}
                <tr style="background-color: #f1f5f9;">
                    <td colspan="4" style="text-align: right; padding-right: 15px; font-weight: bold;">
                        Total Alokasi Waktu Fase {{ $phase }} :
                    </td>
                    <td class="text-center align-middle fw-bold" style="font-size: 11pt;">
                        {{ $totalJp }} JP
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="5" class="text-center" style="padding: 20px;">
                        Data Alur Tujuan Pembelajaran belum tersedia.
                    </td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <table class="signature-table">
        <tr>
            <td width="35%" style="padding-left: 20px;">
                Mengetahui,<br>
                Kepala Sekolah
                <br><br><br><br><br>
                <strong style="text-decoration: underline;">
                    {{ $school->principal_name ?? '............................................' }}
                </strong><br>
                NIP. {{ $school->principal_nip ?? '....................................' }}
            </td>
            
            <td width="30%"></td> <td width="35%">
                Makassar, {{ $tanggalCetak ?? \Carbon\Carbon::now()->locale('id')->translatedFormat('d F Y') }}<br>
                Guru Mata Pelajaran
                <br><br><br><br><br>
                <strong style="text-decoration: underline;">
                    {{ $guru->name ?? Auth::user()->name ?? '............................................' }}
                </strong><br>
                NIP. {{ $guru->nip ?? Auth::user()->nip ?? '....................................' }}
            </td>
        </tr>
    </table>

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