

<?php $__env->startSection('title', 'PSAJ - Penilaian Sumatif Akhir Jenjang'); ?>

<?php $__env->startSection('content'); ?>
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-2">PSAJ - Penilaian Sumatif Akhir Jenjang</h1>
            <p class="text-muted mb-0"><?php echo e($school->name); ?></p>
        </div>
        <div class="d-flex gap-2">
            <button type="button" class="btn btn-success" onclick="generateKartuBatch()" title="Cetak 4 kartu per halaman A4">
                <i class="bi bi-printer me-2"></i>Generate Kartu Peserta (Batch)
            </button>
            <button type="button" class="btn btn-warning" onclick="showSubmitModal()" title="Finalisasi dan kunci data peserta">
                <i class="bi bi-send me-2"></i>Kirim Data Final
            </button>
            <a href="<?php echo e(route('admin-sekolah.psaj.create')); ?>" class="btn btn-primary">
                <i class="bi bi-plus-circle me-2"></i>Tambah Peserta
            </a>
        </div>
    </div>

    <?php if(session('success')): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <?php echo e(session('success')); ?>

            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <?php if(session('error')): ?>
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <?php echo e(session('error')); ?>

            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <!-- Filter Jenjang -->
    <div class="card mb-3">
        <div class="card-body">
            <div class="d-flex align-items-center gap-3">
                <label class="mb-0 fw-bold">Filter Jenjang:</label>
                <div class="btn-group" role="group">
                    <a href="<?php echo e(route('admin-sekolah.psaj.index')); ?>" 
                       class="btn btn-sm <?php echo e(!$jenjang ? 'btn-primary' : 'btn-outline-primary'); ?>">
                        Semua
                    </a>
                    <?php $__currentLoopData = $jenjangOptions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <a href="<?php echo e(route('admin-sekolah.psaj.index', ['jenjang' => strtolower($key)])); ?>" 
                           class="btn btn-sm <?php echo e($jenjang === $key ? 'btn-primary' : 'btn-outline-primary'); ?>">
                            <?php echo e($key); ?>

                        </a>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="text-white-50 small">Total Peserta</div>
                            <h4 class="mb-0"><?php echo e($participants->count()); ?></h4>
                        </div>
                        <i class="bi bi-people fs-1 opacity-50"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-success text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="text-white-50 small">Laki-laki</div>
                            <h4 class="mb-0"><?php echo e($participants->filter(fn($p) => str_starts_with($p->nik, '1') || str_starts_with($p->nik, '3') || str_starts_with($p->nik, '5') || str_starts_with($p->nik, '7') || str_starts_with($p->nik, '9'))->count()); ?></h4>
                        </div>
                        <i class="bi bi-gender-male fs-1 opacity-50"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-info text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="text-white-50 small">Perempuan</div>
                            <h4 class="mb-0"><?php echo e($participants->filter(fn($p) => str_starts_with($p->nik, '2') || str_starts_with($p->nik, '4') || str_starts_with($p->nik, '6') || str_starts_with($p->nik, '8') || str_starts_with($p->nik, '0'))->count()); ?></h4>
                        </div>
                        <i class="bi bi-gender-female fs-1 opacity-50"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-warning text-dark">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="text-dark-50 small">Dengan Foto</div>
                            <h4 class="mb-0"><?php echo e($participants->whereNotNull('photo')->count()); ?></h4>
                        </div>
                        <i class="bi bi-card-image fs-1 opacity-50"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Table -->
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th width="3%">
                                <input type="checkbox" id="selectAll" class="form-check-input" onchange="toggleSelectAll(this)">
                            </th>
                            <th width="5%">No</th>
                            <th width="15%">No. Peserta</th>
                            <th>Nama</th>
                            <th width="10%">NISN</th>
                            <th width="8%">Jenjang</th>
                            <th width="8%">Kelas</th>
                            <th width="10%">Ketunaan</th>
                            <th width="8%">Status</th>
                            <th width="8%">Foto</th>
                            <th width="15%" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $participants; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $participant): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                            <tr class="<?php echo e($participant->is_locked ? 'table-secondary' : ''); ?>">
                                <td>
                                    <?php if(!$participant->is_locked): ?>
                                        <input type="checkbox" name="participant_ids[]" value="<?php echo e($participant->id); ?>" class="form-check-input participant-checkbox">
                                    <?php endif; ?>
                                </td>
                                <td><?php echo e($index + 1); ?></td>
                                <td>
                                    <small class="text-muted fw-bold"><?php echo e($participant->no_peserta); ?></small>
                                </td>
                                <td>
                                    <strong><?php echo e($participant->nama); ?></strong><br>
                                    <small class="text-muted">NIK: <?php echo e($participant->nik); ?></small>
                                </td>
                                <td><?php echo e($participant->nisn); ?></td>
                                <td>
                                    <span class="badge bg-primary"><?php echo e($participant->jenjang); ?></span>
                                </td>
                                <td class="text-center"><?php echo e($participant->kelas); ?></td>
                                <td>
                                    <span class="badge bg-info"><?php echo e($participant->disabilityType->code); ?></span>
                                </td>
                                <td>
                                    <?php if($participant->is_locked): ?>
                                        <span class="badge bg-success" title="Data final, terkunci pada <?php echo e($participant->submitted_at?->format('d/m/Y H:i')); ?>">
                                            <i class="bi bi-lock-fill me-1"></i>Final
                                        </span>
                                    <?php else: ?>
                                        <span class="badge bg-warning">
                                            <i class="bi bi-pencil me-1"></i>Draft
                                        </span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-center">
                                    <?php if($participant->photo): ?>
                                        <img src="<?php echo e(asset('storage/' . $participant->photo)); ?>" 
                                             alt="Foto" 
                                             class="img-thumbnail" 
                                             style="width: 40px; height: 50px; object-fit: cover;">
                                    <?php else: ?>
                                        <span class="badge bg-secondary">Tidak Ada</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <div class="btn-group btn-group-sm" role="group">
                                        <a href="<?php echo e(route('admin-sekolah.psaj.show', $participant->id)); ?>" 
                                           class="btn btn-info" 
                                           title="Lihat Detail">
                                            <i class="bi bi-eye"></i>
                                        </a>
                                        <?php if(!$participant->is_locked): ?>
                                            <a href="<?php echo e(route('admin-sekolah.psaj.edit', $participant->id)); ?>" 
                                               class="btn btn-warning" 
                                               title="Edit">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                        <?php endif; ?>
                                        <a href="<?php echo e(route('admin-sekolah.psaj.kartu', $participant->id)); ?>" 
                                           class="btn btn-success" 
                                           title="Cetak Kartu">
                                            <i class="bi bi-card-heading"></i>
                                        </a>
                                        <?php if(!$participant->is_locked): ?>
                                            <button type="button" 
                                                    class="btn btn-danger" 
                                                    title="Hapus"
                                                    onclick="confirmDelete(<?php echo e($participant->id); ?>)">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                            <tr>
                                <td colspan="11" class="text-center py-5 text-muted">
                                    <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                                    Belum ada peserta PSAJ <?php echo e($jenjang ? 'untuk jenjang ' . $jenjang : ''); ?>

                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Konfirmasi Hapus</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                Apakah Anda yakin ingin menghapus peserta ini?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                <form id="deleteForm" method="POST" style="display: inline;">
                    <?php echo csrf_field(); ?>
                    <?php echo method_field('DELETE'); ?>
                    <button type="submit" class="btn btn-danger">Hapus</button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- Submit Final Modal -->
