<?php $__env->startSection('title', 'Libro de Notas'); ?>
<?php $__env->startSection('page-title', 'Libro de Notas'); ?>

<?php $__env->startSection('content'); ?>


<div class="card" style="margin-bottom:24px;">
    <form method="GET" action="<?php echo e(route('notas.index')); ?>" style="display:flex;gap:14px;align-items:flex-end;flex-wrap:wrap;">
        <div class="form-group" style="margin:0;flex:1;min-width:160px;">
            <label class="form-label">Año Escolar</label>
            <select name="anio" class="form-control">
                <?php for($y = date('Y'); $y >= 2020; $y--): ?>
                    <option value="<?php echo e($y); ?>" <?php echo e($anio == $y ? 'selected' : ''); ?>><?php echo e($y); ?></option>
                <?php endfor; ?>
            </select>
        </div>
        <div class="form-group" style="margin:0;flex:2;min-width:180px;">
            <label class="form-label">Sección *</label>
            <select name="seccion_id" class="form-control" required>
                <option value="">— Seleccionar —</option>
                <?php $__currentLoopData = $secciones; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <option value="<?php echo e($s->id); ?>" <?php echo e($seccionId == $s->id ? 'selected' : ''); ?>>
                        <?php echo e($s->grado->nombre); ?> — Sec. <?php echo e($s->nombre); ?> (<?php echo e(ucfirst($s->turno)); ?>)
                    </option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>
        <div class="form-group" style="margin:0;flex:2;min-width:180px;">
            <label class="form-label">Materia *</label>
            <select name="materia_id" class="form-control" required>
                <option value="">— Seleccionar —</option>
                <?php $__currentLoopData = $materias; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $m): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <option value="<?php echo e($m->id); ?>" <?php echo e($materiaId == $m->id ? 'selected' : ''); ?>><?php echo e($m->nombre); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>
        <div style="flex-shrink:0;">
            <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> Ver Notas</button>
        </div>
    </form>
</div>

<?php if($seccion && $materia): ?>


<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;flex-wrap:wrap;gap:12px;">
    <div>
        <h3 style="font-size:16px;font-weight:700;">
            <?php echo e($materia->nombre); ?> — <?php echo e($seccion->grado->nombre); ?>, Sección <?php echo e($seccion->nombre); ?>

        </h3>
        <p style="font-size:12px;color:var(--muted);margin-top:2px;">Año escolar <?php echo e($anio); ?> · <?php echo e($libroNotas->count()); ?> alumno(s)</p>
    </div>
    <a href="<?php echo e(route('notas.index')); ?>?seccion_id=<?php echo e($seccionId); ?>&materia_id=<?php echo e($materiaId); ?>&anio=<?php echo e($anio); ?>&imprimir=1"
       class="btn btn-secondary btn-sm" onclick="window.print();return false;">
        <i class="fas fa-print"></i> Imprimir
    </a>
</div>


<div style="display:flex;gap:6px;margin-bottom:0;flex-wrap:wrap;" id="tabs-bimestre">
    <?php for($b = 1; $b <= 4; $b++): ?>
    <button onclick="cambiarBimestre(<?php echo e($b); ?>)" id="tab-<?php echo e($b); ?>"
        class="btn btn-sm <?php echo e($b == 1 ? 'btn-primary' : 'btn-secondary'); ?>"
        style="border-radius:10px 10px 0 0;">
        <?php echo e($b); ?>° Bimestre
    </button>
    <?php endfor; ?>
    <button onclick="cambiarBimestre(0)" id="tab-0"
        class="btn btn-sm btn-secondary" style="border-radius:10px 10px 0 0;">
        <i class="fas fa-table"></i> Resumen
    </button>
</div>


