Actualizar usuarios.html
This commit is contained in:
@@ -175,7 +175,8 @@
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchMunicipios(provName) {
|
||||
// 1. Cargamos municipios AGRUPADOS por nombre
|
||||
async function fetchMunicipios(provName) {
|
||||
const selMun = document.getElementById('selMunicipio');
|
||||
const loader = document.getElementById('loaderMun');
|
||||
selMun.innerHTML = '<option value="">-- Cargando... --</option>';
|
||||
@@ -183,7 +184,6 @@
|
||||
if(!provName) return;
|
||||
|
||||
if (loader) loader.classList.remove('hidden');
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/api/geo/municipios/${provName}`, {
|
||||
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
|
||||
@@ -191,28 +191,57 @@
|
||||
const data = await res.json();
|
||||
|
||||
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>';
|
||||
data.municipios.forEach(m => {
|
||||
Object.keys(grouped).sort().forEach(mun => {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = m.municipio;
|
||||
opt.dataset.cp = m.codigo_postal;
|
||||
opt.innerText = `${m.municipio.toUpperCase()} (${m.codigo_postal})`;
|
||||
opt.value = mun;
|
||||
// Guardamos el array de códigos en un dataset
|
||||
opt.dataset.cps = JSON.stringify(grouped[mun]);
|
||||
opt.innerText = `${mun.toUpperCase()} (${grouped[mun].length} CPs)`;
|
||||
selMun.appendChild(opt);
|
||||
});
|
||||
selMun.innerHTML += '<option value="OTRO">-- NO ESTÁ EN LA LISTA (MANUAL) --</option>';
|
||||
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 {
|
||||
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();
|
||||
// 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) {
|
||||
const sel = document.getElementById('selMunicipio');
|
||||
const selectedOpt = sel.options[sel.selectedIndex];
|
||||
|
||||
Reference in New Issue
Block a user