<!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 - Cargos Estatutarios</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">
        <div class="flex items-center space-x-3">
            <span class="font-black text-purple-500">UTRAU <span class="text-slate-400 font-bold">| CARGOS</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">
        
        <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">
            <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">Definición Estatutaria</h2>
                    <p class="text-xs text-slate-500 font-bold mt-1">Estructura organizacional base.</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">
                    + Nuevo 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 w-16 text-center">Nº</th>
                            <th class="p-4 font-black">Nombre del Cargo</th>
                            <th class="p-4 font-black text-center">Poder de Decisión</th>
                            <th class="p-4 font-black text-right">Acción</th>
                        </tr>
                    </thead>
                    <tbody class="text-sm font-medium">
                        {% for cargo in lista_cargos %}
                        <tr class="border-b border-slate-100 dark:border-slate-800/50 hover:bg-slate-50 dark:hover:bg-slate-800/30 transition">
                            <td class="p-4 text-center font-black text-slate-400">{{ cargo.orden }}</td>
                            <td class="p-4 font-bold text-slate-800 dark:text-slate-200">{{ cargo.nombre_cargo }}</td>
                            <td class="p-4 text-center">
                                {% if cargo.es_ejecutivo %}
                                    <span class="px-3 py-1 rounded-full bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-400 text-[10px] font-black uppercase tracking-wider border border-purple-200 dark:border-purple-800">Mesa Ejecutiva</span>
                                {% else %}
                                    <span class="px-3 py-1 rounded-full bg-slate-100 text-slate-600 dark:bg-slate-800 dark:text-slate-400 text-[10px] font-black uppercase tracking-wider border border-slate-200 dark:border-slate-700">Directiva</span>
                                {% endif %}
                            </td>
                            <td class="p-4 text-right">
                                <button onclick="editarCargo('{{ cargo.id_cargo }}', '{{ cargo.nombre_cargo }}', '{{ cargo.es_ejecutivo }}', '{{ cargo.orden }}', '{{ cargo.version }}')" class="p-2 bg-slate-200 dark:bg-slate-700 hover:bg-blue-500 hover:text-white dark:hover:bg-blue-500 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>

        <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">Nuevo Cargo</h2>
            </div>
            <div class="p-6">
                <form action="{{ url_for('config.guardar_cargo') }}" method="POST" class="space-y-4">
                    
                    <input type="hidden" name="id_cargo" id="form_id_cargo" 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 tracking-wide">Nombre del Cargo</label>
                        <input type="text" name="nombre_cargo" id="form_nombre_cargo" required 
                            class="w-full px-4 py-3 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-purple-500 outline-none font-bold transition">
                    </div>
                    
                    <div>
                        <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase tracking-wide">Orden de Aparición (Número)</label>
                        <input type="number" name="orden" id="form_orden" value="0" required 
                            class="w-full px-4 py-3 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-purple-500 outline-none font-bold transition">
                    </div>

                    <div class="flex items-center space-x-3 pt-2">
                        <input type="checkbox" name="es_ejecutivo" id="form_es_ejecutivo" class="w-5 h-5 rounded accent-purple-500">
                        <label for="form_es_ejecutivo" class="text-sm font-bold text-slate-700 dark:text-slate-300 cursor-pointer">Pertenece a Mesa Ejecutiva</label>
                    </div>

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

    <script>
        function editarCargo(id, nombre, es_ejecutivo, orden, version) {
            document.getElementById('titulo_formulario').innerText = "Editar Cargo";
            document.getElementById('form_id_cargo').value = id;
            document.getElementById('form_nombre_cargo').value = nombre;
            document.getElementById('form_orden').value = orden;
            document.getElementById('form_version').value = version;
            document.getElementById('form_es_ejecutivo').checked = (es_ejecutivo === 'True' || es_ejecutivo === '1');
            window.scrollTo({ top: 0, behavior: 'smooth' }); 
        }

        function limpiarFormulario() {
            document.getElementById('titulo_formulario').innerText = "Nuevo Cargo";
            document.getElementById('form_id_cargo').value = "";
            document.getElementById('form_nombre_cargo').value = "";
            document.getElementById('form_orden').value = "0";
            document.getElementById('form_version').value = "1";
            document.getElementById('form_es_ejecutivo').checked = false;
        }
    </script>
</body>
</html>