<?php for($b = 1; $b <= 4; $b++): ?>
<div id="panel-<?php echo e($b); ?>" class="panel-bimestre" style="<?php echo e($b > 1 ? 'display:none;' : ''); ?>">
    <div class="card" style="border-radius:0 12px 12px 12px;">
        <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
            <h4 style="font-size:14px;font-weight:700;color:var(--primary);">
                <i class="fas fa-edit" style="margin-right:6px;"></i>Ingreso de Notas — <?php echo e($b); ?>° Bimestre
            </h4>
        </div>
        <form method="POST" action="<?php echo e(route('notas.guardar')); ?>">
            <?php echo csrf_field(); ?>
            <input type="hidden" name="seccion_id"   value="<?php echo e($seccionId); ?>">
            <input type="hidden" name="materia_id"   value="<?php echo e($materiaId); ?>">
            <input type="hidden" name="anio_escolar" value="<?php echo e($anio); ?>">
            <input type="hidden" name="bimestre"     value="<?php echo e($b); ?>">

            <div class="table-wrapper">
                <table>
                    <thead>
                        <tr>
                            <th style="width:40px;">#</th>
                            <th>Alumno</th>
                            <th style="text-align:center;width:120px;">Nota (0–20)</th>
                            <th style="text-align:center;">Estado</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php $__currentLoopData = $libroNotas; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php $notaObj = $row['bimestres'][$b]; ?>
                        <tr>
                            <td style="color:var(--muted);font-size:12px;"><?php echo e($i + 1); ?></td>
                            <td>
                                <div style="font-weight:600;"><?php echo e($row['alumno']->nombre_completo); ?></div>
                                <div style="font-size:11px;color:var(--muted);"><?php echo e($row['alumno']->codigo); ?></div>
                            </td>
                            <td>
                                <input type="number"
                                    name="notas[<?php echo e($row['alumno']->id); ?>]"
                                    value="<?php echo e($notaObj?->nota ?? ''); ?>"
                                    min="0" max="20" step="0.5"
                                    class="form-control nota-input"
                                    style="text-align:center;padding:8px;"
                                    placeholder="—"
                                    oninput="actualizarEstado(this)">
                            </td>
                            <td style="text-align:center;">
                                <?php if($notaObj): ?>
                                    <span class="badge <?php echo e($notaObj->nota >= 11 ? 'badge-success' : 'badge-danger'); ?>">
                                        <?php echo e($notaObj->nota >= 11 ? 'Aprobado' : 'Desaprobado'); ?>

                                    </span>
                                <?php else: ?>
                                    <span class="badge badge-secondary">Pendiente</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table>
            </div>

            <div style="display:flex;justify-content:flex-end;margin-top:16px;gap:10px;">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save"></i> Guardar <?php echo e($b); ?>° Bimestre
                </button>
            </div>
        </form>
    </div>
</div>
<?php endfor; ?>


