<!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">
    <title>UTRAU - Asignaciones</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">
    <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-amber-500">UTRAU <span class="text-slate-400 font-bold">| ASIGNACIONES</span></span>
        </div>
        <a href="{{ url_for('config.panel') }}" class="text-xs font-bold uppercase text-slate-400 hover:text-white transition"><i class="fa-solid fa-arrow-left mr-2"></i>Volver al Panel</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 grid grid-cols-1 lg:grid-cols-3 gap-6 items-start">
        
        <div class="lg:col-span-2 bg-white dark:bg-slate-900 rounded-2xl border border-slate-200 dark:border-slate-800 shadow-sm overflow-hidden flex flex-col">
            <div class="p-6 border-b border-slate-200 dark:border-slate-800 bg-slate-50 dark:bg-slate-900/50 flex justify-between items-center">
                <div>
                    <h2 class="text-xl font-black uppercase tracking-tight">Directiva y Ejecutivo</h2>
                    <p class="text-xs text-slate-500 font-bold mt-1">Vincular compañeros a cargos estatutarios.</p>
                </div>
                <button onclick="limpiarFormulario()" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white text-xs font-black uppercase tracking-wider rounded-lg transition shadow-md">
                    + Asignar Cargo
                </button>
            </div>
            
            <div class="overflow-x-auto">
                <table class="w-full text-left border-collapse">
                    <thead>
                        <tr class="bg-slate-100 dark:bg-slate-800 text-[10px] uppercase tracking-widest text-slate-500 dark:text-slate-400">
                            <th class="p-4 font-black">Compañero</th>
                            <th class="p-4 font-black">Cargo Ocupado</th>
                            <th class="p-4 font-black text-center">Período</th>
                            <th class="p-4 font-black text-center">Estado</th>
                            <th class="p-4 font-black text-right">Acción</th>
                        </tr>
                    </thead>
                    <tbody class="text-sm font-medium">
                        {% for a in lista_asignaciones %}
                        <tr class="border-b border-slate-100 dark:border-slate-800/50 hover:bg-slate-50 dark:hover:bg-slate-800/30 transition {% if a.fecha_fin %}fila-historica hidden opacity-60 grayscale{% endif %}">
                            <td class="p-4">
                                <div class="flex items-center space-x-3">
                                    {% if a.path_foto %}
                                        <img src="{{ a.path_foto }}" class="w-8 h-8 rounded-full object-cover border-2 border-slate-200 dark:border-slate-700">
                                    {% else %}
                                        <div class="w-8 h-8 rounded-full bg-slate-200 dark:bg-slate-800 flex items-center justify-center text-slate-500 font-black border-2 border-slate-300 dark:border-slate-700 text-xs">
                                            {{ a.nombre_completo[0:1] }}
                                        </div>
                                    {% endif %}
                                    <span class="font-bold text-slate-800 dark:text-slate-200">{{ a.nombre_completo }}</span>
                                </div>
                            </td>
                            <td class="p-4">
                                <span class="font-black text-amber-600 dark:text-amber-500">{{ a.nombre_cargo }}</span>
                                {% if a.es_ejecutivo %}
                                <p class="text-[10px] font-bold uppercase tracking-widest text-purple-500">Mesa Ejecutiva</p>
                                {% endif %}
                            </td>
                            <td class="p-4 text-center text-xs text-slate-500 font-bold">
                                {{ a.fecha_inicio }} <br> al <br> {{ a.fecha_fin or 'Actualidad' }}
                            </td>
                            <td class="p-4 text-center">
                                {% if a.fecha_fin == None %}
                                    <span class="px-2 py-1 rounded bg-emerald-100 text-emerald-700 text-[10px] font-black uppercase">Vigente</span>
                                {% else %}
                                    <span class="px-2 py-1 rounded bg-slate-200 text-slate-600 text-[10px] font-black uppercase">Histórico</span>
                                {% endif %}
                            </td>
                            <td class="p-4 text-right">
                                <button onclick="editarAsignacion('{{ a.id_asignacion }}', '{{ a.id_usuario }}', '{{ a.id_cargo }}', '{{ a.fecha_inicio }}', '{{ a.fecha_fin or '' }}', '{{ a.version }}')" class="p-2 bg-slate-200 dark:bg-slate-700 hover:bg-blue-500 hover:text-white text-slate-700 dark:text-white rounded-lg transition" title="Editar">
                                    <i class="fa-solid fa-pen"></i>
                                </button>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <div class="p-4 border-t border-slate-200 dark:border-slate-800 bg-slate-50 dark:bg-slate-900/50 text-center">
                <button id="btnToggleHistorico" onclick="toggleHistorico()" class="text-xs font-black uppercase tracking-wider text-slate-500 hover:text-amber-600 dark:hover:text-amber-500 transition px-4 py-2 rounded-lg hover:bg-slate-200 dark:hover:bg-slate-800">
                    <i class="fa-solid fa-clock-rotate-left mr-2"></i>Ver Historial de Asignaciones
                </button>
            </div>
        </div>

        <div class="bg-white dark:bg-slate-900 rounded-2xl border border-slate-200 dark:border-slate-800 shadow-sm h-fit">
            <div class="p-6 border-b border-slate-200 dark:border-slate-800 bg-slate-50 dark:bg-slate-900/50">
                <h2 class="text-lg font-black uppercase tracking-tight" id="titulo_formulario">Nueva Asignación</h2>
            </div>
            
            <div class="p-6">
                <form action="{{ url_for('config.guardar_asignacion') }}" method="POST" class="space-y-4">
                    
                    <input type="hidden" name="id_asignacion" id="form_id_asignacion" value="">
                    <input type="hidden" name="version" id="form_version" value="1">
                    
                    <div>
                        <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Compañero (Padrón)</label>
                        <select name="id_usuario" id="form_id_usuario" required class="w-full px-4 py-3 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-amber-500 outline-none font-bold text-sm">
                            <option value="" class="text-black">-- Seleccione Compañero --</option>
                            {% for u in lista_usuarios %}
                            <option value="{{ u.id_usuario }}" class="text-black">{{ u.nombre_completo }} (@{{ u.alias }})</option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <div>
                        <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Cargo a Asignar</label>
                        <select name="id_cargo" id="form_id_cargo" required class="w-full px-4 py-3 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-amber-500 outline-none font-bold text-sm">
                            <option value="" class="text-black">-- Seleccione Cargo --</option>
                            {% for c in lista_cargos %}
                            <option value="{{ c.id_cargo }}" class="text-black">{{ c.nombre_cargo }} {% if c.es_ejecutivo %}(Ejecutivo){% endif %}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="grid grid-cols-2 gap-4 pt-2">
                        <div>
                            <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Fecha Inicio*</label>
                            <input type="date" name="fecha_inicio" id="form_fecha_inicio" required class="w-full px-3 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-amber-500 outline-none font-bold text-sm">
                        </div>
                        <div>
                            <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase text-red-500">Fecha Fin (Cese)</label>
                            <input type="date" name="fecha_fin" id="form_fecha_fin" class="w-full px-3 py-2 rounded-xl border-2 border-red-200 dark:border-red-900/50 bg-red-50 dark:bg-red-900/10 focus:border-red-500 outline-none font-bold text-sm text-red-600">
                            <p class="text-[9px] font-bold text-slate-400 mt-1">Llenar solo si finalizó mandato.</p>
                        </div>
                    </div>

                    <div class="pt-4 border-t border-slate-100 dark:border-slate-800">
                        <button type="submit" class="w-full bg-amber-600 hover:bg-amber-700 text-white font-black py-4 rounded-xl transition-all uppercase tracking-wider text-sm shadow-md">
                            Guardar Asignación
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </main>

    <script>
        function editarAsignacion(id_asig, id_user, id_cargo, f_ini, f_fin, version) {
            document.getElementById('titulo_formulario').innerText = "Editar Asignación";
            document.getElementById('form_id_asignacion').value = id_asig;
            document.getElementById('form_id_usuario').value = id_user;
            document.getElementById('form_id_cargo').value = id_cargo;
            document.getElementById('form_fecha_inicio').value = f_ini;
            document.getElementById('form_fecha_fin').value = f_fin;
            document.getElementById('form_version').value = version;
            window.scrollTo({ top: 0, behavior: 'smooth' });
        }

        function limpiarFormulario() {
            document.getElementById('titulo_formulario').innerText = "Nueva Asignación";
            document.getElementById('form_id_asignacion').value = "";
            document.getElementById('form_id_usuario').value = "";
            document.getElementById('form_id_cargo').value = "";
            document.getElementById('form_fecha_inicio').value = "";
            document.getElementById('form_fecha_fin').value = "";
            document.getElementById('form_version').value = "1";
        }

        function toggleHistorico() {
            const filas = document.querySelectorAll('.fila-historica');
            const btn = document.getElementById('btnToggleHistorico');
            let mostrando = false;

            filas.forEach(fila => {
                if (fila.classList.contains('hidden')) {
                    fila.classList.remove('hidden');
                    mostrando = true; 
                } else {
                    fila.classList.add('hidden');
                }
            });

            if (mostrando) {
                btn.innerHTML = '<i class="fa-solid fa-eye-slash mr-2"></i>Ocultar Historial';
                btn.classList.add('text-amber-600', 'dark:text-amber-500', 'bg-slate-200', 'dark:bg-slate-800');
            } else {
                btn.innerHTML = '<i class="fa-solid fa-clock-rotate-left mr-2"></i>Ver Historial de Asignaciones';
                btn.classList.remove('text-amber-600', 'dark:text-amber-500', 'bg-slate-200', 'dark:bg-slate-800');
            }
        }
    </script>
</body>
</html>