<div class="modal fade" id="submitModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-warning">
                <h5 class="modal-title">
                    <i class="bi bi-exclamation-triangle me-2"></i>Konfirmasi Kirim Data Final
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="submitForm" method="POST" action="<?php echo e(route('admin-sekolah.psaj.submit')); ?>">
                <?php echo csrf_field(); ?>
                <div class="modal-body">
                    <div class="alert alert-warning">
                        <i class="bi bi-info-circle me-2"></i>
                        <strong>Perhatian:</strong> Setelah data dikirim sebagai final, data akan <strong>TERKUNCI</strong> dan tidak dapat diubah atau dihapus lagi.
                    </div>
                    <p>Anda akan mengirimkan <strong id="selectedCount">0</strong> peserta sebagai data final.</p>
                    <p class="mb-0">Apakah Anda yakin?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                    <button type="submit" class="btn btn-warning">
                        <i class="bi bi-send me-2"></i>Ya, Kirim Data Final
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $__env->startPush('scripts'); ?>
<script>
    function confirmDelete(id) {
        const modal = new bootstrap.Modal(document.getElementById('deleteModal'));
        const form = document.getElementById('deleteForm');
        form.action = '<?php echo e(route("admin-sekolah.psaj.destroy", ":id")); ?>'.replace(':id', id);
        modal.show();
    }
    
    function toggleSelectAll(checkbox) {
        const checkboxes = document.querySelectorAll('.participant-checkbox');
        checkboxes.forEach(cb => cb.checked = checkbox.checked);
    }
    
    function showSubmitModal() {
        const checkboxes = document.querySelectorAll('.participant-checkbox:checked');
        
        if (checkboxes.length === 0) {
            alert('Silakan pilih minimal 1 peserta untuk dikirim sebagai data final');
            return;
        }
        
        // Update count
        document.getElementById('selectedCount').textContent = checkboxes.length;
        
        // Clear existing hidden inputs
        const form = document.getElementById('submitForm');
        const existingInputs = form.querySelectorAll('input[name="participant_ids[]"]');
        existingInputs.forEach(input => input.remove());
        
        // Add selected participant IDs to form
        checkboxes.forEach(checkbox => {
            const input = document.createElement('input');
            input.type = 'hidden';
            input.name = 'participant_ids[]';
            input.value = checkbox.value;
            form.appendChild(input);
        });
        
        // Show modal
        const modal = new bootstrap.Modal(document.getElementById('submitModal'));
        modal.show();
    }
    
    function generateKartuBatch() {
        const jenjang = '<?php echo e($jenjang ?? ""); ?>';
        let url = '<?php echo e(route("admin-sekolah.psaj.kartu-batch")); ?>';
        
        if (jenjang) {
            url += '?jenjang=' + jenjang;
        }
        
        window.open(url, '_blank');
    }
</script>
<?php $__env->stopPush(); ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\appsuper\resources\views/admin-sekolah/psaj/index.blade.php ENDPATH**/ ?>