<!DOCTYPE html>
<html lang="es" class="{{ 'dark' if tema == 1 else '' }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>UTRAU - Expedientes</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
    <script>
        tailwind.config = { darkMode: 'class', theme: { extend: {} } }
    </script>
</head>
<body class="bg-slate-100 dark:bg-slate-950 text-slate-900 dark:text-slate-100 transition-colors duration-300 min-h-screen">
    
    <nav class="bg-slate-900 border-b border-slate-800 p-4 flex justify-between items-center shadow-md text-white sticky top-0 z-40">
        <div class="flex items-center space-x-3">
            <span class="font-black text-red-500">UTRAU <span class="text-slate-400 font-bold hidden sm:inline">| GESTIÓN DE EXPEDIENTES</span></span>
        </div>
        <a href="{{ url_for('index') }}" class="text-xs font-bold uppercase text-slate-400 hover:text-white transition"><i class="fa-solid fa-arrow-left mr-2"></i>Volver</a>
    </nav>

    <div class="max-w-7xl mx-auto px-6 mt-4">
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    {% if category == 'success' %}
                        <div class="bg-emerald-100 dark:bg-emerald-900/30 border border-emerald-500 text-emerald-700 dark:text-emerald-400 px-4 py-3 rounded-xl flex items-center mb-4 shadow-sm">
                            <i class="fa-solid fa-circle-check mr-3 text-lg"></i><span class="text-sm font-bold">{{ message }}</span>
                        </div>
                    {% elif category == 'error' %}
                        <div class="bg-red-100 dark:bg-red-900/30 border border-red-500 text-red-700 dark:text-red-400 px-4 py-3 rounded-xl flex items-center mb-4 shadow-sm">
                            <i class="fa-solid fa-triangle-exclamation mr-3 text-lg"></i><span class="text-sm font-bold">{{ message }}</span>
                        </div>
                    {% endif %}
                {% endfor %}
            {% endif %}
        {% endwith %}
    </div>

    <main class="p-6 max-w-7xl mx-auto">
        <div class="flex justify-between items-center mb-6">
            <div>
                <h1 class="text-2xl font-black uppercase tracking-tight">Expedientes Activos</h1>
                <p class="text-xs font-bold text-slate-500 uppercase tracking-widest">Trazabilidad</p>
            </div>
            {% if user_rol >= 3 %}
            <button onclick="document.getElementById('modalNuevo').classList.remove('hidden'); document.getElementById('modalNuevo').classList.add('flex');" class="bg-red-600 hover:bg-red-700 text-white px-4 py-2 rounded-xl font-black uppercase tracking-wider text-xs transition shadow-md flex items-center">
                <i class="fa-solid fa-plus mr-2"></i>Abrir Expediente
            </button>
            {% endif %}
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            {% for c in conflictos %}
            <a href="{{ url_for('conflictos.detalle', id_conflicto=c.id_conflicto) }}" class="bg-white dark:bg-slate-900 border {% if c.estado == 'Activo' %}border-red-200 dark:border-red-900/30{% else %}border-slate-200 dark:border-slate-800{% endif %} rounded-2xl p-5 hover:shadow-lg transition group block relative overflow-hidden">
                
                {% if c.estado == 'Activo' %}
                    <div class="absolute top-0 left-0 w-1 h-full bg-red-500"></div>
                {% else %}
                    <div class="absolute top-0 left-0 w-1 h-full bg-slate-500"></div>
                {% endif %}

                <div class="flex justify-between items-start mb-3 pl-2">
                    <span class="text-[10px] font-black uppercase tracking-widest px-2 py-0.5 rounded {% if c.estado == 'Activo' %}bg-red-100 text-red-700 dark:bg-red-900/50 dark:text-red-400{% else %}bg-slate-200 text-slate-600 dark:bg-slate-800 dark:text-slate-400{% endif %}">
                        {{ c.estado }}
                    </span>
                    <span class="text-[10px] font-bold text-slate-400 uppercase"><i class="fa-regular fa-clock mr-1"></i>{{ c.fecha_creacion.strftime('%d/%m/%Y') }}</span>
                </div>
                
                <div class="pl-2">
                    <h3 class="text-lg font-black leading-tight mb-1 text-slate-800 dark:text-slate-100 group-hover:text-red-500 transition">{{ c.titulo }}</h3>
                    <p class="text-xs font-bold text-slate-500 uppercase tracking-widest mb-4"><i class="fa-solid fa-building mr-1"></i>{{ c.empresa_nombre }}</p>
                    
                    {% if c.estado == 'Activo' %}
                    <div class="mb-4">
                        <span class="text-[9px] font-black uppercase tracking-widest px-2 py-1 rounded 
                        {% if c.esperando_a == 'UTRAU' %} bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-400
                        {% elif c.esperando_a == 'Empresa' %} bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-400
                        {% else %} bg-purple-100 text-purple-700 dark:bg-purple-900/40 dark:text-purple-400 {% endif %}">
                            <i class="fa-solid fa-hourglass-half mr-1"></i>Turno: {{ c.esperando_a }}
                        </span>
                    </div>
                    {% endif %}

                    <div class="pt-3 border-t border-slate-100 dark:border-slate-800/50 flex justify-between items-center">
                        <div class="flex items-center space-x-2">
                            <div class="w-6 h-6 rounded-full bg-slate-100 dark:bg-slate-800 text-slate-600 dark:text-slate-400 flex items-center justify-center text-[10px] font-black">
                                <i class="fa-solid fa-user-tie"></i>
                            </div>
                            <span class="text-[10px] font-bold text-slate-600 dark:text-slate-400 uppercase">@{{ c.responsable_nombre }}</span>
                        </div>
                        <span class="text-[9px] font-black px-2 py-1 bg-slate-100 dark:bg-slate-800 text-slate-500 rounded uppercase tracking-widest">{{ c.ambito }}</span>
                    </div>
                </div>
            </a>
            {% else %}
            <div class="col-span-full py-12 text-center border-2 border-dashed border-slate-300 dark:border-slate-700 rounded-2xl">
                <i class="fa-solid fa-folder-open text-4xl text-slate-300 dark:text-slate-600 mb-3"></i>
                <p class="text-sm font-bold text-slate-500 uppercase tracking-widest">No hay expedientes activos asignados a tu usuario.</p>
            </div>
            {% endfor %}
        </div>
    </main>

    {% if user_rol >= 3 %}
    <div id="modalNuevo" class="fixed inset-0 bg-slate-900/60 z-50 hidden items-center justify-center backdrop-blur-sm p-4">
        <div class="bg-white dark:bg-slate-900 p-6 rounded-3xl shadow-2xl w-full max-w-md border border-slate-200 dark:border-slate-800 flex flex-col max-h-[90vh]">
            <div class="flex justify-between items-center mb-4 border-b border-slate-100 dark:border-slate-800 pb-2">
                <h3 class="text-lg font-black uppercase tracking-tight text-red-600"><i class="fa-solid fa-folder-plus mr-2"></i>Abrir Expediente</h3>
                <button type="button" onclick="document.getElementById('modalNuevo').classList.add('hidden'); document.getElementById('modalNuevo').classList.remove('flex');" class="text-slate-400 hover:text-red-500"><i class="fa-solid fa-xmark text-xl"></i></button>
            </div>
            
            <form action="{{ url_for('conflictos.crear') }}" method="POST" class="flex flex-col flex-1 overflow-hidden space-y-4">
                <div>
                    <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase">Carátula / Título*</label>
                    <input type="text" name="titulo" required placeholder="Ej: Negociación Salarial G23" class="w-full px-4 py-3 border-2 rounded-xl dark:bg-slate-800 dark:border-slate-700 outline-none focus:border-red-500 font-bold text-sm">
                </div>
                
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase">Entidad*</label>
                        <select name="id_empresa" required class="w-full px-3 py-2 border-2 rounded-xl dark:bg-slate-800 dark:border-slate-700 outline-none focus:border-red-500 font-bold text-sm">
                            <option value="">-- Entidad --</option>
                            {% for e in empresas %}
                            <option value="{{ e.id_empresa }}" class="text-black">{{ e.nombre_fantasia }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div>
                        <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase">Ámbito</label>
                        <select name="ambito" required class="w-full px-3 py-2 border-2 rounded-xl dark:bg-slate-800 dark:border-slate-700 outline-none focus:border-red-500 font-bold text-sm">
                            <option value="Bipartito" class="text-black">Bipartito</option>
                            <option value="Tripartito" class="text-black">Tripartito</option>
                            <option value="Interno" class="text-black">Interno</option>
                            <option value="Judicial" class="text-black">Judicial</option>
                        </select>
                    </div>
                </div>

                <div>
                    <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase">Responsable Directo*</label>
                    <select name="id_responsable" required class="w-full px-4 py-3 border-2 rounded-xl dark:bg-slate-800 dark:border-slate-700 outline-none focus:border-red-500 font-bold text-sm">
                        {% for r in responsables %}
                        <option value="{{ r.id_usuario }}" class="text-black" {% if r.id_usuario == user_id %}selected{% endif %}>@{{ r.alias }}</option>
                        {% endfor %}
                    </select>
                </div>

                <div class="flex-1 flex flex-col min-h-[150px]">
                    <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase">Convocar Sectores (Opcional)</label>
                    <div class="flex-1 overflow-y-auto border-2 border-slate-200 dark:border-slate-700 rounded-xl p-2 bg-slate-50 dark:bg-slate-800/50 space-y-1">
                        {% for g in grupos %}
                        <label class="flex items-center space-x-2 p-1.5 hover:bg-white dark:hover:bg-slate-700 rounded cursor-pointer transition">
                            <input type="checkbox" name="grupos_ids" value="{{ g.id_grupo }}" class="w-4 h-4 accent-red-600">
                            <span class="text-xs font-bold text-slate-800 dark:text-slate-200">{{ g.nombre_grupo }}</span>
                        </label>
                        {% endfor %}
                    </div>
                    <p class="text-[9px] font-bold text-slate-400 mt-1">El sistema tomará una foto de los compañeros que estén en estos sectores hoy.</p>
                </div>
                
                <button type="submit" class="w-full bg-red-600 hover:bg-red-700 text-white font-black py-4 rounded-xl transition uppercase tracking-wider text-sm shadow-md">
                    Confirmar Registro
                </button>
            </form>
        </div>
    </div>
    {% endif %}

</body>
</html>