Actualizar configuracion.html
This commit is contained in:
@@ -6,293 +6,44 @@
|
|||||||
<title>Configuración - IntegraRepara</title>
|
<title>Configuración - IntegraRepara</title>
|
||||||
<script src="https://cdn.tailwindcss.com"></script>
|
<script src="https://cdn.tailwindcss.com"></script>
|
||||||
<script src="https://unpkg.com/lucide@latest"></script>
|
<script src="https://unpkg.com/lucide@latest"></script>
|
||||||
<style>
|
<style>.fade-in { animation: fadeIn 0.5s ease-in-out; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }</style>
|
||||||
.fade-in { animation: fadeIn 0.5s ease-in-out; }
|
|
||||||
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
<body class="bg-gray-50 text-gray-800 font-sans antialiased">
|
<body class="bg-gray-50 text-gray-800 font-sans antialiased">
|
||||||
|
|
||||||
<div class="flex h-screen overflow-hidden">
|
<div class="flex h-screen overflow-hidden">
|
||||||
|
|
||||||
<aside class="w-64 bg-slate-900 text-white flex flex-col hidden md:flex shadow-xl z-20">
|
<div id="sidebar-container" class="h-full"></div>
|
||||||
<div class="h-16 flex items-center justify-center border-b border-slate-800">
|
|
||||||
<h1 class="text-xl font-bold tracking-wider text-blue-400">INTEGRA<span class="text-white">REPARA</span></h1>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<nav class="flex-1 px-4 py-6 space-y-2">
|
|
||||||
<a href="panel.html" class="flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white hover:bg-slate-800 rounded-lg transition-all">
|
|
||||||
<i data-lucide="layout-dashboard" class="w-5 h-5"></i>
|
|
||||||
<span>Dashboard</span>
|
|
||||||
</a>
|
|
||||||
<a href="#" class="flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white hover:bg-slate-800 rounded-lg transition-all">
|
|
||||||
<i data-lucide="calendar" class="w-5 h-5"></i>
|
|
||||||
<span>Calendario</span>
|
|
||||||
</a>
|
|
||||||
<a href="#" class="flex items-center gap-3 px-4 py-3 text-slate-400 hover:text-white hover:bg-slate-800 rounded-lg transition-all">
|
|
||||||
<i data-lucide="users" class="w-5 h-5"></i>
|
|
||||||
<span>Clientes</span>
|
|
||||||
</a>
|
|
||||||
<a href="configuracion.html" class="flex items-center gap-3 px-4 py-3 bg-blue-600 rounded-lg text-white transition-all shadow-lg shadow-blue-500/30">
|
|
||||||
<i data-lucide="settings" class="w-5 h-5"></i>
|
|
||||||
<span>Configuración</span>
|
|
||||||
</a>
|
|
||||||
</nav>
|
|
||||||
|
|
||||||
<div class="p-4 border-t border-slate-800">
|
|
||||||
<button id="btnLogout" class="flex items-center gap-2 text-slate-400 hover:text-red-400 transition-colors w-full p-2 rounded hover:bg-slate-800">
|
|
||||||
<i data-lucide="log-out" class="w-5 h-5"></i>
|
|
||||||
<span>Cerrar Sesión</span>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</aside>
|
|
||||||
|
|
||||||
<div class="flex-1 flex flex-col overflow-hidden relative">
|
<div class="flex-1 flex flex-col overflow-hidden relative">
|
||||||
|
|
||||||
<header class="h-16 bg-white border-b border-gray-200 flex items-center justify-between px-8 shadow-sm">
|
<div id="header-container"></div>
|
||||||
<h2 class="text-xl font-semibold text-gray-700">Configuración del Sistema</h2>
|
|
||||||
<div class="flex items-center gap-3">
|
|
||||||
<div class="text-right hidden sm:block">
|
|
||||||
<p class="text-sm font-semibold text-gray-700" id="userName">Admin</p>
|
|
||||||
<p class="text-xs text-gray-500" id="userRole">Administrador</p>
|
|
||||||
</div>
|
|
||||||
<div class="w-10 h-10 bg-slate-800 rounded-full flex items-center justify-center text-white font-bold shadow-lg">A</div>
|
|
||||||
</div>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<main class="flex-1 overflow-x-hidden overflow-y-auto bg-gray-50 p-8 fade-in">
|
<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">Configuración del Sistema</h2>
|
||||||
|
|
||||||
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
||||||
|
<div class="bg-white p-6 rounded-xl shadow-sm">
|
||||||
<div class="lg:col-span-1 space-y-6">
|
<h3 class="font-bold mb-4">Gremios</h3>
|
||||||
<div class="bg-white p-6 rounded-xl shadow-sm border border-gray-100">
|
<div id="guildsList">Cargando...</div>
|
||||||
<div class="flex items-center gap-3 mb-4 text-blue-600">
|
|
||||||
<i data-lucide="hammer" class="w-6 h-6"></i>
|
|
||||||
<h3 class="text-lg font-bold text-gray-800">Gremios</h3>
|
|
||||||
</div>
|
</div>
|
||||||
<p class="text-sm text-gray-500 mb-4">Añade especialidades (Fontanería, Electricidad...)</p>
|
|
||||||
|
|
||||||
<form onsubmit="addGuild(event)" class="flex gap-2 mb-6">
|
|
||||||
<input type="text" id="newGuildName" placeholder="Nombre (ej. Pintura)" 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 p-2 rounded-lg hover:bg-slate-700">
|
|
||||||
<i data-lucide="plus" class="w-5 h-5"></i>
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<div class="max-h-64 overflow-y-auto space-y-2" id="guildsList">
|
|
||||||
<p class="text-xs text-center text-gray-400">Cargando gremios...</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="lg:col-span-2 space-y-6">
|
|
||||||
<div class="bg-white p-6 rounded-xl shadow-sm border border-gray-100">
|
|
||||||
<div class="flex items-center gap-3 mb-6 text-green-600">
|
|
||||||
<i data-lucide="user-plus" class="w-6 h-6"></i>
|
|
||||||
<h3 class="text-lg font-bold text-gray-800">Crear Nuevo Usuario</h3>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<form onsubmit="createUser(event)" class="space-y-4">
|
|
||||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
||||||
<div>
|
|
||||||
<label class="block text-xs font-bold text-gray-600 mb-1">Nombre Completo</label>
|
|
||||||
<input type="text" id="uName" required class="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-green-500 outline-none">
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label class="block text-xs font-bold text-gray-600 mb-1">Correo Electrónico</label>
|
|
||||||
<input type="email" id="uEmail" required class="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-green-500 outline-none">
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
||||||
<div>
|
|
||||||
<label class="block text-xs font-bold text-gray-600 mb-1">Contraseña</label>
|
|
||||||
<input type="password" id="uPass" required class="w-full px-3 py-2 border rounded-lg focus:ring-2 focus:ring-green-500 outline-none">
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label class="block text-xs font-bold text-gray-600 mb-1">Rol / Permisos</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">
|
|
||||||
<option value="operario">Operario (Básico)</option>
|
|
||||||
<option value="facturacion">Facturación</option>
|
|
||||||
<option value="admin">Administrador</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<label class="block text-xs font-bold text-gray-600 mb-2">Asignar Gremios</label>
|
|
||||||
<div class="p-3 border rounded-lg bg-gray-50 grid grid-cols-2 md:grid-cols-3 gap-2" id="guildsCheckboxArea">
|
|
||||||
<p class="text-xs text-gray-400">Crea gremios primero para asignarlos.</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="pt-2">
|
|
||||||
<button type="submit" id="btnCreateUser" class="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-3 rounded-lg transition-all shadow-lg shadow-green-500/30">
|
|
||||||
Crear Usuario
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="toast" class="fixed bottom-5 right-5 bg-slate-800 text-white px-6 py-3 rounded-lg shadow-2xl transform translate-y-20 opacity-0 transition-all duration-300 z-50 flex items-center gap-3">
|
<script src="layout.js"></script>
|
||||||
<span id="toastMsg">Mensaje</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const API_URL = "https://integrarepara-api.integrarepara.es";
|
// Aquí va SOLAMENTE la lógica de fetchGuilds, createUser, addGuild
|
||||||
let availableGuilds = [];
|
// Ya no necesitas la lógica de logout ni de cargar nombre de usuario, eso lo hace layout.js
|
||||||
|
|
||||||
// 1. INICIALIZACIÓN
|
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
lucide.createIcons();
|
// Iniciar carga de gremios...
|
||||||
const token = localStorage.getItem("token");
|
|
||||||
if (!token) { window.location.href = "index.html"; return; }
|
|
||||||
|
|
||||||
// Cargar datos
|
|
||||||
fetchGuilds();
|
fetchGuilds();
|
||||||
|
|
||||||
// Logout
|
|
||||||
document.getElementById('btnLogout').addEventListener('click', () => {
|
|
||||||
localStorage.removeItem("token");
|
|
||||||
window.location.href = "index.html";
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// 2. GESTIÓN DE GREMIOS
|
// ... tus funciones fetchGuilds, addGuild, etc ...
|
||||||
async function fetchGuilds() {
|
async function fetchGuilds() { /* ... */ }
|
||||||
try {
|
|
||||||
const res = await fetch(`${API_URL}/guilds`, {
|
|
||||||
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
|
|
||||||
});
|
|
||||||
const data = await res.json();
|
|
||||||
if (data.ok) {
|
|
||||||
availableGuilds = data.guilds;
|
|
||||||
renderGuildsList();
|
|
||||||
renderGuildsCheckboxes();
|
|
||||||
}
|
|
||||||
} catch (e) { console.error(e); }
|
|
||||||
}
|
|
||||||
|
|
||||||
async function addGuild(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
const name = document.getElementById('newGuildName').value;
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${API_URL}/guilds`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Authorization": `Bearer ${localStorage.getItem("token")}`
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ name })
|
|
||||||
});
|
|
||||||
if (res.ok) {
|
|
||||||
document.getElementById('newGuildName').value = "";
|
|
||||||
fetchGuilds(); // Recargar
|
|
||||||
showToast("Gremio añadido");
|
|
||||||
}
|
|
||||||
} catch (e) { showToast("Error al añadir", 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();
|
|
||||||
} catch (e) { console.error(e); }
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. RENDERS (Pintar HTML)
|
|
||||||
function renderGuildsList() {
|
|
||||||
const list = document.getElementById('guildsList');
|
|
||||||
list.innerHTML = "";
|
|
||||||
availableGuilds.forEach(g => {
|
|
||||||
list.innerHTML += `
|
|
||||||
<div class="flex justify-between items-center bg-gray-50 p-2 rounded border hover:bg-white transition">
|
|
||||||
<span class="text-sm font-medium text-gray-700">${g.name}</span>
|
|
||||||
<button onclick="deleteGuild(${g.id})" class="text-red-400 hover:text-red-600"><i data-lucide="trash-2" class="w-4 h-4"></i></button>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
});
|
|
||||||
lucide.createIcons();
|
|
||||||
}
|
|
||||||
|
|
||||||
function renderGuildsCheckboxes() {
|
|
||||||
const area = document.getElementById('guildsCheckboxArea');
|
|
||||||
if (availableGuilds.length === 0) {
|
|
||||||
area.innerHTML = '<p class="text-xs text-gray-400 col-span-3">No hay gremios creados.</p>';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
area.innerHTML = "";
|
|
||||||
availableGuilds.forEach(g => {
|
|
||||||
area.innerHTML += `
|
|
||||||
<label class="flex items-center space-x-2 cursor-pointer p-1 hover:bg-gray-100 rounded">
|
|
||||||
<input type="checkbox" value="${g.id}" class="guild-checkbox form-checkbox h-4 w-4 text-green-600 rounded border-gray-300">
|
|
||||||
<span class="text-sm text-gray-700">${g.name}</span>
|
|
||||||
</label>
|
|
||||||
`;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. CREAR USUARIO
|
|
||||||
async function createUser(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
const btn = document.getElementById('btnCreateUser');
|
|
||||||
btn.disabled = true; btn.innerText = "Guardando...";
|
|
||||||
|
|
||||||
// Recoger gremios seleccionados
|
|
||||||
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,
|
|
||||||
password: document.getElementById('uPass').value,
|
|
||||||
role: document.getElementById('uRole').value,
|
|
||||||
guilds: selectedGuilds
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${API_URL}/admin/users`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"Authorization": `Bearer ${localStorage.getItem("token")}`
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data)
|
|
||||||
});
|
|
||||||
const json = await res.json();
|
|
||||||
|
|
||||||
if (json.ok) {
|
|
||||||
showToast("✅ Usuario creado con éxito");
|
|
||||||
e.target.reset(); // Limpiar form
|
|
||||||
renderGuildsCheckboxes(); // Reset checkboxes
|
|
||||||
} else {
|
|
||||||
showToast("❌ " + json.error, true);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
showToast("Error de conexión", true);
|
|
||||||
} finally {
|
|
||||||
btn.disabled = false; btn.innerText = "Crear Usuario";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function showToast(msg, isError = false) {
|
|
||||||
const toast = document.getElementById('toast');
|
|
||||||
document.getElementById('toastMsg').innerText = msg;
|
|
||||||
toast.className = `fixed bottom-5 right-5 px-6 py-3 rounded-lg shadow-2xl transform transition-all duration-300 z-50 flex items-center gap-3 ${isError ? 'bg-red-600' : 'bg-slate-800'} text-white`;
|
|
||||||
toast.classList.remove('translate-y-20', 'opacity-0');
|
|
||||||
setTimeout(() => toast.classList.add('translate-y-20', 'opacity-0'), 3000);
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user