<c-layouts.app title="Auditoría de Diplomas | 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">Auditoría ISO 17025</span>
                    </div>
                    <h1 class="text-2xl font-black text-zinc-900 flex items-center gap-2.5">
                        <c-lbi.rmx.qr-code w="1.5rem" h="1.5rem" class="text-emerald-600" />
                        <span>Diplomas & Certificaciones</span>
                    </h1>
                </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 %}

            <!-- SECCIÓN: CUSTODIA Y FIRMA DIGITAL CRIPTOGRÁFICA (.P12 / .PFX) -->
            <div class="bg-gradient-to-r from-zinc-900 via-zinc-900 to-emerald-950 text-white rounded-3xl border border-zinc-800 shadow-xl p-6 sm:p-8">
                <div class="flex flex-col lg:flex-row items-start lg:items-center justify-between gap-6">
                    <div class="space-y-2 max-w-xl">
                        <div class="inline-flex items-center gap-2 px-3 py-1 rounded-full bg-emerald-500/20 border border-emerald-500/30 text-emerald-300 text-xs font-black uppercase tracking-wider">
                            <span>🔒 Firma Digital PAdES / Adobe Reader</span>
                        </div>
                        <h2 class="text-xl font-black text-white">Certificado Digital de Emisión</h2>
                        <p class="text-xs text-zinc-400 leading-relaxed">
                            El sistema estampa la firma criptográfica sobre el PDF de cada diploma oficial emitido, certificando la autenticidad e inalterabilidad del documento ante cualquier entidad.
                        </p>
                        {% if active_signature %}
                            <div class="pt-2 flex items-center gap-3">
                                <span class="w-2.5 h-2.5 rounded-full bg-emerald-400 animate-pulse"></span>
                                <span class="text-xs font-bold text-emerald-300">Certificado Activo: {{ active_signature.name }}</span>
                                <span class="text-[11px] text-zinc-400 font-mono">({{ active_signature.signer_name }} - {{ active_signature.signer_role }})</span>
                            </div>
                        {% else %}
                            <div class="pt-2 flex items-center gap-3">
                                <span class="w-2.5 h-2.5 rounded-full bg-amber-400"></span>
                                <span class="text-xs font-bold text-amber-300">Sin archivo .p12 activo. Los diplomas se emiten con Sello QR y Hash SHA-256 institucional.</span>
                            </div>
                        {% endif %}
                    </div>

                    <!-- Acciones / Carga de Firma -->
                    <div class="flex flex-col sm:flex-row items-center gap-3 w-full lg:w-auto">
                        <form method="post" action="{% url 'studio:studio_certificates' %}" class="w-full sm:w-auto">
                            {% csrf_token %}
                            <input type="hidden" name="action" value="generate_demo_signature">
                            <button type="submit" class="w-full sm:w-auto px-4 py-2.5 bg-emerald-600 hover:bg-emerald-500 text-white rounded-xl text-xs font-black uppercase tracking-wider transition-all shadow-lg flex items-center justify-center gap-2">
                                <span>⚡ Generar Certificado Digital Institucional</span>
                            </button>
                        </form>
                    </div>
                </div>

                <!-- Formulario desplegable para subir .p12 propio -->
                <div class="mt-6 pt-6 border-t border-zinc-800/80">
                    <h4 class="text-xs font-black uppercase tracking-wider text-zinc-400 mb-3">+ Cargar Llave Privada / Certificado .p12 / .pfx Comercial</h4>
                    <form method="post" action="{% url 'studio:studio_certificates' %}" enctype="multipart/form-data" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3 items-end">
                        {% csrf_token %}
                        <input type="hidden" name="action" value="upload_signature">
                        <div>
                            <label class="block text-[10px] font-bold uppercase text-zinc-400 mb-1">Archivo .p12 / .pfx</label>
                            <input type="file" name="pfx_file" required accept=".p12,.pfx" class="w-full px-3 py-1.5 bg-zinc-800 border border-zinc-700 rounded-lg text-xs text-zinc-300 file:mr-2 file:py-1 file:px-2 file:rounded file:border-0 file:text-xs file:bg-emerald-600 file:text-white">
                        </div>
                        <div>
                            <label class="block text-[10px] font-bold uppercase text-zinc-400 mb-1">Contraseña</label>
                            <input type="password" name="p12_password" placeholder="Contraseña de la llave..." class="w-full px-3 py-2 bg-zinc-800 border border-zinc-700 rounded-lg text-xs text-white focus:outline-none focus:border-emerald-500">
                        </div>
                        <div>
                            <label class="block text-[10px] font-bold uppercase text-zinc-400 mb-1">Nombre Firmante</label>
                            <input type="text" name="signer_name" value="Director Técnico & Calidad" class="w-full px-3 py-2 bg-zinc-800 border border-zinc-700 rounded-lg text-xs text-white focus:outline-none focus:border-emerald-500">
                        </div>
                        <button type="submit" class="w-full py-2 px-4 bg-zinc-700 hover:bg-zinc-600 text-white font-bold rounded-lg text-xs uppercase tracking-wider transition-colors">
                            Guardar y Activar
                        </button>
                    </form>
                </div>
            </div>

            <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">Diplomas Oficiales Emitidos ({{ certificates.count }})</h2>
                        <p class="text-xs text-zinc-500 font-medium">Todos los certificados son validados mediante código QR inviolable</p>
                    </div>

                    <form method="get" action="{% url 'studio:studio_certificates' %}" class="flex items-center gap-2">
                        <input type="text" name="q" value="{{ query }}" placeholder="Código QR, nombre o curso..."
                               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_certificates' %}" 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">Código QR / Certificado</th>
                                <th class="py-3 px-2">Alumno Titular</th>
                                <th class="py-3 px-2">RUT / Empresa</th>
                                <th class="py-3 px-2">Programa Metrológico</th>
                                <th class="py-3 px-2">Emisión</th>
                                <th class="py-3 px-2 text-right">Acción</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-zinc-100 text-xs font-medium">
                            {% for cert in certificates %}
                                <tr class="hover:bg-zinc-50 transition-colors">
                                    <td class="py-3.5 px-2 font-mono font-bold text-emerald-800">
                                        {{ cert.certificate_code }}
                                    </td>
                                    <td class="py-3.5 px-2 font-bold text-zinc-900">
                                        {{ cert.enrollment.user.get_full_name|default:cert.enrollment.user.username }}
                                    </td>
                                    <td class="py-3.5 px-2 text-zinc-600">
                                        {{ cert.enrollment.user.profile.rut|default:"—" }} <br>
                                        <span class="text-[10px] text-zinc-400">{{ cert.enrollment.user.profile.company }}</span>
                                    </td>
                                    <td class="py-3.5 px-2 font-semibold text-zinc-800">
                                        {{ cert.enrollment.course.title|truncatechars:38 }}
                                    </td>
                                    <td class="py-3.5 px-2 text-zinc-400">
                                        {{ cert.issued_at|date:"d M, Y" }}
                                    </td>
                                    <td class="py-3.5 px-2 text-right">
                                        <div class="flex items-center justify-end space-x-2">
                                            <a href="{% url 'certificates:verify_detail' cert.certificate_code %}" target="_blank"
                                               class="px-2.5 py-1 bg-zinc-100 hover:bg-zinc-200 text-zinc-700 rounded-lg text-[11px] font-bold">
                                                Ver QR
                                            </a>
                                            <a href="{% url 'certificates:download' cert.certificate_code %}" target="_blank"
                                               class="px-2.5 py-1 bg-emerald-600 hover:bg-emerald-700 text-white rounded-lg text-[11px] font-bold">
                                                PDF
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            {% empty %}
                                <tr>
                                    <td colspan="6" class="py-8 text-center text-zinc-400 font-medium">
                                        No hay diplomas emitidos registrados con el filtro actual.
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</c-layouts.app>