<div id="panel-0" style="display:none;">
    <div class="card" style="border-radius:0 12px 12px 12px;">
        <div style="margin-bottom:16px;">
            <h4 style="font-size:14px;font-weight:700;color:var(--primary);">
                <i class="fas fa-table" style="margin-right:6px;"></i>Resumen General de Notas
            </h4>
        </div>
        <div class="table-wrapper">
            <table>
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Alumno</th>
                        <th style="text-align:center;">B1</th>
                        <th style="text-align:center;">B2</th>
                        <th style="text-align:center;">B3</th>
                        <th style="text-align:center;">B4</th>
                        <th style="text-align:center;">Promedio</th>
                        <th style="text-align:center;">Estado</th>
                        <th style="text-align:center;">Boleta</th>
                    </tr>
                </thead>
                <tbody>
                <?php $__currentLoopData = $libroNotas; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr>
                        <td style="color:var(--muted);font-size:12px;"><?php echo e($i + 1); ?></td>
                        <td>
                            <div style="font-weight:600;"><?php echo e($row['alumno']->nombre_completo); ?></div>
                            <div style="font-size:11px;color:var(--muted);"><?php echo e($row['alumno']->codigo); ?></div>
                        </td>
                        <?php for($b = 1; $b <= 4; $b++): ?>
                            <?php $n = $row['bimestres'][$b]; ?>
                            <td style="text-align:center;">
                                <?php if($n): ?>
                                    <span style="font-weight:700;color:<?php echo e($n->nota >= 11 ? 'var(--success)' : 'var(--danger)'); ?>;">
                                        <?php echo e(number_format($n->nota, 1)); ?>

                                    </span>
                                <?php else: ?>
                                    <span style="color:var(--muted);">—</span>
                                <?php endif; ?>
                            </td>
                        <?php endfor; ?>
                        <td style="text-align:center;font-weight:800;font-size:15px;
                            color:<?php echo e($row['promedio'] !== null ? ($row['promedio'] >= 11 ? 'var(--success)' : 'var(--danger)') : 'var(--muted)'); ?>;">
                            <?php echo e($row['promedio'] !== null ? number_format($row['promedio'], 2) : '—'); ?>

                        </td>
                        <td style="text-align:center;">
                            <?php if($row['estado'] === 'aprobado'): ?>
                                <span class="badge badge-success">Aprobado</span>
                            <?php elseif($row['estado'] === 'desaprobado'): ?>
                                <span class="badge badge-danger">Desaprobado</span>
                            <?php else: ?>
                                <span class="badge badge-secondary">Pendiente</span>
                            <?php endif; ?>
                        </td>
                        <td style="text-align:center;">
                            <a href="<?php echo e(route('notas.boleta', $row['alumno']->id)); ?>?anio=<?php echo e($anio); ?>"
                               class="btn btn-sm btn-secondary btn-icon" title="Ver boleta">
                                <i class="fas fa-file-alt"></i>
                            </a>
                        </td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        </div>

        
        <?php
            $aprobados   = $libroNotas->where('estado','aprobado')->count();
            $desaprobados= $libroNotas->where('estado','desaprobado')->count();
            $pendientes  = $libroNotas->where('estado','pendiente')->count();
            $total       = $libroNotas->count();
        ?>
        <?php if($total > 0): ?>
        <div style="display:flex;gap:16px;margin-top:20px;flex-wrap:wrap;">
            <div style="flex:1;min-width:120px;text-align:center;padding:14px;background:#d1fae5;border-radius:12px;">
                <div style="font-size:24px;font-weight:800;color:#065f46;"><?php echo e($aprobados); ?></div>
                <div style="font-size:12px;color:#065f46;">Aprobados</div>
            </div>
            <div style="flex:1;min-width:120px;text-align:center;padding:14px;background:#fee2e2;border-radius:12px;">
                <div style="font-size:24px;font-weight:800;color:#7f1d1d;"><?php echo e($desaprobados); ?></div>
                <div style="font-size:12px;color:#7f1d1d;">Desaprobados</div>
            </div>
            <div style="flex:1;min-width:120px;text-align:center;padding:14px;background:#f1f5f9;border-radius:12px;">
                <div style="font-size:24px;font-weight:800;color:var(--muted);"><?php echo e($pendientes); ?></div>
                <div style="font-size:12px;color:var(--muted);">Pendientes</div>
            </div>
            <div style="flex:1;min-width:120px;text-align:center;padding:14px;background:#e0e7ff;border-radius:12px;">
                <div style="font-size:24px;font-weight:800;color:#3730a3;">
                    <?php echo e($total > 0 ? round(($aprobados/$total)*100) : 0); ?>%
                </div>
                <div style="font-size:12px;color:#3730a3;">Aprobación</div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php else: ?>
<div class="card" style="padding:48px;text-align:center;color:var(--muted);">
    <i class="fas fa-clipboard-list" style="font-size:48px;opacity:.2;display:block;margin-bottom:16px;"></i>
    <p style="font-size:15px;font-weight:600;">Selecciona una sección y materia para ver el libro de notas.</p>
</div>
<?php endif; ?>

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
function cambiarBimestre(b) {
    // Ocultar todos los panels
    document.querySelectorAll('.panel-bimestre').forEach(p => p.style.display = 'none');
    document.getElementById('panel-0').style.display = 'none';

    // Quitar active de tabs
    for(let i = 0; i <= 4; i++) {
        const t = document.getElementById('tab-' + i);
        if(t) { t.classList.remove('btn-primary'); t.classList.add('btn-secondary'); }
    }

    // Mostrar panel activo
    const panel = document.getElementById('panel-' + b);
    if(panel) panel.style.display = 'block';
    const tab = document.getElementById('tab-' + b);
    if(tab) { tab.classList.remove('btn-secondary'); tab.classList.add('btn-primary'); }
}

function actualizarEstado(input) {
    const val = parseFloat(input.value);
    const row = input.closest('tr');
    const estadoCell = row.querySelector('td:last-child span') || row.cells[3].querySelector('span');
    if(!estadoCell) return;
    if(isNaN(val) || input.value === '') {
        estadoCell.className = 'badge badge-secondary';
        estadoCell.textContent = 'Pendiente';
    } else if(val >= 11) {
        estadoCell.className = 'badge badge-success';
        estadoCell.textContent = 'Aprobado';
    } else {
        estadoCell.className = 'badge badge-danger';
        estadoCell.textContent = 'Desaprobado';
    }
}
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\CRMyERP\crm-colegio\resources\views/notas/index.blade.php ENDPATH**/ ?>