Actualizar usuarios.html
This commit is contained in:
@@ -401,34 +401,82 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderUsersTable() {
|
function renderUsersTable() {
|
||||||
const tbody = document.getElementById('usersListBody');
|
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 text-left">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(", ");
|
|
||||||
const groupedZones = (u.zones || []).reduce((acc, curr) => {
|
|
||||||
if (!acc[curr.city]) acc[curr.city] = [];
|
|
||||||
if (!acc[curr.city].includes(curr.cps)) acc[curr.city].push(curr.cps);
|
|
||||||
return acc;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
const uZonesHtml = Object.keys(groupedZones).map(city => {
|
currentUsers.forEach(u => {
|
||||||
return `<div class="mb-1 last:mb-0"><span class="font-black text-blue-600 text-[10px] uppercase">📍 ${city}</span> <span class="text-gray-500 text-[9px]">(${groupedZones[city].join(", ")})</span></div>`;
|
// Determinamos si el usuario está activo basándonos en el campo 'status' del servidor
|
||||||
}).join("") || '<span class="text-[9px] text-gray-300 italic">Sin zona</span>';
|
const isActive = (u.status === 'active' || u.status === null);
|
||||||
|
const uGuildNames = (u.guilds || []).map(gid => availableGuilds.find(ag => ag.id === gid)?.name).filter(Boolean).join(", ");
|
||||||
|
|
||||||
tbody.innerHTML += `
|
const groupedZones = (u.zones || []).reduce((acc, curr) => {
|
||||||
<tr class="bg-white hover:bg-gray-50 transition border-b">
|
if (!acc[curr.city]) acc[curr.city] = [];
|
||||||
<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">${u.email}</span></div></td>
|
if (!acc[curr.city].includes(curr.cps)) acc[curr.city].push(curr.cps);
|
||||||
<td class="p-4 font-black text-green-600">${u.phone}</td>
|
return acc;
|
||||||
<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 text-left"><div class="max-h-24 overflow-y-auto no-scrollbar">${uZonesHtml}</div></td>
|
|
||||||
<td class="p-4 text-right space-x-3">
|
const uZonesHtml = Object.keys(groupedZones).map(city => {
|
||||||
<button onclick="editUser(${u.id})" class="text-blue-500 hover:text-blue-700 font-black text-xs uppercase">Editar</button>
|
return `<div class="mb-1 last:mb-0">
|
||||||
<button onclick="deleteUser(${u.id})" class="text-red-400 hover:text-red-700 font-black text-xs">Baja</button>
|
<span class="font-black ${isActive ? 'text-blue-600' : 'text-gray-400'} text-[10px] uppercase">📍 ${city}</span>
|
||||||
</td>
|
<span class="text-gray-500 text-[9px]">(${groupedZones[city].join(", ")})</span>
|
||||||
</tr>`;
|
</div>`;
|
||||||
});
|
}).join("") || '<span class="text-[9px] text-gray-300 italic">Sin zona</span>';
|
||||||
|
|
||||||
|
// Aplicamos clases CSS de "grisado" si no está activo
|
||||||
|
tbody.innerHTML += `
|
||||||
|
<tr class="${isActive ? 'bg-white' : 'bg-gray-100 opacity-60'} hover:bg-gray-50 transition border-b">
|
||||||
|
<td class="p-4">
|
||||||
|
<div class="flex flex-col">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
${!isActive ? '<i data-lucide="lock" class="w-3 h-3 text-red-500"></i>' : ''}
|
||||||
|
<span class="font-black ${isActive ? 'text-slate-800' : 'text-gray-500'} uppercase">${u.full_name}</span>
|
||||||
|
</div>
|
||||||
|
<span class="text-[9px] text-gray-400 font-bold">${u.email}</span>
|
||||||
|
${!isActive ? '<span class="text-[8px] font-black text-red-500 mt-1 uppercase tracking-tighter">[ Baja Temporal ]</span>' : ''}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td class="p-4 font-black ${isActive ? 'text-green-600' : 'text-gray-400'}">${u.phone}</td>
|
||||||
|
<td class="p-4">
|
||||||
|
<span class="${isActive ? 'bg-slate-100 text-slate-600' : 'bg-gray-200 text-gray-400'} 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 text-left"><div class="max-h-24 overflow-y-auto no-scrollbar">${uZonesHtml}</div></td>
|
||||||
|
<td class="p-4 text-right space-x-2">
|
||||||
|
<button onclick="editUser(${u.id})" class="text-blue-500 hover:text-blue-700 font-black text-[10px] uppercase">Editar</button>
|
||||||
|
|
||||||
|
<button onclick="toggleUserStatus(${u.id}, '${u.status || 'active'}')"
|
||||||
|
class="${isActive ? 'text-amber-500 hover:text-amber-700' : 'text-green-500 hover:text-green-700'} font-black text-[10px] uppercase">
|
||||||
|
${isActive ? 'Baja' : 'Activar'}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button onclick="deleteUser(${u.id})" class="text-red-400 hover:text-red-700 font-black text-[10px] uppercase">Borrar</button>
|
||||||
|
</td>
|
||||||
|
</tr>`;
|
||||||
|
});
|
||||||
|
lucide.createIcons(); // Importante para que se vea el candado
|
||||||
|
}
|
||||||
|
|
||||||
|
// Asegúrate de tener también esta función para que los botones de la tabla funcionen
|
||||||
|
async function toggleUserStatus(id, currentStatus) {
|
||||||
|
const newStatus = currentStatus === 'active' ? 'inactive' : 'active';
|
||||||
|
if(!confirm(`¿Deseas cambiar el estado de este operario?`)) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_URL}/admin/users/${id}/status`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Authorization": `Bearer ${localStorage.getItem("token")}`
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ status: newStatus })
|
||||||
|
});
|
||||||
|
if(await res.json()) {
|
||||||
|
showToast(newStatus === 'active' ? "Operario activado" : "Operario en baja temporal");
|
||||||
|
fetchUsers();
|
||||||
}
|
}
|
||||||
|
} catch(e) { showToast("Error", true); }
|
||||||
|
}
|
||||||
|
|
||||||
function showToast(msg, err=false){
|
function showToast(msg, err=false){
|
||||||
const t=document.getElementById('toast'), m=document.getElementById('toastMsg');
|
const t=document.getElementById('toast'), m=document.getElementById('toastMsg');
|
||||||
|
|||||||
Reference in New Issue
Block a user