<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Cetak Modul Ajar</title>
    <style>
        /* Reset & Base */
        @page { size: A4; margin: 2cm 2.5cm; }
        body { font-family: 'Times New Roman', Times, serif; font-size: 11pt; line-height: 1.3; color: #000; }
        
        /* Helpers */
        .text-center { text-align: center; }
        .text-bold { font-weight: bold; }
        .uppercase { text-transform: uppercase; }
        .page-break { page-break-after: always; }
        
        /* Judul Dokumen */
        .doc-title { margin-bottom: 20px; border-bottom: 3px double #000; padding-bottom: 10px; }
        .doc-title h3 { margin: 0; font-size: 14pt; letter-spacing: 1px; }
        .doc-title h4 { margin: 5px 0 0; font-size: 12pt; font-weight: normal; }

        /* Tabel Layout Utama */
        .section-box { width: 100%; border: 1px solid #000; margin-bottom: 15px; border-collapse: collapse; }
        .section-header { 
            background-color: #f0f0f0; 
            font-weight: bold; 
            padding: 5px 10px; 
            border-bottom: 1px solid #000;
            font-size: 11pt;
        }
        
        /* Tabel Informasi Detail */
        .info-table { width: 100%; border-collapse: collapse; }
        .info-table td { padding: 4px 8px; vertical-align: top; }
        .info-label { width: 180px; font-weight: bold; }
        .info-sep { width: 10px; }

        /* Area Konten */
        .content-padding { padding: 10px; }
        .content-justify { text-align: justify; } /* TAMBAHAN: Agar teks rata kanan kiri */
        .sub-header { font-weight: bold; margin-bottom: 5px; margin-top: 10px; display: block; }
        
        /* Tanda Tangan */
        .signature-section { margin-top: 40px; width: 100%; page-break-inside: avoid; }
        .sig-table { width: 100%; border: none; }
        .sig-table td { 
            text-align: left; 
            vertical-align: top; 
            width: 50%; 
            padding-right: 20px; 
        }
        .sig-table td:last-child {
            padding-left: 40px; 
        }
        .sig-space { height: 80px; }
    </style>
</head>
<body>

    @foreach($data as $modul)
    <div class="{{ !$loop->last ? 'page-break' : '' }}">
        
        <div class="text-center doc-title">
            <h3 class="uppercase text-bold">MODUL AJAR</h3>
            <h4>{{ $modul->subject->name }} | Kelas {{ $modul->grade_level }}</h4>
            <h4>SMK Negeri 10 Makassar</h4>

        </div>

        <div class="section-box">
            <div class="section-header">I. INFORMASI UMUM</div>
            <div class="content-padding">
                <table class="info-table">
                    <tr>
                        <td class="info-label">Nama Penyusun</td>
                        <td class="info-sep">:</td>
                        <td>{{ $modul->user->name ?? Auth::user()->name }}</td>
                    </tr>
                    <tr>
                        <td class="info-label">NIP</td>
                        <td class="info-sep">:</td>
                        <td>{{ $modul->user->nip ?? Auth::user()->nip }}</td>
                    </tr>
                    <tr>
                        <td class="info-label">Tahun / Semester</td>
                        <td class="info-sep">:</td>
                        <td>{{ $modul->created_at->format('Y') }} / {{ $modul->semester ?? '-' }}</td>
                    </tr>
                    <tr>
                        <td class="info-label">Topik / Materi</td>
                        <td class="info-sep">:</td>
                        <td>{{ $modul->topic }}</td>
                    </tr>
                    <tr>
                        <td class="info-label">Elemen</td>
                        <td class="info-sep">:</td>
                        <td>{{ $modul->elemen ?? '-' }}</td>
                    </tr>
                    <tr>
                        <td class="info-label">Alokasi Waktu</td>
                        <td class="info-sep">:</td>
                        <td>Pertemuan ke-{{ $modul->meeting_sequence }} ({{ $modul->allocation_jp }} JP x {{ $modul->duration_minutes }} Menit)</td>
                    </tr>
                    <tr>
                        <td class="info-label">Dimensi Profil Lulusan</td>
                        <td class="info-sep">:</td>
                        <td>
                            @if(is_array($modul->profil_pelajar_pancasila))
                                {{ implode(', ', $modul->profil_pelajar_pancasila) }}
                            @else
                                -
                            @endif
                        </td>
                    </tr>
                    <tr>
                        <td class="info-label">Sarana & Prasarana</td>
                        <td class="info-sep">:</td>
                        <td>
                            Lingkungan: {{ $modul->learning_environment ?? '-' }}<br>
                            Digital: {{ $modul->digital_usage ?? '-' }}
                        </td>
                    </tr>
                </table>
            </div>
        </div>

        <div class="section-box">
            <div class="section-header">II. KOMPONEN INTI</div>
            <div class="content-padding content-justify">
                
                <span class="sub-header" style="margin-top: 0;">A. Capaian Pembelajaran (CP)</span>
                <div>{{ $modul->cp_text }}</div>

                <span class="sub-header">B. Tujuan Pembelajaran (TP)</span>
                <div>{{ $modul->tp_text }}</div>

                <span class="sub-header">C. Metodologi Pembelajaran</span>
                <table class="info-table">
                    <tr>
                        <td width="130" class="text-bold">Model</td>
                        <td width="10">:</td>
                        <td>{{ $modul->model_learning }}</td>
                    </tr>
                    <tr>
                        <td class="text-bold">Pendekatan</td>
                        <td>:</td>
                        <td>{{ $modul->approach_learning ?? '-' }}</td>
                    </tr>
                    <tr>
                        <td class="text-bold">Metode</td>
                        <td>:</td>
                        <td>{{ $modul->method_learning }}</td>
                    </tr>
                    <tr>
                        <td class="text-bold">Strategi</td>
                        <td>:</td>
                        <td>{{ $modul->strategy_learning ?? '-' }}</td>
                    </tr>
                </table>
            </div>
        </div>

        <div class="section-box">
            <div class="section-header">III. LANGKAH-LANGKAH PEMBELAJARAN</div>
            <div class="content-padding">
                
                {{-- 1. PENDAHULUAN --}}
                <div style="margin-bottom: 15px;">
                    <div class="text-bold" style="background: #eee; padding: 2px 5px; display: inline-block; margin-bottom: 5px;">1. Kegiatan Pendahuluan</div>
                    <div class="content-justify" style="padding-left: 15px;">
                        {{-- PERBAIKAN: Hapus ** dan Rapikan --}}
                        {!! nl2br(e(str_replace('**', '', $modul->activity_initial))) !!}
                    </div>
                </div>

                {{-- 2. INTI --}}
                <div style="margin-bottom: 15px;">
                    <div class="text-bold" style="background: #eee; padding: 2px 5px; display: inline-block; margin-bottom: 5px;">2. Kegiatan Inti</div>
                    <table class="info-table" style="margin-left: 10px;">
                        {{-- a. Memahami --}}
                        <tr>
                            <td width="20" class="text-bold" style="vertical-align: top;">a.</td>
                            <td class="text-bold">Mengamati / Memahami:</td>
                        </tr>
                        <tr>
                            <td></td>
                            <td class="content-justify">
                                {!! nl2br(e(str_replace('**', '', $modul->activity_core_understand ?? '-'))) !!}
                            </td>
                        </tr>
                        
                        {{-- b. Mencoba --}}
                        <tr>
                            <td width="20" class="text-bold" style="vertical-align: top;">b.</td>
                            <td class="text-bold">Mencoba / Mengaplikasi:</td>
                        </tr>
                        <tr>
                            <td></td>
                            <td class="content-justify">
                                {!! nl2br(e(str_replace('**', '', $modul->activity_core_apply ?? '-'))) !!}
                            </td>
                        </tr>
                        
                        {{-- c. Merefleksi --}}
                        <tr>
                            <td width="20" class="text-bold" style="vertical-align: top;">c.</td>
                            <td class="text-bold">Merefleksi / Mengasosiasi:</td>
                        </tr>
                        <tr>
                            <td></td>
                            <td class="content-justify">
                                {!! nl2br(e(str_replace('**', '', $modul->activity_core_reflect ?? '-'))) !!}
                            </td>
                        </tr>
                    </table>
                </div>

                {{-- 3. PENUTUP --}}
                <div>
                    <div class="text-bold" style="background: #eee; padding: 2px 5px; display: inline-block; margin-bottom: 5px;">3. Kegiatan Penutup</div>
                    <div class="content-justify" style="padding-left: 15px;">
                        {!! nl2br(e(str_replace('**', '', $modul->activity_closing))) !!}
                    </div>
                </div>

            </div>
        </div>

        <div class="signature-section">
            <table class="sig-table">
                <tr>
                    <td>
                        Mengetahui,<br>
                        Kepala Sekolah
                        <div class="sig-space"></div>
                        <span class="text-bold" style="text-decoration: underline;">{{ $school->principal_name }}</span><br>
                        NIP. {{ $school->principal_nip }}
                    </td>
                    <td>
                        Makassar, {{ $tanggalCetak }}<br>
                        Guru Mata Pelajaran
                        <div class="sig-space"></div>
                        <span class="text-bold" style="text-decoration: underline;">{{ $modul->user->name ?? Auth::user()->name }}</span><br>
                        NIP. {{ $modul->user->nip ?? (Auth::user()->nip ?? '-') }}
                    </td>
                </tr>
            </table>
        </div>

    </div>
    @endforeach

    <script>
        window.onload = function() {
            window.print();
        }
    </script>
</body>
</html>