{% load lb_tags %}

<c-layouts.app title="Portal E-Learning | Lo Justo LMS">
    <div class="bg-zinc-100 min-h-screen pb-20">
        <!-- BARRA SUPERIOR DEL PORTAL E-LEARNING SAAS -->
        <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>
                    <h1 class="text-2xl font-black text-zinc-900 flex items-center gap-2.5">
                        <c-lbi.rmx.home-4 w="1.5rem" h="1.5rem" class="text-emerald-600" />
                        <span>Mis Cursos</span>
                    </h1>
                    <p class="text-xs text-zinc-500 font-medium mt-0.5">Revisa tu avance y continúa con tus lecciones.</p>
                </div>

                <div class="flex items-center gap-3">
                    <div class="px-3.5 py-2 rounded-xl bg-zinc-100 border border-zinc-200 text-xs font-medium text-zinc-600 hidden md:flex items-center gap-2">
                        <span class="w-2 h-2 rounded-full bg-emerald-500"></span>
                        <span>Alumno: <strong class="text-zinc-900">{{ user.get_full_name|default:user.username }}</strong></span>
                    </div>
                </div>
            </div>
        </div>

        <!-- CONTENEDOR PRINCIPAL DEL PORTAL -->
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 pt-8 space-y-10">
            {% if not in_progress and not completed %}
            <div class="bg-white border border-zinc-200/80 rounded-2xl p-10 text-center max-w-xl mx-auto space-y-5 shadow-sm">
                <div class="w-12 h-12 rounded-xl bg-emerald-100 text-emerald-800 flex items-center justify-center mx-auto border border-emerald-200">
                    <c-lbi.rmx.book-open w="1.5rem" h="1.5rem" class="shrink-0" />
                </div>
                <div class="space-y-1.5">
                    <h3 class="text-base font-black text-zinc-900">No tienes programas asignados actualmente</h3>
                    <p class="text-xs text-zinc-500 leading-relaxed font-medium">
                        Tu panel está listo. Explora las especialidades disponibles en el catálogo de metrología, calibración y normativas ISO.
                    </p>
                </div>
                <div class="pt-2">
                    <a href="{% url 'courses:catalog' %}" class="inline-flex items-center gap-2 bg-emerald-600 hover:bg-emerald-700 text-white font-bold uppercase text-xs tracking-wider rounded-xl px-6 py-3 shadow-sm transition-all">
                        <span>Ver Catálogo Institucional</span>
                    </a>
                </div>
            </div>
            {% endif %}

        <!-- CURSOS EN PROGRESO -->
        {% if in_progress %}
        <div class="space-y-6">
            <div class="flex items-center justify-between border-b border-zinc-200 pb-4">
                <h2 class="text-2xl font-black text-zinc-900 flex items-center space-x-2.5">
                    <span class="w-3 h-3 rounded-full bg-amber-400 animate-pulse"></span>
                    <span>Programas en Progreso</span>
                </h2>
                <span class="text-xs font-bold uppercase tracking-wider text-zinc-400">{{ in_progress|length }} curso(s) activos</span>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
                {% for item in in_progress %}
                <c-lb.card class="bg-white border border-zinc-200 rounded-2xl shadow-lg hover:shadow-2xl transition-all duration-300 flex flex-col justify-between overflow-hidden">
                    <div>
                        <div class="h-2 bg-gradient-to-r from-amber-400 to-emerald-500 w-full"></div>
                        <c-lb.card.body class="p-6 sm:p-8 space-y-4">
                            <div class="flex items-center justify-between">
                                <span class="text-[11px] font-mono font-bold uppercase tracking-wider text-emerald-600">
                                    {{ item.enrollment.course.iso_standard }}
                                </span>
                                <span class="text-xs font-black text-amber-500">{{ item.percent }}% completado</span>
                            </div>

                            <c-lb.card.title class="text-lg font-black text-zinc-900 leading-snug">
                                {{ item.enrollment.course.title }}
                            </c-lb.card.title>

                            <!-- Barra de progreso -->
                            <div class="w-full bg-zinc-100 rounded-full h-2 overflow-hidden">
                                <div class="bg-gradient-to-r from-emerald-500 to-amber-400 h-2 rounded-full" style="width: {{ item.percent }}%"></div>
                            </div>
                        </c-lb.card.body>
                    </div>

                    <div class="p-6 sm:p-8 pt-0 border-t border-zinc-100 mt-4">
                        <a href="{% url 'campus:resume_course' slug=item.enrollment.course.slug %}" class="btn btn-block bg-emerald-600 hover:bg-emerald-500 text-white font-black uppercase text-xs tracking-wider rounded-xl shadow-md flex items-center justify-center space-x-2 border-0 h-11">
                            <span>Continuar Lecciones</span>
                            <c-lbi.rmx.play-circle w="1.125rem" h="1.125rem" />
                        </a>
                    </div>
                </c-lb.card>
                {% endfor %}
            </div>
        </div>
        {% endif %}

        <!-- CURSOS COMPLETADOS / DIPLOMAS -->
        {% if completed %}
        <div class="space-y-6">
            <div class="flex items-center justify-between border-b border-zinc-200 pb-4">
                <h2 class="text-2xl font-black text-zinc-900 flex items-center space-x-2.5">
                    <span class="w-3 h-3 rounded-full bg-emerald-500"></span>
                    <span>Diplomas & Programas Finalizados</span>
                </h2>
                <span class="text-xs font-bold uppercase tracking-wider text-zinc-400">{{ completed|length }} certification(es)</span>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
                {% for item in completed %}
                <c-lb.card class="bg-white border-2 border-emerald-500/40 rounded-2xl shadow-xl hover:shadow-2xl transition-all duration-300 flex flex-col justify-between overflow-hidden relative">
                    <div>
                        <div class="h-2 bg-emerald-500 w-full"></div>
                        <c-lb.card.body class="p-6 sm:p-8 space-y-4">
                            <div class="flex items-center justify-between">
                                <span class="px-2 py-0.5 rounded bg-emerald-500/10 text-emerald-600 text-[10px] font-black uppercase tracking-wider">
                                    ✓ Aprobado 100%
                                </span>
                                {% if item.enrollment.certificate %}
                                <span class="text-xs font-mono font-bold text-amber-500">
                                    {{ item.enrollment.certificate.certificate_code }}
                                </span>
                                {% endif %}
                            </div>

                            <c-lb.card.title class="text-lg font-black text-zinc-900 leading-snug">
                                {{ item.enrollment.course.title }}
                            </c-lb.card.title>

                            <p class="text-xs text-zinc-500">
                                Finalizado el {{ item.enrollment.enrolled_at|date:"d/m/Y" }}
                            </p>
                        </c-lb.card.body>
                    </div>

                    <div class="p-6 sm:p-8 pt-0 border-t border-zinc-100 mt-4 flex flex-col space-y-2.5">
                        {% if item.enrollment.certificate %}
                        <a href="{% url 'certificates:download' certificate_code=item.enrollment.certificate.certificate_code %}" class="btn btn-block bg-amber-400 hover:bg-amber-300 text-zinc-950 font-black uppercase text-xs tracking-wider rounded-xl shadow-md flex items-center justify-center space-x-2 border-0 h-11">
                            <c-lbi.rmx.file-download w="1rem" h="1rem" class="text-zinc-950" />
                            <span>Descargar PDF (Alta Resolución)</span>
                        </a>
                        <a href="{% url 'certificates:verify_detail' certificate_code=item.enrollment.certificate.certificate_code %}" class="btn btn-block bg-zinc-900 text-white font-bold uppercase text-xs tracking-wider rounded-xl shadow-sm flex items-center justify-center space-x-2 border-0 h-10">
                            <span>🏆 Ver Diploma Digital & QR</span>
                        </a>
                        {% elif item.enrollment.course.evaluations.first %}
                        <a href="{% url 'campus:quiz_detail' slug=item.enrollment.course.slug evaluation_id=item.enrollment.course.evaluations.first.id %}" class="btn btn-block bg-gradient-to-r from-emerald-600 to-emerald-500 hover:from-emerald-500 hover:to-emerald-400 text-white font-black uppercase text-xs tracking-wider rounded-xl shadow-lg flex items-center justify-center space-x-2 border-0 h-11">
                            <span>📝 Rendir Examen y Certificar</span>
                        </a>
                        <a href="{% url 'campus:resume_course' slug=item.enrollment.course.slug %}" class="btn btn-block btn-outline text-xs font-bold rounded-xl h-10">
                            Repasar Contenido
                        </a>
                        {% else %}
                        <a href="{% url 'campus:resume_course' slug=item.enrollment.course.slug %}" class="btn btn-block btn-outline text-xs font-bold rounded-xl h-11">
                            Repasar Contenido
                        </a>
                        {% endif %}
                    </div>
                </c-lb.card>
                {% endfor %}
            </div>
        </div>
        {% endif %}
    </div>
</c-layouts.app>
