Actualizar usuarios.html

This commit is contained in:
2026-02-15 17:17:55 +00:00
parent 7b44ee8767
commit 2cd47ddc75

View File

@@ -175,6 +175,7 @@
}); });
} }
// 1. Cargamos municipios AGRUPADOS por nombre
async function fetchMunicipios(provName) { async function fetchMunicipios(provName) {
const selMun = document.getElementById('selMunicipio'); const selMun = document.getElementById('selMunicipio');
const loader = document.getElementById('loaderMun'); const loader = document.getElementById('loaderMun');
@@ -183,7 +184,6 @@
if(!provName) return; if(!provName) return;
if (loader) loader.classList.remove('hidden'); if (loader) loader.classList.remove('hidden');
try { try {
const res = await fetch(`${API_URL}/api/geo/municipios/${provName}`, { const res = await fetch(`${API_URL}/api/geo/municipios/${provName}`, {
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
@@ -191,26 +191,55 @@
const data = await res.json(); const data = await res.json();
if (data.ok && data.municipios.length > 0) { if (data.ok && data.municipios.length > 0) {
// Agrupamos códigos por municipio
const grouped = data.municipios.reduce((acc, m) => {
if(!acc[m.municipio]) acc[m.municipio] = [];
acc[m.municipio].push(m.codigo_postal);
return acc;
}, {});
selMun.innerHTML = '<option value="">-- Selecciona Pueblo --</option>'; selMun.innerHTML = '<option value="">-- Selecciona Pueblo --</option>';
data.municipios.forEach(m => { Object.keys(grouped).sort().forEach(mun => {
const opt = document.createElement('option'); const opt = document.createElement('option');
opt.value = m.municipio; opt.value = mun;
opt.dataset.cp = m.codigo_postal; // Guardamos el array de códigos en un dataset
opt.innerText = `${m.municipio.toUpperCase()} (${m.codigo_postal})`; opt.dataset.cps = JSON.stringify(grouped[mun]);
opt.innerText = `${mun.toUpperCase()} (${grouped[mun].length} CPs)`;
selMun.appendChild(opt); selMun.appendChild(opt);
}); });
selMun.innerHTML += '<option value="OTRO">-- NO ESTÁ EN LA LISTA (MANUAL) --</option>'; selMun.innerHTML += '<option value="OTRO">-- NO ESTÁ EN LA LISTA (MANUAL) --</option>';
selMun.disabled = false; selMun.disabled = false;
}
} catch (e) { console.error(e); }
finally { if (loader) loader.classList.add('hidden'); }
}
// 2. Añadimos todos los códigos del municipio de una vez
function addZoneToUser() {
const prov = document.getElementById('selProvince').value;
const selMun = document.getElementById('selMunicipio');
const munName = selMun.value;
if (!prov || !munName) { showToast("Selecciona Provincia y Municipio", true); return; }
if (munName === "OTRO") {
const manualCP = document.getElementById('cpInput').value.trim();
if(!manualCP) { showToast("Escribe el CP manual", true); return; }
tempUserZones.push({ province: prov, city: "MANUAL", cps: manualCP });
} else { } else {
selMun.innerHTML = '<option value="OTRO">-- ESCRIBIR MANUALMENTE --</option>'; // Extraemos la lista de CPs del pueblo seleccionado
selMun.disabled = false; const cpList = JSON.parse(selMun.options[selMun.selectedIndex].dataset.cps);
cpList.forEach(cp => {
// Evitamos duplicados antes de añadir
if (!tempUserZones.some(z => z.city === munName && z.cps === cp)) {
tempUserZones.push({ province: prov, city: munName, cps: cp });
} }
} catch (e) { });
console.error("Error de conexión con la DB geográfica"); showToast(`✅ Añadidos ${cpList.length} códigos de ${munName}`);
} finally {
if (loader) loader.classList.add('hidden');
lucide.createIcons();
} }
renderTempZones();
document.getElementById('cpInput').value = "";
} }
function autoFillCP(munName) { function autoFillCP(munName) {