<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kartu Peserta PSAJ - {{ $school->nama_sekolah }}</title>
    <style>
        @page {
            size: A4;
            margin: 10mm;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            font-size: 9pt;
            line-height: 1.3;
        }

        .page {
            width: 210mm;
            min-height: 297mm;
            padding: 5mm;
            margin: 0 auto;
            background: white;
            page-break-after: always;
        }

        .page:last-child {
            page-break-after: auto;
        }

        .cards-container {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            grid-template-rows: repeat(2, 1fr);
            gap: 8mm;
            height: 100%;
        }

        .card {
            border: 2px solid #000;
            padding: 8mm;
            display: flex;
            flex-direction: column;
            page-break-inside: avoid;
            position: relative;
            background: white;
        }

        .card-header {
            text-align: center;
            border-bottom: 2px solid #000;
            padding-bottom: 3mm;
            margin-bottom: 3mm;
        }

        .card-header h2 {
            font-size: 14pt;
            font-weight: bold;
            margin-bottom: 2mm;
            color: #000;
        }

        .card-header h3 {
            font-size: 11pt;
            font-weight: bold;
            margin-bottom: 1mm;
        }

        .card-header p {
            font-size: 8pt;
            margin: 1mm 0;
        }

        .card-body {
            flex: 1;
            display: grid;
            grid-template-columns: 1fr 25mm;
            gap: 3mm;
        }

        .data-section {
            display: flex;
            flex-direction: column;
            gap: 2mm;
        }

        .data-row {
            display: grid;
            grid-template-columns: 35mm 1fr;
            gap: 2mm;
            font-size: 9pt;
            line-height: 1.4;
        }

        .data-label {
            font-weight: bold;
        }

        .data-value {
            border-bottom: 1px dotted #666;
            padding-bottom: 1mm;
        }

        .photo-section {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 2mm;
        }

        .photo-box {
            width: 25mm;
            height: 30mm;
            border: 2px solid #000;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            background: #f5f5f5;
        }

        .photo-box img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .photo-placeholder {
            font-size: 7pt;
            text-align: center;
            color: #999;
            padding: 2mm;
        }

        .no-peserta {
            background: #000;
            color: white;
            padding: 2mm 4mm;
            text-align: center;
            font-weight: bold;
            font-size: 11pt;
            margin-top: 2mm;
            letter-spacing: 0.5mm;
        }

        .signature-section {
            margin-top: 4mm;
            padding-top: 3mm;
            border-top: 1px solid #ccc;
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 4mm;
            font-size: 8pt;
        }

        .signature-box {
            text-align: center;
        }

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

        @media print {
            body {
                margin: 0;
                padding: 0;
            }

            .page {
                margin: 0;
                border: none;
                width: 210mm;
                height: 297mm;
            }

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

        .print-button {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 12px 24px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            z-index: 1000;
        }

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

        .badge {
            display: inline-block;
            padding: 1mm 2mm;
            background: #007bff;
            color: white;
            border-radius: 2mm;
            font-size: 7pt;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <button class="print-button no-print" onclick="window.print()">
        <i class="bi bi-printer"></i> Cetak Kartu
    </button>

    @php
        $chunks = $participants->chunk(4);
    @endphp

    @foreach ($chunks as $pageIndex => $pageParticipants)
    <div class="page">
        <div class="cards-container">
            @foreach ($pageParticipants as $participant)
            <div class="card">
                <div class="card-header">
                    <h2>KARTU PESERTA PSAJ</h2>
                    <h3>{{ $participant->jenjang }}</h3>
                    <p>{{ $school->nama_sekolah }}</p>
                    <p style="font-size: 7pt;">NPSN: {{ $school->npsn }}</p>
                </div>

                <div class="card-body">
                    <div class="data-section">
                        <div class="data-row">
                            <div class="data-label">Nama</div>
                            <div class="data-value">{{ strtoupper($participant->nama) }}</div>
                        </div>
                        <div class="data-row">
                            <div class="data-label">NIK</div>
                            <div class="data-value">{{ $participant->nik }}</div>
                        </div>
                        <div class="data-row">
                            <div class="data-label">NISN</div>
                            <div class="data-value">{{ $participant->nisn }}</div>
                        </div>
                        <div class="data-row">
                            <div class="data-label">Tempat, Tgl Lahir</div>
                            <div class="data-value">
                                {{ $participant->tempat_lahir }}, {{ $participant->tanggal_lahir->format('d-m-Y') }}
                            </div>
                        </div>
                        <div class="data-row">
                            <div class="data-label">Kelas</div>
                            <div class="data-value">{{ $participant->kelas }}</div>
                        </div>
                        <div class="data-row">
                            <div class="data-label">Ketunaan</div>
                            <div class="data-value">
                                <span class="badge">{{ $participant->disabilityType->code }}</span>
                                {{ $participant->disabilityType->name }}
                            </div>
                        </div>
                        <div class="data-row">
                            <div class="data-label">Nama Ibu</div>
                            <div class="data-value">{{ $participant->nama_ibu }}</div>
                        </div>
                    </div>

                    <div class="photo-section">
                        <div class="photo-box">
                            @if ($participant->photo)
                                <img src="{{ public_path('storage/' . $participant->photo) }}" alt="Foto">
                            @else
                                <div class="photo-placeholder">PAS FOTO<br>3x4</div>
                            @endif
                        </div>
                    </div>
                </div>

                <div class="no-peserta">
                    {{ $participant->no_peserta }}
                </div>

                <div class="signature-section">
                    <div class="signature-box">
                        <div>Peserta</div>
                        <div class="signature-line">
                            {{ $participant->nama }}
                        </div>
                    </div>
                    <div class="signature-box">
                        <div>Kepala Sekolah</div>
                        <div class="signature-line">
                            {{ $school->kepala_sekolah ?? '.....................' }}
                        </div>
                    </div>
                </div>
            </div>
            @endforeach

            @php
                $remaining = 4 - $pageParticipants->count();
            @endphp

            @for ($i = 0; $i < $remaining; $i++)
            <div class="card" style="border: 2px dashed #ccc; background: #fafafa;">
                <div style="display: flex; align-items: center; justify-content: center; height: 100%; color: #ccc; font-size: 12pt;">
                    Kosong
                </div>
            </div>
            @endfor
        </div>
    </div>
    @endforeach

    <script>
        // Auto print when page loads (optional)
        // window.onload = function() { window.print(); }
    </script>
</body>
</html>
    <style>
        @page {
            size: A4;
            margin: 10mm;
        }
        
        @media print {
            body {
                margin: 0;
                padding: 0;
            }
            .no-print {
                display: none !important;
            }
            .page-break {
                page-break-after: always;
            }
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            background: #f5f5f5;
            padding: 10px;
        }
        
        .print-controls {
            text-align: center;
            margin-bottom: 20px;
            padding: 15px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .btn {
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 0 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-print {
            background: #007bff;
            color: white;
        }
        
        .btn-print:hover {
            background: #0056b3;
        }
        
        .btn-back {
            background: #6c757d;
            color: white;
        }
        
        .btn-back:hover {
            background: #5a6268;
        }
        
        .a4-container {
            width: 210mm;
            min-height: 297mm;
            background: white;
            margin: 0 auto;
            padding: 10mm;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        
        .cards-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            grid-template-rows: repeat(3, 1fr);
            gap: 8mm;
            height: 277mm;
        }
        
        .kartu-wrapper {
            border: 2px solid #2c3e50;
            border-radius: 8px;
            padding: 8px;
            background: white;
            position: relative;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .kartu-wrapper::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, #007bff 0%, #28a745 50%, #ffc107 100%);
        }
        
        .kartu-header {
            text-align: center;
            border-bottom: 2px solid #007bff;
            padding-bottom: 6px;
            margin-bottom: 8px;
        }
        
        .kartu-header h3 {
            font-size: 14px;
            color: #2c3e50;
            margin: 3px 0;
            font-weight: bold;
        }
        
        .kartu-header h4 {
            font-size: 11px;
            color: #007bff;
            margin: 2px 0;
            font-weight: bold;
        }
        
        .school-info {
            font-size: 9px;
            color: #666;
            margin-top: 3px;
        }
        
        .kartu-body {
            display: flex;
            gap: 8px;
            flex: 1;
        }
        
        .foto-section {
            flex-shrink: 0;
            width: 70px;
            text-align: center;
        }
        
        .foto-box {
            width: 70px;
            height: 85px;
            border: 2px solid #007bff;
            border-radius: 5px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #f8f9fa;
            margin-bottom: 4px;
            overflow: hidden;
        }
        
        .foto-box img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        
        .foto-box .placeholder {
            font-size: 35px;
            color: #007bff;
        }
        
        .no-peserta {
            background: #007bff;
            color: white;
            padding: 3px 5px;
            border-radius: 3px;
            font-weight: bold;
            font-size: 8px;
            word-break: break-all;
            line-height: 1.2;
        }
        
        .data-section {
            flex: 1;
            font-size: 9px;
        }
        
        .data-item {
            display: grid;
            grid-template-columns: 65px 5px 1fr;
            margin-bottom: 3px;
            line-height: 1.3;
        }
        
        .data-label {
            font-weight: bold;
            color: #2c3e50;
        }
        
        .data-value {
            color: #555;
        }
        
        .jenjang-badge {
            position: absolute;
            top: 8px;
            right: 8px;
            background: #28a745;
            color: white;
            padding: 3px 8px;
            border-radius: 10px;
            font-weight: bold;
            font-size: 9px;
        }
        
        .qr-section {
            text-align: center;
            margin-top: 5px;
            padding-top: 5px;
            border-top: 1px solid #e0e0e0;
        }
        
        .qr-box {
            width: 40px;
            height: 40px;
            border: 1px solid #ccc;
            margin: 0 auto 3px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 7px;
            color: #999;
        }
        
        .footer-text {
            font-size: 7px;
            color: #666;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="print-controls no-print">
        <h3 style="margin-bottom: 15px; color: #2c3e50;">
            <i>📄</i> Cetak Kartu Peserta PSAJ (6 Kartu per Halaman)
        </h3>
        <button class="btn btn-print" onclick="window.print()">
            🖨️ Cetak Kartu
        </button>
        <a href="javascript:history.back()" class="btn btn-back">
            ← Kembali
        </a>
    </div>
    
    <div class="a4-container">
        <div class="cards-grid">
            @foreach($participants as $index => $participant)
            <div class="kartu-wrapper">
                <div class="jenjang-badge">{{ strtoupper($jenjang) }}</div>
                
                <div class="kartu-header">
                    <h3>KARTU PESERTA PSAJ</h3>
                    <h4>Program Satuan Pendidikan Anak Jalanan</h4>
                    @if($school)
                    <div class="school-info">{{ $school->name }} - NPSN: {{ $school->npsn }}</div>
                    @endif
                </div>
                
                <div class="kartu-body">
                    <div class="foto-section">
                        <div class="foto-box">
                            @if(isset($participant['foto']) && $participant['foto'])
                                <img src="{{ asset('storage/' . $participant['foto']) }}" alt="Foto">
                            @else
                                <div class="placeholder">👤</div>
                            @endif
                        </div>
                        <div class="no-peserta">{{ $participant['no_peserta'] }}</div>
                    </div>
                    
                    <div class="data-section">
                        <div class="data-item">
                            <div class="data-label">NISN</div>
                            <div>:</div>
                            <div class="data-value">{{ $participant['nisn'] }}</div>
                        </div>
                        <div class="data-item">
                            <div class="data-label">Nama</div>
                            <div>:</div>
                            <div class="data-value">{{ $participant['nama_lengkap'] }}</div>
                        </div>
                        <div class="data-item">
                            <div class="data-label">TTL</div>
                            <div>:</div>
                            <div class="data-value">{{ $participant['tempat_lahir'] }}, {{ \Carbon\Carbon::parse($participant['tanggal_lahir'])->format('d/m/Y') }}</div>
                        </div>
                        <div class="data-item">
                            <div class="data-label">Kelas</div>
                            <div>:</div>
                            <div class="data-value">{{ $participant['kelas'] }}</div>
                        </div>
                        <div class="data-item">
                            <div class="data-label">Program</div>
                            <div>:</div>
                            <div class="data-value">{{ $participant['program'] }}</div>
                        </div>
                    </div>
                </div>
                
                <div class="qr-section">
                    <div class="qr-box">QR</div>
                    <div class="footer-text">Kartu resmi kepesertaan PSAJ</div>
                </div>
            </div>
            @endforeach
            
            @for($i = count($participants); $i < 6; $i++)
            <div class="kartu-wrapper" style="border-style: dashed; opacity: 0.3;">
                <div class="kartu-header">
                    <h3>KARTU PESERTA PSAJ</h3>
                    <h4>Slot Kosong</h4>
                </div>
                <div style="text-align: center; padding: 30px; color: #ccc;">
                    <div style="font-size: 40px;">📋</div>
                    <div style="margin-top: 10px; font-size: 10px;">Slot {{ $i + 1 }}</div>
                </div>
            </div>
            @endfor
        </div>
    </div>
    
    <script>
        // Optional: Auto print when page loads
        // window.onload = function() {
        //     setTimeout(() => window.print(), 500);
        // };
    </script>
</body>
</html>
