Añadir clientes.html
This commit is contained in:
372
clientes.html
Normal file
372
clientes.html
Normal file
@@ -0,0 +1,372 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Clientes - IntegraRepara</title>
|
||||
<script src="https://cdn.tailwindcss.com"></script>
|
||||
<script src="https://unpkg.com/lucide@latest"></script>
|
||||
<style>
|
||||
.fade-in { animation: fadeIn 0.2s ease-in-out; }
|
||||
@keyframes fadeIn { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }
|
||||
/* Ocultar scrollbar pero permitir scroll */
|
||||
.no-scrollbar::-webkit-scrollbar { display: none; }
|
||||
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
|
||||
.selected-card { border-left: 4px solid #2563eb; background-color: #eff6ff; }
|
||||
</style>
|
||||
</head>
|
||||
<body class="bg-gray-100 text-gray-800 font-sans h-screen overflow-hidden flex">
|
||||
|
||||
<div id="sidebar-container" class="h-full shrink-0"></div>
|
||||
|
||||
<div class="flex-1 flex flex-col h-full min-w-0">
|
||||
<div id="header-container"></div>
|
||||
|
||||
<main class="flex-1 flex flex-row overflow-hidden">
|
||||
|
||||
<div class="w-full md:w-1/3 bg-white border-r border-gray-200 flex flex-col z-10 shadow-lg">
|
||||
|
||||
<div class="p-4 border-b border-gray-100 bg-white">
|
||||
<div class="flex justify-between items-center mb-3">
|
||||
<h2 class="text-lg font-bold text-gray-800">Cartera de Clientes</h2>
|
||||
<button onclick="openNewClientModal()" class="bg-blue-600 hover:bg-blue-700 text-white p-2 rounded-lg shadow transition-colors" title="Nuevo Cliente">
|
||||
<i data-lucide="user-plus" class="w-5 h-5"></i>
|
||||
</button>
|
||||
</div>
|
||||
<div class="relative">
|
||||
<i data-lucide="search" class="w-4 h-4 absolute left-3 top-3 text-gray-400"></i>
|
||||
<input type="text" id="searchInput" onkeyup="searchClients()" placeholder="Buscar por nombre, tlf..." class="w-full pl-10 pr-4 py-2 bg-gray-50 border border-gray-200 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 transition-all">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="clientsList" class="flex-1 overflow-y-auto no-scrollbar p-2 space-y-1">
|
||||
<div class="text-center py-10 text-gray-400 text-sm">Cargando clientes...</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="hidden md:flex flex-1 bg-gray-50 flex-col overflow-hidden relative" id="clientDetailPanel">
|
||||
|
||||
<div id="emptyState" class="absolute inset-0 flex flex-col items-center justify-center text-gray-400">
|
||||
<div class="w-20 h-20 bg-gray-200 rounded-full flex items-center justify-center mb-4">
|
||||
<i data-lucide="users" class="w-10 h-10 text-gray-400"></i>
|
||||
</div>
|
||||
<p class="text-lg font-medium">Selecciona un cliente</p>
|
||||
<p class="text-sm">o crea uno nuevo para ver sus detalles</p>
|
||||
</div>
|
||||
|
||||
<div id="clientContent" class="hidden flex-col h-full">
|
||||
|
||||
<div class="bg-white p-6 border-b border-gray-200 shadow-sm shrink-0">
|
||||
<div class="flex justify-between items-start">
|
||||
<div class="flex items-center gap-4">
|
||||
<div class="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center text-2xl font-bold text-blue-600" id="detailAvatar">
|
||||
A
|
||||
</div>
|
||||
<div>
|
||||
<h1 class="text-2xl font-bold text-gray-800 leading-tight" id="detailName">Nombre Cliente</h1>
|
||||
<div class="flex items-center gap-3 mt-1 text-sm text-gray-500">
|
||||
<span class="flex items-center gap-1"><i data-lucide="phone" class="w-3 h-3"></i> <span id="detailPhone">...</span></span>
|
||||
<span class="hidden md:flex items-center gap-1"><i data-lucide="mail" class="w-3 h-3"></i> <span id="detailEmail">...</span></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex gap-2">
|
||||
<a id="btnCall" href="#" class="p-2 text-gray-500 hover:bg-gray-100 rounded-lg transition" title="Llamar"><i data-lucide="phone" class="w-5 h-5"></i></a>
|
||||
<a id="btnWhatsapp" href="#" target="_blank" class="p-2 text-green-600 hover:bg-green-50 rounded-lg transition" title="WhatsApp"><i data-lucide="message-circle" class="w-5 h-5"></i></a>
|
||||
<button onclick="saveNotes()" class="bg-slate-800 text-white px-4 py-2 rounded-lg text-sm font-bold shadow hover:bg-slate-700 transition flex items-center gap-2">
|
||||
<i data-lucide="save" class="w-4 h-4"></i> Guardar Notas
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex-1 overflow-y-auto p-6 space-y-6">
|
||||
|
||||
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||
|
||||
<div class="bg-white p-5 rounded-xl shadow-sm border border-gray-100">
|
||||
<h3 class="text-sm font-bold text-gray-400 uppercase tracking-wider mb-4 flex items-center gap-2">
|
||||
<i data-lucide="map-pin" class="w-4 h-4"></i> Direcciones
|
||||
</h3>
|
||||
<div id="addressList" class="space-y-3">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white p-5 rounded-xl shadow-sm border border-gray-100 flex flex-col">
|
||||
<h3 class="text-sm font-bold text-gray-400 uppercase tracking-wider mb-2 flex items-center gap-2">
|
||||
<i data-lucide="sticky-note" class="w-4 h-4"></i> Notas Privadas
|
||||
</h3>
|
||||
<textarea id="detailNotes" class="w-full flex-1 p-3 bg-yellow-50 border border-yellow-100 rounded-lg text-sm text-gray-700 focus:outline-none resize-none" placeholder="Escribe notas sobre este cliente..."></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
|
||||
<div class="p-4 border-b border-gray-100 bg-gray-50 flex justify-between items-center">
|
||||
<h3 class="font-bold text-gray-700">Historial de Servicios</h3>
|
||||
<button onclick="createNewServiceForClient()" class="text-xs font-bold text-blue-600 hover:underline">+ Nuevo Servicio</button>
|
||||
</div>
|
||||
<div class="overflow-x-auto">
|
||||
<table class="w-full text-sm text-left">
|
||||
<thead class="text-xs text-gray-500 uppercase bg-gray-50 border-b">
|
||||
<tr>
|
||||
<th class="px-4 py-3">Fecha</th>
|
||||
<th class="px-4 py-3">Servicio</th>
|
||||
<th class="px-4 py-3">Estado</th>
|
||||
<th class="px-4 py-3">Técnico</th>
|
||||
<th class="px-4 py-3 text-right">Acción</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="servicesTableBody" class="text-gray-600">
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<div id="newClientModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 flex items-center justify-center backdrop-blur-sm fade-in">
|
||||
<div class="bg-white rounded-xl shadow-2xl w-full max-w-md p-6">
|
||||
<div class="flex justify-between items-center mb-4">
|
||||
<h3 class="text-lg font-bold text-gray-800">Nuevo Cliente</h3>
|
||||
<button onclick="document.getElementById('newClientModal').classList.add('hidden')" class="text-gray-400 hover:text-gray-600"><i data-lucide="x" class="w-5 h-5"></i></button>
|
||||
</div>
|
||||
<form onsubmit="handleCreateClient(event)" class="space-y-4">
|
||||
<input type="text" id="ncName" placeholder="Nombre Completo" required class="w-full border p-2 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none">
|
||||
<input type="tel" id="ncPhone" placeholder="Teléfono" required class="w-full border p-2 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none">
|
||||
<input type="email" id="ncEmail" placeholder="Email (Opcional)" class="w-full border p-2 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none">
|
||||
<input type="text" id="ncAddress" placeholder="Dirección Principal" class="w-full border p-2 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none">
|
||||
<textarea id="ncNotes" placeholder="Notas iniciales..." class="w-full border p-2 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none" rows="3"></textarea>
|
||||
<button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 rounded-lg shadow transition">Crear Ficha</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="toast" class="fixed bottom-5 right-5 bg-slate-800 text-white px-6 py-3 rounded-lg shadow-2xl translate-y-20 opacity-0 transition-all duration-300 z-50 flex items-center gap-3"><span id="toastMsg"></span></div>
|
||||
|
||||
<script src="js/layout.js"></script>
|
||||
<script>
|
||||
let currentClientId = null;
|
||||
let currentClientData = null;
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
loadClients();
|
||||
});
|
||||
|
||||
// 1. CARGAR LISTA
|
||||
async function loadClients(search = "") {
|
||||
const list = document.getElementById('clientsList');
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/clients?search=${search}`, {
|
||||
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
list.innerHTML = "";
|
||||
if(data.clients.length === 0) {
|
||||
list.innerHTML = `<div class="text-center py-8 text-gray-400 text-xs">No se encontraron clientes.</div>`;
|
||||
return;
|
||||
}
|
||||
|
||||
data.clients.forEach(c => {
|
||||
const el = document.createElement('div');
|
||||
el.className = `p-3 mb-1 rounded-lg cursor-pointer hover:bg-gray-50 transition border border-transparent hover:border-gray-200 flex justify-between items-center group`;
|
||||
el.id = `client-card-${c.id}`;
|
||||
el.onclick = () => loadClientDetails(c.id);
|
||||
|
||||
el.innerHTML = `
|
||||
<div class="flex items-center gap-3 overflow-hidden">
|
||||
<div class="w-10 h-10 rounded-full bg-slate-100 flex items-center justify-center text-slate-500 font-bold text-sm shrink-0">
|
||||
${c.full_name.charAt(0).toUpperCase()}
|
||||
</div>
|
||||
<div class="min-w-0">
|
||||
<p class="font-bold text-gray-700 text-sm truncate">${c.full_name}</p>
|
||||
<p class="text-xs text-gray-400 truncate">${c.phone}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-right shrink-0">
|
||||
<span class="bg-blue-50 text-blue-600 text-[10px] font-bold px-2 py-1 rounded-full border border-blue-100">
|
||||
${c.service_count} Svc
|
||||
</span>
|
||||
</div>
|
||||
`;
|
||||
list.appendChild(el);
|
||||
});
|
||||
} catch(e) { console.error(e); }
|
||||
}
|
||||
|
||||
function searchClients() {
|
||||
const val = document.getElementById('searchInput').value;
|
||||
loadClients(val);
|
||||
}
|
||||
|
||||
// 2. CARGAR DETALLES
|
||||
async function loadClientDetails(id) {
|
||||
currentClientId = id;
|
||||
|
||||
// UI Toggle
|
||||
document.getElementById('emptyState').classList.add('hidden');
|
||||
document.getElementById('clientContent').classList.remove('hidden');
|
||||
document.getElementById('clientContent').classList.add('flex'); // Flex para estructura
|
||||
|
||||
// Resaltar seleccionado
|
||||
document.querySelectorAll('#clientsList > div').forEach(d => d.classList.remove('selected-card'));
|
||||
const card = document.getElementById(`client-card-${id}`);
|
||||
if(card) card.classList.add('selected-card');
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/clients/${id}/details`, {
|
||||
headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` }
|
||||
});
|
||||
const data = await res.json();
|
||||
const c = data.client;
|
||||
currentClientData = c;
|
||||
|
||||
// Rellenar Cabecera
|
||||
document.getElementById('detailAvatar').innerText = c.full_name.charAt(0).toUpperCase();
|
||||
document.getElementById('detailName').innerText = c.full_name;
|
||||
document.getElementById('detailPhone').innerText = c.phone;
|
||||
document.getElementById('detailEmail').innerText = c.email || "Sin email";
|
||||
document.getElementById('detailNotes').value = c.notes || "";
|
||||
|
||||
// Botones Acción
|
||||
document.getElementById('btnCall').href = `tel:${c.phone}`;
|
||||
document.getElementById('btnWhatsapp').href = `https://wa.me/${c.phone.replace('+','')}`;
|
||||
|
||||
// Direcciones
|
||||
const addrContainer = document.getElementById('addressList');
|
||||
addrContainer.innerHTML = "";
|
||||
const addresses = c.addresses || [];
|
||||
if(addresses.length === 0) {
|
||||
addrContainer.innerHTML = `<p class="text-xs text-gray-400 italic">No hay direcciones guardadas.</p>`;
|
||||
} else {
|
||||
addresses.forEach(addr => {
|
||||
const div = document.createElement('div');
|
||||
div.className = "flex justify-between items-center text-sm p-2 bg-gray-50 rounded border border-gray-100";
|
||||
div.innerHTML = `
|
||||
<span class="truncate pr-2">${addr}</span>
|
||||
<a href="https://maps.google.com/?q=${encodeURIComponent(addr)}" target="_blank" class="text-blue-500 hover:text-blue-700">
|
||||
<i data-lucide="map" class="w-4 h-4"></i>
|
||||
</a>
|
||||
`;
|
||||
addrContainer.appendChild(div);
|
||||
});
|
||||
}
|
||||
|
||||
// Tabla de Servicios
|
||||
const tbody = document.getElementById('servicesTableBody');
|
||||
tbody.innerHTML = "";
|
||||
if(data.services.length === 0) {
|
||||
tbody.innerHTML = `<tr><td colspan="5" class="px-4 py-8 text-center text-xs text-gray-400">Este cliente aún no tiene servicios.</td></tr>`;
|
||||
} else {
|
||||
data.services.forEach(s => {
|
||||
const tr = document.createElement('tr');
|
||||
tr.className = "border-b border-gray-50 hover:bg-gray-50 transition";
|
||||
const date = new Date(s.scheduled_date).toLocaleDateString();
|
||||
const color = s.status_color || 'gray';
|
||||
|
||||
tr.innerHTML = `
|
||||
<td class="px-4 py-3 font-mono text-xs text-gray-500">${date}</td>
|
||||
<td class="px-4 py-3 font-medium text-gray-800">${s.title || 'Servicio'}</td>
|
||||
<td class="px-4 py-3"><span class="bg-${color}-100 text-${color}-700 px-2 py-1 rounded text-xs font-bold">${s.status_name}</span></td>
|
||||
<td class="px-4 py-3 text-xs text-gray-500">${s.assigned_name || '-'}</td>
|
||||
<td class="px-4 py-3 text-right">
|
||||
<a href="servicios.html?id=${s.id}" class="text-blue-600 hover:underline text-xs font-bold">Ver</a>
|
||||
</td>
|
||||
`;
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
}
|
||||
|
||||
lucide.createIcons();
|
||||
|
||||
} catch(e) { console.error(e); }
|
||||
}
|
||||
|
||||
// 3. GUARDAR NOTAS
|
||||
async function saveNotes() {
|
||||
if(!currentClientId || !currentClientData) return;
|
||||
const newNotes = document.getElementById('detailNotes').value;
|
||||
|
||||
try {
|
||||
// Hacemos PUT actualizando solo las notas, manteniendo el resto
|
||||
const body = {
|
||||
full_name: currentClientData.full_name,
|
||||
email: currentClientData.email,
|
||||
addresses: currentClientData.addresses,
|
||||
notes: newNotes
|
||||
};
|
||||
|
||||
const res = await fetch(`${API_URL}/clients/${currentClientId}`, {
|
||||
method: 'PUT',
|
||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
|
||||
if(res.ok) {
|
||||
currentClientData.notes = newNotes; // Actualizar local
|
||||
showToast("Notas guardadas");
|
||||
} else {
|
||||
showToast("Error al guardar", true);
|
||||
}
|
||||
} catch(e) { showToast("Error conexión", true); }
|
||||
}
|
||||
|
||||
// 4. CREAR CLIENTE
|
||||
function openNewClientModal() {
|
||||
document.getElementById('newClientModal').classList.remove('hidden');
|
||||
document.getElementById('ncName').focus();
|
||||
}
|
||||
|
||||
async function handleCreateClient(e) {
|
||||
e.preventDefault();
|
||||
const data = {
|
||||
full_name: document.getElementById('ncName').value,
|
||||
phone: document.getElementById('ncPhone').value,
|
||||
email: document.getElementById('ncEmail').value,
|
||||
address: document.getElementById('ncAddress').value,
|
||||
notes: document.getElementById('ncNotes').value
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/clients`, {
|
||||
method: 'POST',
|
||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
|
||||
const json = await res.json();
|
||||
if(res.ok) {
|
||||
showToast("Cliente creado");
|
||||
document.getElementById('newClientModal').classList.add('hidden');
|
||||
e.target.reset();
|
||||
loadClients(); // Recargar lista
|
||||
loadClientDetails(json.id); // Abrir el nuevo cliente
|
||||
} else {
|
||||
showToast("Error al crear", true);
|
||||
}
|
||||
} catch(e) { showToast("Error conexión", true); }
|
||||
}
|
||||
|
||||
function createNewServiceForClient() {
|
||||
if(!currentClientData) return;
|
||||
// Redirigir a servicios pre-rellenando datos (requiere lógica en servicios.html, o un modal aquí)
|
||||
// Por simplicidad, mandamos un alert o podríamos abrir el modal de servicios aquí si lo importamos.
|
||||
// Opción Pro: Navegar a calendario con el cliente seleccionado
|
||||
window.location.href = `calendario.html?client_id=${currentClientId}`;
|
||||
}
|
||||
|
||||
function showToast(msg, isError = false) {
|
||||
const t = document.getElementById('toast'), m = document.getElementById('toastMsg');
|
||||
t.className = `fixed bottom-5 right-5 px-6 py-3 rounded-lg shadow-xl transition-all duration-300 z-50 flex items-center gap-3 ${isError ? 'bg-red-600' : 'bg-slate-800'} text-white font-medium`;
|
||||
m.innerText = msg; t.classList.remove('translate-y-20', 'opacity-0');
|
||||
setTimeout(() => t.classList.add('translate-y-20', 'opacity-0'), 3000);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user