Actualizar usuarios.html

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

View File

@@ -175,43 +175,72 @@
}); });
} }
async function fetchMunicipios(provName) { // 1. Cargamos municipios AGRUPADOS por nombre
const selMun = document.getElementById('selMunicipio'); async function fetchMunicipios(provName) {
const loader = document.getElementById('loaderMun'); const selMun = document.getElementById('selMunicipio');
selMun.innerHTML = '<option value="">-- Cargando... --</option>'; const loader = document.getElementById('loaderMun');
selMun.disabled = true; selMun.innerHTML = '<option value="">-- Cargando... --</option>';
if(!provName) return; selMun.disabled = true;
if(!provName) return;
if (loader) loader.classList.remove('hidden'); if (loader) loader.classList.remove('hidden');
try {
const res = await fetch(`${API_URL}/api/geo/municipios/${provName}`, {
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
});
const data = await res.json();
try { if (data.ok && data.municipios.length > 0) {
const res = await fetch(`${API_URL}/api/geo/municipios/${provName}`, { // Agrupamos códigos por municipio
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } const grouped = data.municipios.reduce((acc, m) => {
}); if(!acc[m.municipio]) acc[m.municipio] = [];
const data = await res.json(); acc[m.municipio].push(m.codigo_postal);
return acc;
}, {});
if (data.ok && data.municipios.length > 0) { selMun.innerHTML = '<option value="">-- Selecciona Pueblo --</option>';
selMun.innerHTML = '<option value="">-- Selecciona Pueblo --</option>'; Object.keys(grouped).sort().forEach(mun => {
data.municipios.forEach(m => { const opt = document.createElement('option');
const opt = document.createElement('option'); opt.value = mun;
opt.value = m.municipio; // Guardamos el array de códigos en un dataset
opt.dataset.cp = m.codigo_postal; opt.dataset.cps = JSON.stringify(grouped[mun]);
opt.innerText = `${m.municipio.toUpperCase()} (${m.codigo_postal})`; 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;
} else {
selMun.innerHTML = '<option value="OTRO">-- ESCRIBIR MANUALMENTE --</option>';
selMun.disabled = false;
}
} catch (e) {
console.error("Error de conexión con la DB geográfica");
} finally {
if (loader) loader.classList.add('hidden');
lucide.createIcons();
}
} }
} 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 {
// Extraemos la lista de CPs del pueblo seleccionado
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 });
}
});
showToast(`✅ Añadidos ${cpList.length} códigos de ${munName}`);
}
renderTempZones();
document.getElementById('cpInput').value = "";
}
function autoFillCP(munName) { function autoFillCP(munName) {
const sel = document.getElementById('selMunicipio'); const sel = document.getElementById('selMunicipio');