Actualizar usuarios.html
This commit is contained in:
188
usuarios.html
188
usuarios.html
@@ -16,17 +16,17 @@
|
||||
<div id="header-container"></div>
|
||||
|
||||
<main class="flex-1 overflow-x-hidden overflow-y-auto bg-gray-50 p-8 fade-in">
|
||||
<h2 class="text-2xl font-bold text-gray-800 mb-8">Gestión de Equipo y Zonas</h2>
|
||||
<h2 class="text-2xl font-bold text-gray-800 mb-8 text-left">Gestión de Equipo y Zonas</h2>
|
||||
|
||||
<div class="flex flex-col gap-8 max-w-5xl mx-auto text-left">
|
||||
|
||||
<div class="w-full bg-white p-6 rounded-xl shadow-sm border border-gray-100">
|
||||
<div class="flex items-center gap-3 mb-4 text-blue-600">
|
||||
<div class="flex items-center gap-3 mb-4 text-blue-600 text-left">
|
||||
<i data-lucide="hammer" class="w-6 h-6"></i>
|
||||
<h3 class="text-lg font-bold text-gray-800">1. Gremios / Especialidades</h3>
|
||||
</div>
|
||||
<div class="flex flex-col md:flex-row gap-6 text-left">
|
||||
<form onsubmit="addGuild(event)" class="flex gap-2 h-10 w-full md:w-1/2">
|
||||
<form onsubmit="addGuild(event)" class="flex gap-2 h-10 w-full md:w-1/2 text-left">
|
||||
<input type="text" id="newGuildName" placeholder="NUEVO GREMIO (EJ. LIMPIEZA)" required class="flex-1 px-3 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none text-sm uppercase">
|
||||
<button type="submit" class="bg-slate-800 text-white px-4 rounded-lg hover:bg-slate-700 font-bold"><i data-lucide="plus" class="w-5 h-5"></i></button>
|
||||
</form>
|
||||
@@ -44,7 +44,7 @@
|
||||
|
||||
<form onsubmit="handleUserSubmit(event)" class="space-y-6 text-left">
|
||||
<input type="hidden" id="editUserId">
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 text-left">
|
||||
<div class="space-y-4">
|
||||
<div>
|
||||
<label class="block text-xs font-black text-gray-400 uppercase mb-1">Nombre Completo</label>
|
||||
@@ -60,7 +60,7 @@
|
||||
<label class="block text-xs font-black text-gray-400 uppercase mb-1">Teléfono</label>
|
||||
<input type="tel" id="uPhone" required placeholder="+34 600..." class="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-green-500 outline-none font-bold text-green-700">
|
||||
</div>
|
||||
<div class="flex gap-4">
|
||||
<div class="flex gap-4 text-left">
|
||||
<div class="w-1/2">
|
||||
<label class="block text-xs font-black text-gray-400 uppercase mb-1">Rol</label>
|
||||
<select id="uRole" class="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-green-500 outline-none bg-white font-bold">
|
||||
@@ -79,41 +79,42 @@
|
||||
|
||||
<div class="pt-2 text-left">
|
||||
<label class="block text-xs font-black text-gray-400 uppercase mb-2">Gremios Asignados</label>
|
||||
<div class="p-4 border rounded-lg bg-gray-50 grid grid-cols-2 md:grid-cols-4 gap-3" id="guildsCheckboxArea"></div>
|
||||
<div class="p-4 border rounded-lg bg-gray-50 grid grid-cols-2 md:grid-cols-4 gap-3 text-left" id="guildsCheckboxArea"></div>
|
||||
</div>
|
||||
|
||||
<div class="pt-2 text-left">
|
||||
<label class="block text-xs font-black text-blue-600 uppercase mb-2 tracking-widest">Cobertura Geográfica (Zonas de Trabajo)</label>
|
||||
<div class="bg-blue-50/50 border-2 border-blue-100 rounded-2xl p-5 space-y-4">
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-3">
|
||||
<label class="block text-xs font-black text-blue-600 uppercase mb-2 tracking-widest text-left">Cobertura Geográfica (Toda España)</label>
|
||||
<div class="bg-blue-50/50 border-2 border-blue-100 rounded-2xl p-5 space-y-4 text-left">
|
||||
<div class="grid grid-cols-1 md:grid-cols-3 gap-3 text-left">
|
||||
<div>
|
||||
<label class="block text-[10px] font-bold text-blue-500 mb-1">Provincia</label>
|
||||
<select id="selProvince" class="w-full text-sm p-2 border rounded-lg outline-none focus:border-blue-500 font-bold">
|
||||
<option value="">Seleccionar...</option>
|
||||
<option value="CADIZ">CADIZ</option>
|
||||
<option value="MALAGA">MALAGA</option>
|
||||
<option value="SEVILLA">SEVILLA</option>
|
||||
<label class="block text-[10px] font-bold text-blue-500 mb-1 uppercase">1. Provincia</label>
|
||||
<select id="selProvince" onchange="fetchMunicipios(this.value)" class="w-full text-sm p-2.5 border rounded-lg outline-none focus:border-blue-500 font-bold bg-white">
|
||||
<option value="">-- Seleccionar --</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-[10px] font-bold text-blue-500 mb-1">Municipio (Ej. Algeciras)</label>
|
||||
<input type="text" id="cityName" placeholder="ALGECIRAS" class="w-full text-sm p-2 border rounded-lg uppercase font-bold outline-none">
|
||||
<div class="relative text-left">
|
||||
<label class="block text-[10px] font-bold text-blue-500 mb-1 uppercase text-left">2. Municipio</label>
|
||||
<select id="selMunicipio" onchange="autoFillCP(this.value)" class="w-full text-sm p-2.5 border rounded-lg outline-none focus:border-blue-500 font-bold bg-white disabled:opacity-50" disabled>
|
||||
<option value="">-- Elige Municipio --</option>
|
||||
</select>
|
||||
<div id="loaderMun" class="hidden absolute right-10 top-9"><i data-lucide="loader-2" class="w-4 h-4 animate-spin text-blue-500"></i></div>
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-[10px] font-bold text-blue-500 mb-1">Códigos Postales (Rango o Indiv.)</label>
|
||||
<div class="flex gap-1">
|
||||
<input type="text" id="cpInput" placeholder="11201-11207" class="flex-1 text-sm p-2 border rounded-lg font-black outline-none">
|
||||
<button type="button" onclick="addZoneToUser()" class="bg-blue-600 text-white px-3 rounded-lg font-bold"><i data-lucide="plus" class="w-4 h-4"></i></button>
|
||||
<div class="text-left">
|
||||
<label class="block text-[10px] font-bold text-blue-500 mb-1 uppercase text-left">3. Código(s) Postal(es)</label>
|
||||
<div class="flex gap-1 text-left">
|
||||
<input type="text" id="cpInput" placeholder="Ej: 11201-11207" class="flex-1 text-sm p-2.5 border rounded-lg font-black outline-none focus:border-blue-500 text-blue-700">
|
||||
<button type="button" onclick="addZoneToUser()" class="bg-blue-600 text-white px-4 rounded-lg font-black hover:bg-blue-700 transition-all shadow-md active:scale-95">
|
||||
<i data-lucide="plus" class="w-5 h-5"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="userZonesArea" class="flex flex-wrap gap-2 min-h-[40px] p-2 bg-white rounded-xl border border-blue-100">
|
||||
</div>
|
||||
<div id="userZonesArea" class="flex flex-wrap gap-2 min-h-[50px] p-3 bg-white rounded-xl border border-blue-100 shadow-inner text-left"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="pt-4 border-t border-gray-100 text-left">
|
||||
<button type="submit" id="btnSubmitUser" class="w-full bg-green-600 hover:bg-green-700 text-white font-black py-4 rounded-xl transition-all shadow-lg shadow-green-500/30 uppercase tracking-widest text-sm">
|
||||
<button type="submit" id="btnSubmitUser" class="w-full bg-green-600 hover:bg-green-700 text-white font-black py-4 rounded-xl transition-all shadow-lg shadow-green-500/30 uppercase tracking-widest text-sm text-left justify-center flex items-center">
|
||||
Guardar Usuario y Zonas
|
||||
</button>
|
||||
</div>
|
||||
@@ -121,13 +122,13 @@
|
||||
</div>
|
||||
|
||||
<div class="w-full bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden text-left">
|
||||
<div class="p-6 border-b border-gray-100 bg-gray-50 flex justify-between items-center">
|
||||
<div class="p-6 border-b border-gray-100 bg-gray-50 text-left">
|
||||
<h3 class="font-bold text-gray-800">Listado de Equipo</h3>
|
||||
</div>
|
||||
<div class="overflow-x-auto no-scrollbar">
|
||||
<div class="overflow-x-auto no-scrollbar text-left">
|
||||
<table class="w-full text-left border-collapse">
|
||||
<thead>
|
||||
<tr class="bg-white text-gray-400 text-[10px] font-black uppercase tracking-[0.2em] border-b">
|
||||
<tr class="bg-white text-gray-400 text-[10px] font-black uppercase tracking-[0.2em] border-b text-left">
|
||||
<th class="p-4">Personal</th>
|
||||
<th class="p-4">Contacto</th>
|
||||
<th class="p-4">Rol / Gremios</th>
|
||||
@@ -135,7 +136,7 @@
|
||||
<th class="p-4 text-right">Acciones</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="usersListBody" class="text-sm divide-y divide-gray-100"></tbody>
|
||||
<tbody id="usersListBody" class="text-sm divide-y divide-gray-100 text-left"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
@@ -151,25 +152,76 @@
|
||||
<script>
|
||||
let availableGuilds = [];
|
||||
let currentUsers = [];
|
||||
let tempUserZones = []; // [{city: 'ALGECIRAS', cps: '11201-11207'}]
|
||||
let tempUserZones = [];
|
||||
|
||||
// Listado de Provincias
|
||||
const provinciasES = ["Álava","Albacete","Alicante","Almería","Asturias","Ávila","Badajoz","Baleares","Barcelona","Burgos","Cáceres","Cádiz","Cantabria","Castellón","Ciudad Real","Córdoba","A Coruña","Cuenca","Gipuzkoa","Girona","Granada","Guadalajara","Huelva","Huesca","Jaén","León","Lleida","Lugo","Madrid","Málaga","Murcia","Navarra","Ourense","Palencia","Las Palmas","Pontevedra","La Rioja","Salamanca","Segovia","Sevilla","Soria","Tarragona","Santa Cruz de Tenerife","Teruel","Toledo","Valencia","Valladolid","Bizkaia","Zamora","Zaragoza","Ceuta","Melilla"];
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
if (!localStorage.getItem("token")) { window.location.href = "index.html"; return; }
|
||||
fetchGuilds();
|
||||
fetchUsers();
|
||||
setupProvinceSelect(); //
|
||||
});
|
||||
|
||||
// --- ZONAS DE TRABAJO ---
|
||||
// --- LÓGICA DE GEOGRAFÍA (API) ---
|
||||
function setupProvinceSelect() {
|
||||
const sel = document.getElementById('selProvince');
|
||||
if(!sel) return;
|
||||
provinciasES.sort().forEach(p => {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = p;
|
||||
opt.innerText = p.toUpperCase();
|
||||
sel.appendChild(opt);
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchMunicipios(provName) {
|
||||
const selMun = document.getElementById('selMunicipio');
|
||||
const loader = document.getElementById('loaderMun');
|
||||
selMun.innerHTML = '<option value="">-- Cargando... --</option>';
|
||||
selMun.disabled = true;
|
||||
if(!provName) return;
|
||||
|
||||
loader.classList.remove('hidden');
|
||||
try {
|
||||
// API Pública de Municipios
|
||||
const res = await fetch(`https://raw.githubusercontent.com/IversoftSolutions/municipios-espana/master/municipios.json`);
|
||||
const allData = await res.json();
|
||||
const filtered = allData.filter(m => m.provincia.toUpperCase() === provName.toUpperCase());
|
||||
|
||||
selMun.innerHTML = '<option value="">-- Selecciona Pueblo --</option>';
|
||||
filtered.sort((a,b) => a.nombre.localeCompare(b.nombre)).forEach(m => {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = m.nombre;
|
||||
opt.dataset.cp = m.cp_base || m.codigo_postal || "";
|
||||
opt.innerText = m.nombre.toUpperCase();
|
||||
selMun.appendChild(opt);
|
||||
});
|
||||
selMun.disabled = false;
|
||||
} catch (e) { showToast("Error cargando municipios", true); }
|
||||
finally { loader.classList.add('hidden'); lucide.createIcons(); }
|
||||
}
|
||||
|
||||
function autoFillCP(munName) {
|
||||
const sel = document.getElementById('selMunicipio');
|
||||
const selectedOpt = sel.options[sel.selectedIndex];
|
||||
if(selectedOpt && selectedOpt.dataset.cp) {
|
||||
document.getElementById('cpInput').value = selectedOpt.dataset.cp;
|
||||
}
|
||||
}
|
||||
|
||||
function addZoneToUser() {
|
||||
const city = document.getElementById('cityName').value.trim().toUpperCase();
|
||||
const cps = document.getElementById('cpInput').value.trim();
|
||||
const province = document.getElementById('selProvince').value;
|
||||
const prov = document.getElementById('selProvince').value;
|
||||
const mun = document.getElementById('selMunicipio').value;
|
||||
const cp = document.getElementById('cpInput').value.trim();
|
||||
if (!prov || !mun || !cp) { showToast("Completa los datos de zona", true); return; }
|
||||
|
||||
if(!city || !cps || !province) { showToast("Faltan datos de zona", true); return; }
|
||||
if (tempUserZones.some(z => z.city === mun && z.cps === cp)) {
|
||||
showToast("Esta zona ya está añadida", true); return;
|
||||
}
|
||||
|
||||
tempUserZones.push({ city, cps, province });
|
||||
document.getElementById('cityName').value = "";
|
||||
document.getElementById('cpInput').value = "";
|
||||
tempUserZones.push({ province: prov, city: mun, cps: cp });
|
||||
renderTempZones();
|
||||
}
|
||||
|
||||
@@ -185,7 +237,7 @@
|
||||
area.innerHTML += `
|
||||
<span class="bg-blue-100 text-blue-700 px-3 py-1.5 rounded-lg text-[10px] font-black border border-blue-200 flex items-center gap-2">
|
||||
${z.city} (${z.cps})
|
||||
<button onclick="removeTempZone(${i})" class="text-blue-400 hover:text-red-500"><i data-lucide="x" class="w-3 h-3"></i></button>
|
||||
<button type="button" onclick="removeTempZone(${i})" class="text-blue-400 hover:text-red-500"><i data-lucide="x" class="w-3 h-3"></i></button>
|
||||
</span>`;
|
||||
});
|
||||
lucide.createIcons();
|
||||
@@ -213,6 +265,14 @@
|
||||
} catch (e) { showToast("Error", true); }
|
||||
}
|
||||
|
||||
async function deleteGuild(id) {
|
||||
if(!confirm("¿Borrar gremio?")) return;
|
||||
try {
|
||||
await fetch(`${API_URL}/guilds/${id}`, { method: 'DELETE', headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||
fetchGuilds(); showToast("Gremio eliminado");
|
||||
} catch(e) { showToast("Error", true); }
|
||||
}
|
||||
|
||||
// --- USUARIOS ---
|
||||
async function fetchUsers() {
|
||||
try {
|
||||
@@ -233,7 +293,6 @@
|
||||
btn.disabled = true; btn.innerText = "PROCESANDO...";
|
||||
|
||||
const selectedGuilds = Array.from(document.querySelectorAll('.guild-checkbox:checked')).map(cb => cb.value);
|
||||
|
||||
const data = {
|
||||
fullName: document.getElementById('uName').value,
|
||||
email: document.getElementById('uEmail').value,
|
||||
@@ -241,7 +300,7 @@
|
||||
password: document.getElementById('uPass').value,
|
||||
role: document.getElementById('uRole').value,
|
||||
guilds: selectedGuilds,
|
||||
zones: tempUserZones // Enviamos las zonas configuradas
|
||||
zones: tempUserZones
|
||||
};
|
||||
|
||||
const url = isEdit ? `${API_URL}/admin/users/${userId}` : `${API_URL}/admin/users`;
|
||||
@@ -256,9 +315,9 @@
|
||||
showToast(isEdit ? "Datos actualizados" : "Usuario creado");
|
||||
resetUserForm();
|
||||
fetchUsers();
|
||||
} else { showToast("❌ " + json.error, true); }
|
||||
} else { showToast("❌ " + (json.error || "Error"), true); }
|
||||
} catch (e) { showToast("Error conexión", true); }
|
||||
finally { btn.disabled = false; btn.innerText = isEdit ? "Actualizar Datos" : "Guardar Usuario"; }
|
||||
finally { btn.disabled = false; btn.innerText = isEdit ? "Actualizar Datos" : "Guardar Usuario y Zonas"; }
|
||||
}
|
||||
|
||||
function editUser(id) {
|
||||
@@ -271,7 +330,7 @@
|
||||
document.getElementById('uRole').value = user.role;
|
||||
document.getElementById('uPass').value = "";
|
||||
|
||||
tempUserZones = user.zones || []; // Cargamos zonas del usuario
|
||||
tempUserZones = user.zones || [];
|
||||
renderTempZones();
|
||||
|
||||
const userGuilds = user.guilds || [];
|
||||
@@ -285,18 +344,28 @@
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
}
|
||||
|
||||
async function deleteUser(id) {
|
||||
if(!confirm("¿Seguro que quieres borrar a este usuario?")) return;
|
||||
try {
|
||||
await fetch(`${API_URL}/admin/users/${id}`, { method: 'DELETE', headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
|
||||
fetchUsers(); showToast("Usuario borrado");
|
||||
} catch(e) { showToast("Error", true); }
|
||||
}
|
||||
|
||||
function resetUserForm() {
|
||||
document.getElementById('editUserId').value = "";
|
||||
document.querySelectorAll('input').forEach(i => i.value = "");
|
||||
document.getElementById('uName').value = "";
|
||||
document.getElementById('uEmail').value = "";
|
||||
document.getElementById('uPhone').value = "";
|
||||
document.getElementById('uPass').value = "";
|
||||
tempUserZones = [];
|
||||
renderTempZones();
|
||||
document.getElementById('formTitle').innerText = "2. Nuevo Usuario";
|
||||
document.getElementById('btnSubmitUser').classList.replace('bg-blue-600', 'bg-green-600');
|
||||
document.getElementById('btnCancelEdit').classList.add('hidden');
|
||||
renderGuildsCheckboxes();
|
||||
document.querySelectorAll('.guild-checkbox').forEach(cb => cb.checked = false);
|
||||
}
|
||||
|
||||
// --- RENDERS ---
|
||||
function renderGuildsList() {
|
||||
const list = document.getElementById('guildsList');
|
||||
list.innerHTML = "";
|
||||
@@ -316,22 +385,19 @@
|
||||
|
||||
function renderUsersTable() {
|
||||
const tbody = document.getElementById('usersListBody');
|
||||
tbody.innerHTML = currentUsers.length === 0 ? `<tr><td colspan="5" class="p-8 text-center text-gray-300 font-bold uppercase tracking-widest">Cargando equipo...</td></tr>` : "";
|
||||
|
||||
tbody.innerHTML = currentUsers.length === 0 ? `<tr><td colspan="5" class="p-8 text-center text-gray-300 font-bold uppercase tracking-widest text-left">Cargando equipo...</td></tr>` : "";
|
||||
currentUsers.forEach(u => {
|
||||
const uGuildNames = (u.guilds || []).map(gid => availableGuilds.find(ag => ag.id === gid)?.name).filter(Boolean).join(", ");
|
||||
// Formatear visualmente las zonas
|
||||
const uZonesHtml = (u.zones || []).map(z => `<span class="block text-[10px] font-bold text-blue-600">📍 ${z.city}: ${z.cps}</span>`).join("");
|
||||
|
||||
const uZonesHtml = (u.zones || []).map(z => `<span class="block text-[10px] font-bold text-blue-600 text-left">📍 ${z.city}: ${z.cps}</span>`).join("");
|
||||
tbody.innerHTML += `
|
||||
<tr class="bg-white hover:bg-gray-50 transition border-b">
|
||||
<td class="p-4"><div class="flex flex-col"><span class="font-black text-slate-800 uppercase">${u.full_name}</span><span class="text-[9px] text-gray-400 font-bold tracking-tighter">${u.email}</span></div></td>
|
||||
<td class="p-4 font-black text-green-600">${u.phone}</td>
|
||||
<td class="p-4"><span class="bg-slate-100 text-slate-600 px-2 py-0.5 rounded text-[10px] font-black uppercase">${u.role}</span><p class="text-[9px] text-gray-400 mt-1 uppercase font-bold">${uGuildNames || '-'}</p></td>
|
||||
<td class="p-4">${uZonesHtml || '<span class="text-[9px] text-gray-300 italic">Sin zona</span>'}</td>
|
||||
<td class="p-4 text-right space-x-3">
|
||||
<button onclick="editUser(${u.id})" class="text-blue-500 hover:text-blue-700 font-black text-xs uppercase tracking-widest">Editar</button>
|
||||
<button onclick="deleteUser(${u.id})" class="text-red-400 hover:text-red-700 font-black text-xs uppercase tracking-widest">Baja</button>
|
||||
<tr class="bg-white hover:bg-gray-50 transition border-b text-left">
|
||||
<td class="p-4"><div class="flex flex-col text-left"><span class="font-black text-slate-800 uppercase text-left">${u.full_name}</span><span class="text-[9px] text-gray-400 font-bold tracking-tighter text-left">${u.email}</span></div></td>
|
||||
<td class="p-4 font-black text-green-600 text-left">${u.phone}</td>
|
||||
<td class="p-4 text-left"><span class="bg-slate-100 text-slate-600 px-2 py-0.5 rounded text-[10px] font-black uppercase text-left">${u.role}</span><p class="text-[9px] text-gray-400 mt-1 uppercase font-bold text-left">${uGuildNames || '-'}</p></td>
|
||||
<td class="p-4 text-left">${uZonesHtml || '<span class="text-[9px] text-gray-300 italic text-left">Sin zona</span>'}</td>
|
||||
<td class="p-4 text-right space-x-3 text-left">
|
||||
<button onclick="editUser(${u.id})" class="text-blue-500 hover:text-blue-700 font-black text-xs uppercase tracking-widest text-left">Editar</button>
|
||||
<button onclick="deleteUser(${u.id})" class="text-red-400 hover:text-red-700 font-black text-xs uppercase tracking-widest text-left">Baja</button>
|
||||
</td>
|
||||
</tr>`;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user