<c-layouts.app title="Alumnos & Matriculación | Studio Lo Justo">
    <div class="bg-zinc-100 min-h-screen pb-20">
        <!-- BARRA SUPERIOR DEL DASHBOARD SAAS (SIN BANNERS DE MARKETING) -->
        <div class="bg-white border-b border-zinc-200/80 px-4 sm:px-6 lg:px-8 py-5 shadow-sm">
            <div class="max-w-7xl mx-auto flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                <div>
                    <div class="flex items-center gap-2 text-xs font-bold uppercase tracking-wider text-zinc-400 mb-1">
                        <span>Gestión Académica</span>
                        <span>/</span>
                        <span class="text-emerald-700 font-black">Directorio de Alumnos</span>
                    </div>
                    <h1 class="text-2xl font-black text-zinc-900 flex items-center gap-2.5">
                        <c-lbi.rmx.group w="1.5rem" h="1.5rem" class="text-emerald-600" />
                        <span>Alumnos & Matriculación</span>
                    </h1>
                </div>

                <div class="flex items-center gap-2.5">
                    <button onclick="document.getElementById('modal_enroll_student').showModal()" class="px-4 py-2.5 bg-emerald-600 hover:bg-emerald-700 text-white rounded-xl font-bold text-xs uppercase tracking-wider shadow-sm hover:shadow transition-all flex items-center gap-1.5">
                        <span class="text-base leading-none font-black">+</span>
                        <span>Matricular Alumno</span>
                    </button>
                </div>
            </div>
        </div>

        <!-- CONTENEDOR PRINCIPAL DEL DASHBOARD -->
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 pt-6 space-y-6">

            {% if messages %}
                <div class="mb-6 space-y-2">
                    {% for message in messages %}
                        <div class="p-4 rounded-xl text-xs font-bold {% if message.tags == 'error' %}bg-red-50 text-red-700 border border-red-200{% else %}bg-emerald-50 text-emerald-800 border border-emerald-200{% endif %}">
                            {{ message }}
                        </div>
                    {% endfor %}
                </div>
            {% endif %}

            <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
                <!-- Formulario Rápido de Matriculación (1 span) -->
                <div class="lg:col-span-1">
                    <div class="bg-white rounded-3xl border border-zinc-200/80 shadow-xl p-6 sm:p-8 sticky top-28">
                        <h2 class="text-lg font-black text-zinc-900 mb-2 flex items-center gap-2">
                            <c-lbi.rmx.user-add w="1.35rem" h="1.35rem" class="text-emerald-600 shrink-0" />
                            <span>Matricular Alumno</span>
                        </h2>
                        <p class="text-xs text-zinc-500 font-medium mb-6">Asigna un alumno del directorio a cualquiera de los cursos publicados en el LMS.</p>

                        <form method="post" action="{% url 'studio:studio_enroll_student' %}" class="space-y-4">
                            {% csrf_token %}
                            <div>
                                <label class="block text-xs font-bold text-zinc-700 uppercase tracking-wider mb-1">Seleccionar Alumno</label>
                                <select name="user_id" required
                                        class="w-full px-4 py-2.5 bg-zinc-50 border border-zinc-300 rounded-xl text-xs font-semibold focus:bg-white focus:outline-none focus:ring-2 focus:ring-emerald-500">
                                    <option value="">Buscar en directorio de alumnos...</option>
                                    {% for st in students %}
                                        <option value="{{ st.id }}">{{ st.get_full_name|default:st.username }} ({{ st.profile.rut|default:st.email }})</option>
                                    {% endfor %}
                                </select>
                            </div>

                            <div>
                                <label class="block text-xs font-bold text-zinc-700 uppercase tracking-wider mb-1">Curso o Programa a Matricular</label>
                                <select name="course_id" required
                                        class="w-full px-4 py-2.5 bg-zinc-50 border border-zinc-300 rounded-xl text-xs font-semibold focus:bg-white focus:outline-none focus:ring-2 focus:ring-emerald-500">
                                    <option value="">Seleccionar curso publicado...</option>
                                    {% for c in courses %}
                                        <option value="{{ c.id }}">{{ c.title }}</option>
                                    {% endfor %}
                                </select>
                            </div>

                            <div class="pt-2">
                                <button type="submit"
                                        class="w-full py-3 px-6 bg-gradient-to-r from-emerald-600 to-emerald-700 hover:from-emerald-700 text-white font-black rounded-xl shadow-lg shadow-emerald-600/20 uppercase tracking-wider text-xs transition-all flex items-center justify-center gap-2">
                                    <c-lbi.rmx.check w="1.1rem" h="1.1rem" />
                                    <span>Asignar Matrícula →</span>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>

                <!-- Tabla del Directorio y Matrículas (2 spans) -->
                <div class="lg:col-span-2 space-y-6">
                    <div class="bg-white rounded-3xl border border-zinc-200/80 shadow-xl p-6 sm:p-8">
                        <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-6">
                            <div>
                                <h2 class="text-lg font-black text-zinc-900">Directorio de Alumnos & Matrículas</h2>
                                <p class="text-xs text-zinc-500 font-medium">Historial completo de inscripciones del ecosistema</p>
                            </div>

                            <!-- Buscador en vivo -->
                            <form method="get" action="{% url 'studio:studio_students' %}" class="flex items-center gap-2">
                                <input type="text" name="q" value="{{ query }}" placeholder="Buscar RUT, nombre o empresa..."
                                       class="px-4 py-2 bg-zinc-50 border border-zinc-300 rounded-xl text-xs font-medium focus:bg-white focus:outline-none focus:ring-2 focus:ring-emerald-500">
                                <button type="submit" class="px-4 py-2 bg-zinc-900 hover:bg-zinc-800 text-white rounded-xl text-xs font-bold uppercase tracking-wider">
                                    Buscar
                                </button>
                                {% if query %}
                                    <a href="{% url 'studio:studio_students' %}" class="text-xs text-red-600 font-bold ml-1">Limpiar</a>
                                {% endif %}
                            </form>
                        </div>

                        <div class="overflow-x-auto">
                            <table class="w-full text-left border-collapse">
                                <thead>
                                    <tr class="border-b border-zinc-200 text-[11px] font-black uppercase text-zinc-400 tracking-wider">
                                        <th class="py-3 px-2">Alumno</th>
                                        <th class="py-3 px-2">RUT / DNI</th>
                                        <th class="py-3 px-2">Curso Inscrito</th>
                                        <th class="py-3 px-2">Empresa</th>
                                        <th class="py-3 px-2 text-right">Fecha</th>
                                    </tr>
                                </thead>
                                <tbody class="divide-y divide-zinc-100 text-xs font-medium">
                                    {% for enroll in enrollments %}
                                        <tr class="hover:bg-zinc-50 transition-colors">
                                            <td class="py-3.5 px-2">
                                                <div class="font-bold text-zinc-900">{{ enroll.user.get_full_name|default:enroll.user.username }}</div>
                                                <div class="text-[11px] text-zinc-400">{{ enroll.user.email }}</div>
                                            </td>
                                            <td class="py-3.5 px-2 font-mono font-semibold text-zinc-700">
                                                {{ enroll.user.profile.rut|default:"—" }}
                                            </td>
                                            <td class="py-3.5 px-2 font-bold text-emerald-800">
                                                {{ enroll.course.title|truncatechars:38 }}
                                            </td>
                                            <td class="py-3.5 px-2 text-zinc-600">
                                                {{ enroll.user.profile.company|default:"—" }}
                                            </td>
                                            <td class="py-3.5 px-2 text-right text-zinc-400">
                                                {{ enroll.enrolled_at|date:"d M, Y" }}
                                            </td>
                                        </tr>
                                    {% empty %}
                                        <tr>
                                            <td colspan="5" class="py-8 text-center text-zinc-400 font-medium">
                                                No se encontraron matriculaciones con el filtro actual.
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</c-layouts.app>
