<!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 - Legajo de Compañeros</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-emerald-500">UTRAU <span class="text-slate-400 font-bold">| LEGAJOS</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</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 xl:grid-cols-5 gap-6 items-start">
        
        <div id="seccion_lista" class="xl:col-span-2 bg-white dark:bg-slate-900 rounded-2xl border border-slate-200 dark:border-slate-800 shadow-sm overflow-hidden h-[50vh] xl:h-[85vh] flex flex-col scroll-mt-20">
            <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">Padrón</h2>
                    <p class="text-xs text-slate-500 font-bold mt-1">Directorio UTRAU.</p>
                </div>
                <button onclick="limpiarFormulario()" class="px-3 py-2 bg-blue-600 hover:bg-blue-700 text-white text-[10px] font-black uppercase tracking-wider rounded-lg transition shadow-md">
                    + Nuevo
                </button>
            </div>
            
            <div class="overflow-y-auto flex-1">
                <table class="w-full text-left border-collapse">
                    <tbody class="text-sm font-medium">
                        {% for u in lista_usuarios %}
                        <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">
                                <div class="flex items-center space-x-3">
                                    {% if u.path_foto %}
                                        <img src="{{ u.path_foto }}" class="w-12 h-12 rounded-full object-cover border-2 border-slate-200 dark:border-slate-700 shadow-sm">
                                    {% else %}
                                        <div class="w-12 h-12 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">
                                            {{ u.nombre_completo[0:1] }}
                                        </div>
                                    {% endif %}
                                    <div>
                                        <p class="font-black text-slate-800 dark:text-slate-200 text-sm leading-tight">{{ u.nombre_completo }}</p>
                                        <p class="text-[10px] text-slate-500 font-bold uppercase tracking-wide">
                                            @{{ u.alias }} | 
                                            {% if u.estado == 'ACTIVO' %}<span class="text-emerald-500">ACTIVO</span>{% else %}<span class="text-red-500">BAJA</span>{% endif %}
                                        </p>
                                        
                                        <div class="flex space-x-1 mt-1.5">
                                            {% if u.path_foto %}
                                                <a href="{{ u.path_foto }}" target="_blank" title="Ver Foto"><img src="{{ u.path_foto }}" class="w-6 h-6 rounded bg-white object-cover border border-slate-300 dark:border-slate-600 hover:scale-125 transition-transform shadow-sm"></a>
                                            {% else %}
                                                <div class="w-6 h-6 rounded bg-red-50 dark:bg-red-900/20 border border-dashed border-red-300 dark:border-red-800/50 flex items-center justify-center text-red-400" title="Falta Foto"><i class="fa-solid fa-camera text-[9px]"></i></div>
                                            {% endif %}
                                            
                                            {% if u.path_firma %}
                                                <a href="{{ u.path_firma }}" target="_blank" title="Ver Firma"><img src="{{ u.path_firma }}" class="w-6 h-6 rounded bg-white object-contain border border-slate-300 dark:border-slate-600 hover:scale-125 transition-transform shadow-sm p-0.5"></a>
                                            {% else %}
                                                <div class="w-6 h-6 rounded bg-red-50 dark:bg-red-900/20 border border-dashed border-red-300 dark:border-red-800/50 flex items-center justify-center text-red-400" title="Falta Firma"><i class="fa-solid fa-signature text-[9px]"></i></div>
                                            {% endif %}
                                            
                                            {% if u.path_cedula_frente %}
                                                <a href="{{ u.path_cedula_frente }}" target="_blank" title="Ver CI Frente"><img src="{{ u.path_cedula_frente }}" class="w-6 h-6 rounded bg-white object-cover border border-slate-300 dark:border-slate-600 hover:scale-125 transition-transform shadow-sm"></a>
                                            {% else %}
                                                <div class="w-6 h-6 rounded bg-red-50 dark:bg-red-900/20 border border-dashed border-red-300 dark:border-red-800/50 flex items-center justify-center text-red-400" title="Falta C.I. Frente"><i class="fa-solid fa-id-card text-[9px]"></i></div>
                                            {% endif %}
                                            
                                            {% if u.path_cedula_atras %}
                                                <a href="{{ u.path_cedula_atras }}" target="_blank" title="Ver CI Atrás"><img src="{{ u.path_cedula_atras }}" class="w-6 h-6 rounded bg-white object-cover border border-slate-300 dark:border-slate-600 hover:scale-125 transition-transform shadow-sm"></a>
                                            {% else %}
                                                <div class="w-6 h-6 rounded bg-red-50 dark:bg-red-900/20 border border-dashed border-red-300 dark:border-red-800/50 flex items-center justify-center text-red-400" title="Falta C.I. Atrás"><i class="fa-solid fa-id-card text-[9px]"></i></div>
                                            {% endif %}
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td class="p-4 text-right">
                                <button onclick="editarLegajo(
                                    '{{ u.id_usuario }}', '{{ u.alias }}', '{{ u.nombre_completo }}', '{{ u.rol }}', '{{ u.estado }}', '{{ u.version }}',
                                    '{{ u.tipo_documento }}', '{{ u.cedula_identidad or '' }}', '{{ u.fecha_nacimiento or '' }}',
                                    '{{ u.whatsapp or '' }}', '{{ u.celular or '' }}', '{{ u.email or '' }}',
                                    '{{ u.fecha_ingreso or '' }}', '{{ u.id_sector or '' }}', '{{ u.id_empresa or '' }}', '{{ u.tipo_contrato }}'
                                )" class="p-2.5 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 Legajo">
                                    <i class="fa-solid fa-pen"></i>
                                </button>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>

        <div id="seccion_formulario" class="xl:col-span-3 bg-white dark:bg-slate-900 rounded-2xl border border-slate-200 dark:border-slate-800 shadow-sm h-fit mb-10 scroll-mt-20">
            <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">
                <h2 class="text-lg font-black uppercase tracking-tight" id="titulo_formulario">Alta de Legajo Digital</h2>
                <button type="button" onclick="document.getElementById('seccion_lista').scrollIntoView({ behavior: 'smooth', block: 'start' });" class="xl:hidden text-xs font-bold text-slate-500 uppercase flex items-center hover:text-blue-500 transition">
                    <i class="fa-solid fa-arrow-up mr-1"></i> Lista
                </button>
            </div>
            
            <div class="p-6">
                <form action="{{ url_for('config.guardar_usuario') }}" method="POST" enctype="multipart/form-data" class="space-y-6">
                    
                    <input type="hidden" name="id_usuario" id="form_id_usuario" value="">
                    <input type="hidden" name="version" id="form_version" value="1">
                    
                    <div>
                        <h3 class="text-xs font-black uppercase text-slate-400 border-b border-slate-200 dark:border-slate-800 pb-2 mb-4"><i class="fa-solid fa-fingerprint mr-2"></i>Acceso e Identidad</h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Alias (Usuario)*</label>
                                <input type="text" name="alias" id="form_alias" required class="w-full px-4 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                            </div>
                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">PIN de Acceso / Firma*</label>
                                <input type="password" name="pin_firma" id="form_pin_firma" placeholder="Dejar blanco para no cambiar" class="w-full px-4 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                            </div>
                            <div class="md:col-span-2">
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Nombre Completo*</label>
                                <input type="text" name="nombre_completo" id="form_nombre_completo" required class="w-full px-4 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                            </div>
                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Documento</label>
                                <div class="flex space-x-2">
                                    <select name="tipo_documento" id="form_tipo_documento" onchange="toggleDocumento()" class="w-1/3 px-2 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                                        <option value="CI" class="text-black">C.I.</option>
                                        <option value="PASAPORTE" class="text-black">Pasaporte</option>
                                        <option value="OTROS" class="text-black">Otro</option>
                                    </select>
                                    <input type="text" name="cedula_identidad" id="form_cedula_identidad" placeholder="Sin puntos ni guiones" class="w-2/3 px-4 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                                </div>
                            </div>
                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Fecha Nacimiento</label>
                                <input type="date" name="fecha_nacimiento" id="form_fecha_nacimiento" class="w-full px-4 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                            </div>
                        </div>
                    </div>

                    <div>
                        <h3 class="text-xs font-black uppercase text-slate-400 border-b border-slate-200 dark:border-slate-800 pb-2 mb-4"><i class="fa-solid fa-briefcase mr-2"></i>Contacto y Laboral</h3>
                        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Celular / WhatsApp</label>
                                <input type="text" name="whatsapp" id="form_whatsapp" class="w-full px-4 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                            </div>
                            <div class="md:col-span-2">
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Email</label>
                                <input type="email" name="email" id="form_email" class="w-full px-4 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                            </div>

                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Sector (Rama)</label>
                                <div class="flex space-x-2">
                                    <select name="id_sector" id="form_id_sector" class="flex-1 px-3 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                                        <option value="" class="text-black">-- Seleccione --</option>
                                        {% for s in lista_sectores %}
                                        <option value="{{ s.id_sector }}" class="text-black">{{ s.codigo }} - {{ s.nombre }}</option>
                                        {% endfor %}
                                    </select>
                                    <button type="button" onclick="document.getElementById('modalSector').classList.remove('hidden')" class="bg-blue-600 hover:bg-blue-700 text-white px-3 py-2 rounded-xl font-black transition"><i class="fa-solid fa-plus"></i></button>
                                </div>
                            </div>
                            
                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Empresa / Organismo</label>
                                <div class="flex space-x-2">
                                    <select name="id_empresa" id="form_id_empresa" class="flex-1 px-3 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                                        <option value="" class="text-black">-- Seleccione --</option>
                                        {% for e in lista_empresas %}
                                        <option value="{{ e.id_empresa }}" class="text-black">{{ e.nombre_fantasia }}</option>
                                        {% endfor %}
                                    </select>
                                </div>
                            </div>
                            
                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Contrato</label>
                                <select name="tipo_contrato" id="form_tipo_contrato" class="w-full px-4 py-2 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-transparent focus:border-emerald-500 outline-none font-bold text-sm">
                                    <option value="MENSUAL" class="text-black">Mensual</option>
                                    <option value="JORNALERO" class="text-black">Jornalero</option>
                                    <option value="ZAFRAL" class="text-black">Zafral</option>
                                    <option value="OTROS" class="text-black">Otros</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div>
                        <h3 class="text-xs font-black uppercase text-slate-400 border-b border-slate-200 dark:border-slate-800 pb-2 mb-4"><i class="fa-solid fa-camera mr-2"></i>Digitalización (Opcional)</h3>
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div class="p-3 border-2 border-dashed border-slate-300 dark:border-slate-700 rounded-xl bg-slate-50 dark:bg-slate-800/50">
                                <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase text-center"><i class="fa-solid fa-user mr-1"></i>Foto de Perfil</label>
                                <input type="file" name="foto" accept="image/*" class="w-full text-xs text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-xs file:font-black file:bg-blue-100 file:text-blue-700 hover:file:bg-blue-200 dark:file:bg-blue-900/50 dark:file:text-blue-400">
                            </div>
                            <div class="p-3 border-2 border-dashed border-slate-300 dark:border-slate-700 rounded-xl bg-slate-50 dark:bg-slate-800/50">
                                <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase text-center"><i class="fa-solid fa-signature mr-1"></i>Foto Firma</label>
                                <input type="file" name="firma" accept="image/*" class="w-full text-xs text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-xs file:font-black file:bg-emerald-100 file:text-emerald-700 hover:file:bg-emerald-200 dark:file:bg-emerald-900/50 dark:file:text-emerald-400">
                            </div>
                            <div class="p-3 border-2 border-dashed border-slate-300 dark:border-slate-700 rounded-xl bg-slate-50 dark:bg-slate-800/50">
                                <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase text-center"><i class="fa-solid fa-id-card mr-1"></i>C.I. Frente</label>
                                <input type="file" name="cedula_frente" accept="image/*" class="w-full text-xs text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-xs file:font-black file:bg-slate-200 file:text-slate-700 hover:file:bg-slate-300 dark:file:bg-slate-700 dark:file:text-slate-300">
                            </div>
                            <div class="p-3 border-2 border-dashed border-slate-300 dark:border-slate-700 rounded-xl bg-slate-50 dark:bg-slate-800/50">
                                <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase text-center"><i class="fa-solid fa-id-card mr-1"></i>C.I. Atrás</label>
                                <input type="file" name="cedula_atras" accept="image/*" class="w-full text-xs text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-xs file:font-black file:bg-slate-200 file:text-slate-700 hover:file:bg-slate-300 dark:file:bg-slate-700 dark:file:text-slate-300">
                            </div>
                        </div>
                    </div>

                    <div class="pt-4 border-t border-slate-100 dark:border-slate-800 flex justify-between items-center space-x-4">
                        <div class="flex-1 grid grid-cols-2 gap-4">
                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Rol Actual (Lectura)</label>
                                <select id="form_rol" disabled class="w-full px-4 py-3 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-slate-200 dark:bg-slate-800/50 text-slate-500 font-black text-sm opacity-70 cursor-not-allowed">
                                    <option value="1">1 - Afiliado Base</option>
                                    <option value="2">2 - Directiva</option>
                                    <option value="3">3 - Ejecutivo</option>
                                </select>
                                <p class="text-[9px] font-bold text-slate-400 mt-1">El rol se actualiza desde Asignaciones.</p>
                            </div>
                            <div>
                                <label class="block text-[10px] font-black text-slate-500 mb-1.5 uppercase">Estado en Padrón</label>
                                <select name="estado" id="form_estado" class="w-full px-4 py-3 rounded-xl border-2 border-slate-200 dark:border-slate-800 bg-slate-100 dark:bg-slate-900 focus:border-emerald-500 font-black text-sm text-emerald-600">
                                    <option value="ACTIVO" class="text-black">ACTIVO</option>
                                    <option value="INACTIVO" class="text-red-600">INACTIVO (Baja)</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    
                    <button type="submit" class="w-full bg-emerald-600 hover:bg-emerald-700 text-white font-black py-4 rounded-xl transition-all uppercase tracking-wider text-sm shadow-md mt-4">
                        <i class="fa-solid fa-save mr-2"></i>Guardar Legajo
                    </button>
                    
                </form>
            </div>
        </div>
    </main>

    <div id="modalSector" class="fixed inset-0 bg-black/60 z-50 hidden flex items-center justify-center backdrop-blur-sm">
        <div class="bg-white dark:bg-slate-900 p-6 rounded-2xl shadow-xl w-full max-w-sm border border-slate-200 dark:border-slate-800">
            <h3 class="text-lg font-black uppercase mb-4">Nuevo Sector</h3>
            <div class="space-y-4">
                <div>
                    <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase">Código (Ej: G23-01)</label>
                    <input type="text" id="nuevo_sector_codigo" class="w-full px-3 py-2 border-2 rounded-xl dark:bg-slate-800 dark:border-slate-700 outline-none focus:border-blue-500 font-bold">
                </div>
                <div>
                    <label class="block text-[10px] font-black text-slate-500 mb-1 uppercase">Descripción</label>
                    <input type="text" id="nuevo_sector_nombre" class="w-full px-3 py-2 border-2 rounded-xl dark:bg-slate-800 dark:border-slate-700 outline-none focus:border-blue-500 font-bold">
                </div>
                <div class="flex space-x-2 pt-2">
                    <button type="button" onclick="document.getElementById('modalSector').classList.add('hidden')" class="flex-1 bg-slate-200 dark:bg-slate-800 text-slate-700 dark:text-slate-300 font-black py-2 rounded-xl">Cancelar</button>
                    <button type="button" onclick="guardarSector()" class="flex-1 bg-blue-600 hover:bg-blue-700 text-white font-black py-2 rounded-xl">Guardar</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        function toggleDocumento() {
            const tipo = document.getElementById('form_tipo_documento').value;
            const input = document.getElementById('form_cedula_identidad');
            if (tipo === 'CI') {
                input.type = "number";
                input.placeholder = "Sin puntos ni guiones";
            } else {
                input.type = "text";
                input.placeholder = "Número de Documento";
            }
        }

        function editarLegajo(id, alias, nombre, rol, estado, version, tipo_doc, ci, fnac, wp, cel, email, fingreso, id_sector, id_empresa, contrato) {
            document.getElementById('titulo_formulario').innerText = "Editando Legajo: " + nombre;
            document.getElementById('form_id_usuario').value = id;
            document.getElementById('form_version').value = version;
            document.getElementById('form_alias').value = alias;
            document.getElementById('form_nombre_completo').value = nombre;
            document.getElementById('form_pin_firma').value = ""; 
            document.getElementById('form_pin_firma').required = false;
            document.getElementById('form_tipo_documento').value = tipo_doc || 'CI';
            toggleDocumento();
            document.getElementById('form_cedula_identidad').value = ci;
            document.getElementById('form_fecha_nacimiento').value = fnac;
            document.getElementById('form_whatsapp').value = wp;
            document.getElementById('form_email').value = email;
            document.getElementById('form_id_sector').value = id_sector;
            document.getElementById('form_id_empresa').value = id_empresa;
            document.getElementById('form_tipo_contrato').value = contrato || 'MENSUAL';
            document.getElementById('form_rol').value = rol;
            document.getElementById('form_estado').value = estado;
            
            document.getElementById('seccion_formulario').scrollIntoView({ behavior: 'smooth', block: 'start' });
        }

        function limpiarFormulario() {
            document.getElementById('titulo_formulario').innerText = "Alta de Legajo Digital";
            document.getElementById('form_id_usuario').value = "";
            document.getElementById('form_version').value = "1";
            document.getElementById('form_alias').value = "";
            document.getElementById('form_nombre_completo').value = "";
            document.getElementById('form_pin_firma').value = "";
            document.getElementById('form_pin_firma').required = true;
            document.getElementById('form_tipo_documento').value = 'CI';
            toggleDocumento();
            document.getElementById('form_cedula_identidad').value = "";
            document.getElementById('form_fecha_nacimiento').value = "";
            document.getElementById('form_whatsapp').value = "";
            document.getElementById('form_email').value = "";
            document.getElementById('form_id_sector').value = "";
            document.getElementById('form_id_empresa').value = "";
            document.getElementById('form_tipo_contrato').value = "MENSUAL";
            document.getElementById('form_rol').value = "1";
            document.getElementById('form_estado').value = "ACTIVO";

            document.getElementById('seccion_formulario').scrollIntoView({ behavior: 'smooth', block: 'start' });
        }

        async function guardarSector() {
            const cod = document.getElementById('nuevo_sector_codigo').value;
            const nom = document.getElementById('nuevo_sector_nombre').value;
            if(!cod || !nom) return alert("Completá ambos campos");

            let formData = new FormData();
            formData.append('codigo', cod);
            formData.append('nombre', nom);

            let req = await fetch("{{ url_for('config.ajax_add_sector') }}", { method: 'POST', body: formData });
            let res = await req.json();

            if(res.success) {
                let select = document.getElementById('form_id_sector');
                select.add(new Option(res.codigo + ' - ' + res.nombre, res.id_sector));
                select.value = res.id_sector;
                document.getElementById('modalSector').classList.add('hidden');
                document.getElementById('nuevo_sector_codigo').value = '';
                document.getElementById('nuevo_sector_nombre').value = '';
            } else {
                alert("Error: " + res.error);
            }
        }
    </script>
</body>
</html>