{% load lb_tags %}

<c-layouts.app title="Gestión de Especialidades (Categorías) | LMS Studio">
    <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">Especialidades & Categorías</span>
                    </div>
                    <h1 class="text-2xl font-black text-zinc-900 flex items-center gap-2.5">
                        <c-lbi.rmx.dashboard w="1.5rem" h="1.5rem" class="text-emerald-600" />
                        <span>Catálogo de Especialidades</span>
                    </h1>
                </div>

                <div class="flex items-center gap-2.5">
                    <button onclick="document.getElementById('modal_create_category').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>Nueva Especialidad</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="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 %}

            <!-- TABLA SAAS DE ESPECIALIDADES -->
            <div class="bg-white border border-zinc-200/80 rounded-2xl shadow-sm overflow-hidden">
                <div class="p-6 border-b border-zinc-200/60 flex items-center justify-between">
                    <div>
                        <h2 class="text-base font-black text-zinc-900">Ramas de Especialización Activas</h2>
                        <p class="text-xs font-medium text-zinc-500 mt-0.5">Los cursos creados en el Studio se clasifican y filtran bajo estas especialidades.</p>
                    </div>
                </div>

                <div class="overflow-x-auto">
                    <table class="w-full text-left border-collapse">
                        <thead>
                            <tr class="bg-zinc-50 border-b border-zinc-200/60 text-[11px] font-black uppercase tracking-wider text-zinc-400">
                                <th class="py-3.5 px-6">Especialidad & Icono</th>
                                <th class="py-3.5 px-6">Slug (Identificador URL)</th>
                                <th class="py-3.5 px-6">Cursos Asignados</th>
                                <th class="py-3.5 px-6 text-right">Acción</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-zinc-200/60 text-xs font-medium text-zinc-700">
                            {% for category in categories %}
                            <tr class="hover:bg-zinc-50/80 transition-colors">
                                <td class="py-4 px-6 font-bold text-zinc-900">
                                    <div class="flex items-center space-x-3">
                                        <div class="w-9 h-9 rounded-xl bg-emerald-100 text-emerald-800 flex items-center justify-center shrink-0 border border-emerald-200">
                                            <c-lbi.rmx.book-open w="1.1rem" h="1.1rem" class="shrink-0" />
                                        </div>
                                        <div>
                                            <div class="font-black text-sm text-zinc-900">{{ category.title }}</div>
                                            {% if category.description %}
                                                <div class="text-[11px] font-normal text-zinc-500 mt-0.5">{{ category.description|truncatewords:10 }}</div>
                                            {% endif %}
                                        </div>
                                    </div>
                                </td>
                                <td class="py-4 px-6">
                                    <code class="px-2 py-1 bg-zinc-100 rounded text-zinc-600 text-[11px] font-mono">{{ category.slug }}</code>
                                </td>
                                <td class="py-4 px-6">
                                    <span class="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-black {% if category.courses_count > 0 %}bg-emerald-50 text-emerald-700 border border-emerald-200/60{% else %}bg-zinc-100 text-zinc-500{% endif %}">
                                        {{ category.courses_count }} Curso{{ category.courses_count|pluralize:"s" }}
                                    </span>
                                </td>
                                <td class="py-4 px-6 text-right">
                                    {% if category.courses_count == 0 %}
                                    <form action="{% url 'studio:studio_category_delete' category.id %}" method="POST" class="inline" onsubmit="return confirm('¿Eliminar la especialidad {{ category.title }}?');">
                                        {% csrf_token %}
                                        <button type="submit" class="px-3 py-1.5 rounded-lg bg-red-50 hover:bg-red-100 text-red-700 font-bold text-xs uppercase transition-colors">
                                            Eliminar
                                        </button>
                                    </form>
                                    {% else %}
                                    <span class="text-[11px] font-bold text-zinc-400 italic">En uso</span>
                                    {% endif %}
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="4" class="py-12 text-center text-zinc-400 font-medium">
                                    No hay especialidades creadas todavía. Haz clic en "+ Nueva Especialidad" para empezar.
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- MODAL DE CREACIÓN DE ESPECIALIDAD -->
    <dialog id="modal_create_category" class="modal">
        <div class="modal-box bg-white rounded-2xl p-6 shadow-2xl border border-zinc-200 max-w-md">
            <div class="flex items-center justify-between border-b border-zinc-200/60 pb-4 mb-5">
                <h3 class="text-base font-black text-zinc-900 uppercase tracking-wider">Crear Nueva Especialidad</h3>
                <form method="dialog">
                    <button class="btn btn-sm btn-circle btn-ghost text-zinc-400 hover:text-zinc-700 flex items-center justify-center">
                        <c-lbi.rmx.close w="1rem" h="1rem" />
                    </button>
                </form>
            </div>

            <form action="{% url 'studio:studio_category_create' %}" method="POST" class="space-y-4">
                {% csrf_token %}
                <div>
                    <label class="block text-xs font-bold text-zinc-700 uppercase tracking-wider mb-1.5">Título de la Especialidad <span class="text-red-500">*</span></label>
                    <input type="text" name="title" required placeholder="Ej. Termografía Industrial, Electrónica..." class="w-full px-3.5 py-2.5 rounded-xl border border-zinc-300 bg-zinc-50 text-zinc-900 text-sm font-medium focus:border-emerald-600 focus:ring-2 focus:ring-emerald-600/20 outline-none transition-all">
                </div>

                <div>
                    <label class="block text-xs font-bold text-zinc-700 uppercase tracking-wider mb-1.5">Descripción Corta (Opcional)</label>
                    <textarea name="description" rows="2" placeholder="Área enfocada en calibración de instrumentos..." class="w-full px-3.5 py-2 rounded-xl border border-zinc-300 bg-zinc-50 text-zinc-900 text-sm font-medium focus:border-emerald-600 focus:ring-2 focus:ring-emerald-600/20 outline-none transition-all"></textarea>
                </div>

                <div class="pt-2 flex items-center justify-end gap-3 border-t border-zinc-200/60 mt-6">
                    <button type="button" onclick="document.getElementById('modal_create_category').close()" class="px-4 py-2.5 rounded-xl font-bold text-xs uppercase tracking-wider text-zinc-600 hover:bg-zinc-100 transition-colors">
                        Cancelar
                    </button>
                    <button type="submit" class="px-5 py-2.5 rounded-xl bg-emerald-600 hover:bg-emerald-700 text-white font-bold text-xs uppercase tracking-wider shadow-md hover:shadow-lg transition-all">
                        Crear Especialidad
                    </button>
                </div>
            </form>
        </div>
        <form method="dialog" class="modal-backdrop">
            <button>cerrar</button>
        </form>
    </dialog>
</c-layouts.app>
