<?php $__env->startSection('title', 'Nueva Matrícula'); ?>
<?php $__env->startSection('page-title', 'Registrar Matrícula'); ?>

<?php $__env->startSection('content'); ?>
<div style="max-width:760px;">
<div style="margin-bottom:16px;">
    <a href="<?php echo e(route('matriculas.index')); ?>" class="btn btn-secondary"><i class="fas fa-arrow-left"></i> Volver</a>
</div>

<form method="POST" action="<?php echo e(route('matriculas.store')); ?>">
<?php echo csrf_field(); ?>
<div class="card">
    <div class="card-header">
        <span class="card-title"><i class="fas fa-file-signature" style="color:#3b82f6;margin-right:8px;"></i>Datos de la Matrícula</span>
    </div>
    <div class="card-body">
        <div class="form-group">
            <label class="form-label">Alumno *</label>
            <select name="alumno_id" class="form-control" required>
                <option value="">Seleccionar alumno...</option>
                <?php $__currentLoopData = $alumnos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $a): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <option value="<?php echo e($a->id); ?>" <?php echo e(old('alumno_id')==$a->id ? 'selected':''); ?>>
                        <?php echo e($a->nombre_completo); ?> — DNI: <?php echo e($a->dni); ?>

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

        <div class="grid grid-2">
            <div class="form-group">
                <label class="form-label">Grado *</label>
                <select name="grado_id" id="grado_select" class="form-control" required>
                    <option value="">Seleccionar grado...</option>
                    <?php $__currentLoopData = $grados; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $g): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($g->id); ?>" <?php echo e(old('grado_id')==$g->id ? 'selected':''); ?>><?php echo e($g->nombre); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">Sección *</label>
                <select name="seccion_id" id="seccion_select" class="form-control" required>
                    <option value="">Primero selecciona un grado</option>
                    <?php $__currentLoopData = $secciones; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($s->id); ?>" data-grado="<?php echo e($s->grado_id); ?>" <?php echo e(old('seccion_id')==$s->id ? 'selected':''); ?>>
                            Sección <?php echo e($s->nombre); ?> (<?php echo e(ucfirst($s->turno)); ?>)
                        </option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
        </div>

        <div class="grid grid-2">
            <div class="form-group">
                <label class="form-label">Año Escolar *</label>
                <select name="anio_escolar" class="form-control" required>
                    <?php for($y = date('Y'); $y >= 2020; $y--): ?>
                        <option value="<?php echo e($y); ?>" <?php echo e(old('anio_escolar', date('Y'))==$y ? 'selected':''); ?>><?php echo e($y); ?></option>
                    <?php endfor; ?>
                </select>
            </div>
            <div class="form-group">
                <label class="form-label">Fecha de Matrícula *</label>
                <input type="date" name="fecha_matricula" class="form-control"
                    value="<?php echo e(old('fecha_matricula', date('Y-m-d'))); ?>" required>
            </div>
        </div>

        <div class="form-group">
            <label class="form-label">Observaciones</label>
            <textarea name="observaciones" class="form-control" rows="3"><?php echo e(old('observaciones')); ?></textarea>
        </div>
    </div>
</div>

<div style="display:flex;gap:12px;justify-content:flex-end;margin-top:16px;">
    <a href="<?php echo e(route('matriculas.index')); ?>" class="btn btn-secondary">Cancelar</a>
    <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Guardar Matrícula</button>
</div>
</form>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
// Filtrar secciones según el grado seleccionado
const gradoSelect   = document.getElementById('grado_select');
const seccionSelect = document.getElementById('seccion_select');
const allOpts       = Array.from(seccionSelect.options);

gradoSelect.addEventListener('change', function() {
    const gId = this.value;
    seccionSelect.innerHTML = '';
    const placeholder = document.createElement('option');
    placeholder.text  = 'Seleccionar sección...';
    placeholder.value = '';
    seccionSelect.add(placeholder);

    allOpts.filter(o => o.dataset.grado == gId || !o.value).forEach(o => {
        if (o.dataset.grado == gId) seccionSelect.add(o.cloneNode(true));
    });
});

// Disparar cambio si hay valor previo
if (gradoSelect.value) gradoSelect.dispatchEvent(new Event('change'));
</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/enrollments/create.blade.php ENDPATH**/ ?>