Actualizar proveedores.html
This commit is contained in:
146
proveedores.html
146
proveedores.html
@@ -14,6 +14,10 @@
|
||||
.service-card { cursor: pointer; transition: all 0.2s ease; position: relative; }
|
||||
.service-card:hover:not(.archived) { transform: translateY(-2px); border-color: #3b82f6; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); }
|
||||
.archived { opacity: 0.7; grayscale: 100%; }
|
||||
|
||||
/* Animación suave para el icono de esperando */
|
||||
.pulse-slow { animation: pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite; }
|
||||
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .5; } }
|
||||
</style>
|
||||
</head>
|
||||
<body class="bg-gray-50 text-gray-800 font-sans antialiased overflow-hidden text-left">
|
||||
@@ -111,6 +115,7 @@
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form id="importForm" onsubmit="handleFinalImport(event)" class="px-8 py-6 space-y-5 overflow-y-auto no-scrollbar text-left">
|
||||
<input type="hidden" id="impScrapedId">
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
@@ -118,14 +123,14 @@
|
||||
<div class="bg-white p-2 rounded-xl shadow-sm text-slate-400 text-left"><i data-lucide="user" class="w-4 h-4"></i></div>
|
||||
<div class="flex flex-col min-w-0 w-full text-left">
|
||||
<span class="text-[9px] font-black text-slate-400 uppercase mb-0.5 text-left">Asegurado</span>
|
||||
<input type="text" id="impName" readonly class="bg-transparent border-none p-0 text-sm font-bold text-slate-700 outline-none w-full text-left">
|
||||
<input type="text" id="impName" class="bg-transparent border-none p-0 text-sm font-bold text-slate-700 outline-none w-full text-left">
|
||||
</div>
|
||||
</div>
|
||||
<div class="bg-slate-50/50 p-3 rounded-2xl border border-slate-100 flex items-center gap-3 text-left">
|
||||
<div class="bg-white p-2 rounded-xl shadow-sm text-emerald-500 text-left"><i data-lucide="phone" class="w-4 h-4 text-left"></i></div>
|
||||
<div class="flex flex-col min-w-0 w-full text-left">
|
||||
<span class="text-[9px] font-black text-slate-400 uppercase mb-0.5 text-left">Teléfono</span>
|
||||
<input type="text" id="impPhone" readonly class="bg-transparent border-none p-0 text-sm font-black text-emerald-600 outline-none w-full text-left">
|
||||
<input type="text" id="impPhone" class="bg-transparent border-none p-0 text-sm font-black text-emerald-600 outline-none w-full text-left">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -169,6 +174,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-wrap items-center gap-3 pt-4 border-t text-left">
|
||||
<button type="button" onclick="saveDraftChanges(event)" class="bg-slate-100 hover:bg-slate-200 text-slate-600 font-black px-4 py-4 rounded-[1.5rem] transition-all flex items-center justify-center gap-2 active:scale-95 text-[10px] uppercase tracking-widest">
|
||||
<i data-lucide="save" class="w-4 h-4"></i> <span>Guardar</span>
|
||||
@@ -312,6 +318,34 @@
|
||||
const guildName = allGuilds.find(g => g.id == raw['guild_id'])?.name || null;
|
||||
const opName = raw['assigned_to_name'] || null;
|
||||
|
||||
// LOGICA DE ESTADO VISUAL
|
||||
let badgeEstado = '';
|
||||
if (!isArchived) {
|
||||
if (svc.automation_status === 'in_progress') {
|
||||
badgeEstado = `
|
||||
<div class="flex flex-col items-end gap-1">
|
||||
<span class="bg-amber-100 text-amber-700 px-3 py-1 rounded-full text-[10px] font-black uppercase flex items-center gap-1.5 border border-amber-200">
|
||||
<span class="w-1.5 h-1.5 bg-amber-500 rounded-full pulse-slow"></span> En Cola / Esperando
|
||||
</span>
|
||||
</div>`;
|
||||
} else if (raw['assigned_to']) {
|
||||
badgeEstado = `
|
||||
<div class="flex flex-col items-end gap-1">
|
||||
<span class="bg-emerald-100 text-emerald-700 px-3 py-1 rounded-full text-[10px] font-black uppercase flex items-center gap-1.5 border border-emerald-200">
|
||||
<i data-lucide="check-circle-2" class="w-3 h-3"></i> Asignado
|
||||
</span>
|
||||
<span class="text-[9px] font-bold text-slate-500 max-w-[120px] truncate text-right">${opName || 'Operario'}</span>
|
||||
</div>`;
|
||||
} else {
|
||||
badgeEstado = `
|
||||
<div class="flex flex-col items-end gap-1">
|
||||
<span class="bg-slate-100 text-slate-500 px-3 py-1 rounded-full text-[10px] font-black uppercase flex items-center gap-1.5 border border-slate-200">
|
||||
<i data-lucide="user-minus" class="w-3 h-3"></i> Sin Asignar
|
||||
</span>
|
||||
</div>`;
|
||||
}
|
||||
}
|
||||
|
||||
const card = document.createElement('div');
|
||||
card.className = `service-card bg-white p-5 rounded-2xl border ${isArchived ? 'archived' : 'shadow-sm'} flex items-center justify-between transition-all group fade-in text-left`;
|
||||
card.onclick = (e) => {
|
||||
@@ -320,7 +354,7 @@
|
||||
else openEditor(svc.id);
|
||||
};
|
||||
card.innerHTML = `
|
||||
<div class="flex items-center gap-4 min-w-0 text-left">
|
||||
<div class="flex items-center gap-4 min-w-0 text-left flex-1">
|
||||
<div class="w-16 h-16 rounded-2xl flex flex-col items-center justify-center shrink-0 shadow-sm border border-slate-100 ${isArchived ? 'bg-gray-200 text-gray-400' : (svc.provider === 'homeserve' ? 'bg-red-50 text-red-600' : 'bg-blue-50 text-blue-600')}">
|
||||
<span class="text-[9px] font-black uppercase tracking-tighter text-left">${svc.provider === 'multiasistencia' ? 'MULTI' : 'HOME'}</span>
|
||||
<i data-lucide="${isArchived ? 'lock' : 'file-text'}" class="w-5 h-5 mt-0.5 text-left"></i>
|
||||
@@ -328,7 +362,7 @@
|
||||
<div class="w-12 h-12 rounded-xl bg-white border border-slate-100 p-2 flex items-center justify-center shrink-0 shadow-inner text-left">
|
||||
<img src="${getLogoUrl(raw['Compañía'] || raw['COMPAÑIA'])}" onerror="this.src='${companyLogos['DEFAULT']}'" class="max-w-full max-h-full object-contain">
|
||||
</div>
|
||||
<div class="min-w-0 text-left">
|
||||
<div class="min-w-0 text-left flex-1">
|
||||
<div class="flex items-center gap-2 text-left">
|
||||
<h3 class="font-black text-slate-800 truncate uppercase text-lg leading-tight text-left">${name}</h3>
|
||||
<span class="text-[8px] font-black px-2 py-0.5 rounded-full border ${statusClass} text-left">${statusLabel}</span>
|
||||
@@ -337,13 +371,15 @@
|
||||
<div class="flex flex-wrap gap-2 mt-2 text-left">
|
||||
<span class="text-[10px] bg-slate-100 text-slate-500 px-2 py-0.5 rounded-lg font-bold border text-left">#${svc.service_ref}</span>
|
||||
${guildName ? `<span class="text-[10px] bg-blue-50 text-blue-600 px-2 py-0.5 rounded-lg font-bold border border-blue-100 text-left"><i data-lucide="hammer" class="w-2.5 h-2.5 inline mr-1"></i>${guildName}</span>` : ''}
|
||||
${opName ? `<span class="text-[10px] bg-purple-50 text-purple-600 px-2 py-0.5 rounded-lg font-bold border border-purple-100 text-left"><i data-lucide="user" class="w-2.5 h-2.5 inline mr-1"></i>${opName}</span>` : ''}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center gap-2 text-left">
|
||||
|
||||
<div class="flex items-center gap-4 text-left shrink-0 pl-4">
|
||||
${badgeEstado}
|
||||
|
||||
${!isArchived ? `
|
||||
<div class="hidden group-hover:flex items-center gap-2 pr-4 transition-all text-left">
|
||||
<div class="hidden group-hover:flex items-center gap-2 transition-all text-left ml-2 border-l border-slate-100 pl-4">
|
||||
<a href="https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(fullAddr)}" target="_blank" class="action-btn p-2.5 rounded-xl bg-slate-50 text-slate-400 hover:text-blue-600 shadow-sm text-left"><i data-lucide="map" class="w-5 h-5 text-left"></i></a>
|
||||
${phone ? `<a href="https://wa.me/34${phone}" target="_blank" class="action-btn p-2.5 rounded-xl bg-slate-50 text-slate-400 hover:text-emerald-600 shadow-sm text-left"><i data-lucide="message-square" class="w-5 h-5 text-left"></i></a>` : ''}
|
||||
</div>` : ''}
|
||||
@@ -366,17 +402,22 @@
|
||||
|
||||
document.getElementById('impScrapedId').value = id;
|
||||
document.getElementById('impName').value = raw['Nombre Cliente'] || raw['CLIENTE'] || "S/N";
|
||||
|
||||
// Permite editar telefono
|
||||
const rawPhone = raw['Teléfono'] || raw['TELEFONOS'] || raw['TELEFONO'] || "";
|
||||
document.getElementById('impPhone').value = rawPhone.match(/[6789]\d{8}/)?.[0] || "";
|
||||
document.getElementById('impPhone').value = rawPhone.match(/[6789]\d{8}/)?.[0] || rawPhone;
|
||||
|
||||
const addr = raw['Dirección'] || raw['DOMICILIO'] || "";
|
||||
const pop = raw['Población'] || raw['POBLACION-PROVINCIA'] || "";
|
||||
document.getElementById('impAddress').value = `${addr} ${pop}`.trim();
|
||||
document.getElementById('impCP').value = raw['Código Postal'] || raw['C.P.'] || "";
|
||||
document.getElementById('impDesc').value = raw['Descripción'] || "";
|
||||
document.getElementById('impDesc').value = raw['Descripción'] || raw['DESCRIPCION'] || "";
|
||||
|
||||
const isUrgent = raw['Urgente'] === 'Sí' || raw['Urgente'] === 'true' || raw['URGENTE'] === 'SI';
|
||||
document.getElementById('impUrgent').value = isUrgent.toString();
|
||||
document.getElementById('impNotesInt').value = raw['internal_notes'] || "";
|
||||
document.getElementById('impNotesExt').value = raw['client_notes'] || "";
|
||||
|
||||
const savedGuild = raw['guild_id'] || "";
|
||||
document.getElementById('impGuild').value = savedGuild;
|
||||
|
||||
@@ -393,14 +434,11 @@
|
||||
|
||||
function closeModal() { document.getElementById('importModal').classList.add('hidden'); }
|
||||
|
||||
async function saveDraftChanges(e) {
|
||||
const id = document.getElementById('impScrapedId').value;
|
||||
const btn = e.currentTarget;
|
||||
btn.disabled = true;
|
||||
function getFormPayload() {
|
||||
const opSelect = document.getElementById('impOperator');
|
||||
const opName = opSelect.options[opSelect.selectedIndex]?.text.includes('--') ? null : opSelect.options[opSelect.selectedIndex]?.text;
|
||||
|
||||
const payload = {
|
||||
return {
|
||||
name: document.getElementById('impName').value,
|
||||
phone: document.getElementById('impPhone').value,
|
||||
address: document.getElementById('impAddress').value,
|
||||
@@ -413,82 +451,92 @@
|
||||
internal_notes: document.getElementById('impNotesInt').value,
|
||||
client_notes: document.getElementById('impNotesExt').value
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
async function saveFormToDB(id, payload) {
|
||||
const res = await fetch(`${API_URL}/providers/scraped/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem("token")}` },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
if(res.ok) { showToast("✅ Borrador guardado"); loadInbox(); }
|
||||
return res.ok;
|
||||
}
|
||||
|
||||
// CAMBIO 2: Guardar Borrador ahora cierra el modal.
|
||||
async function saveDraftChanges(e) {
|
||||
const id = document.getElementById('impScrapedId').value;
|
||||
const btn = e.currentTarget;
|
||||
btn.disabled = true;
|
||||
try {
|
||||
const ok = await saveFormToDB(id, getFormPayload());
|
||||
if(ok) {
|
||||
showToast("✅ Cambios guardados correctamente");
|
||||
closeModal(); // Cierra el modal
|
||||
loadInbox();
|
||||
}
|
||||
else { showToast("❌ Error al guardar", true); }
|
||||
} catch (e) { showToast("❌ Error de red", true); }
|
||||
} catch (err) { showToast("❌ Error de red", true); }
|
||||
finally { btn.disabled = false; }
|
||||
}
|
||||
|
||||
// CAMBIO 3: Traspaso Manual bloquea si no hay operario asignado
|
||||
async function handleFinalImport(event) {
|
||||
event.preventDefault();
|
||||
const btn = event.submitter;
|
||||
if(btn.type === 'button') return;
|
||||
|
||||
const id = document.getElementById('impScrapedId').value;
|
||||
const payload = getFormPayload();
|
||||
|
||||
// VALIDACIÓN: ¿Hay operario seleccionado?
|
||||
if (!payload.assigned_to) {
|
||||
showToast("⚠️ ERROR: Selecciona un operario para hacer el traspaso manual.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
btn.disabled = true;
|
||||
const payload = {
|
||||
name: document.getElementById('impName').value,
|
||||
phone: document.getElementById('impPhone').value,
|
||||
address: document.getElementById('impAddress').value,
|
||||
cp: document.getElementById('impCP').value,
|
||||
description: document.getElementById('impDesc').value,
|
||||
company_ref: document.getElementById('displayRef').innerText.replace('REF: ', ''),
|
||||
guild_id: document.getElementById('impGuild').value,
|
||||
assigned_to: document.getElementById('impOperator').value || null,
|
||||
internal_notes: document.getElementById('impNotesInt').value,
|
||||
client_notes: document.getElementById('impNotesExt').value,
|
||||
is_urgent: document.getElementById('impUrgent').value === 'true'
|
||||
};
|
||||
payload.company_ref = document.getElementById('displayRef').innerText.replace('REF: ', '');
|
||||
|
||||
try {
|
||||
const res = await fetch(`${API_URL}/providers/import/${document.getElementById('impScrapedId').value}`, {
|
||||
await saveFormToDB(id, payload);
|
||||
|
||||
const res = await fetch(`${API_URL}/providers/import/${id}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem("token")}` },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
const result = await res.json();
|
||||
if(result.ok) { showToast("✅ Traspasado con éxito"); closeModal(); loadInbox(); }
|
||||
if(result.ok) { showToast("✅ Expediente traspasado con éxito"); closeModal(); loadInbox(); }
|
||||
else { showToast("❌ Error al importar", true); }
|
||||
} catch (e) { showToast("❌ Error de red", true); }
|
||||
finally { btn.disabled = false; }
|
||||
}
|
||||
|
||||
// NUEVA FUNCIÓN: MANDAR A LA COLA
|
||||
async function sendToQueue() {
|
||||
const id = document.getElementById('impScrapedId').value;
|
||||
const guild_id = document.getElementById('impGuild').value;
|
||||
const cp = document.getElementById('impCP').value;
|
||||
const payload = getFormPayload();
|
||||
|
||||
if (!guild_id) {
|
||||
showToast("⚠️ Selecciona un gremio para el automatismo", true);
|
||||
if (!payload.guild_id) {
|
||||
showToast("⚠️ Debes seleccionar un Gremio Especialista", true);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await saveFormToDB(id, payload);
|
||||
|
||||
showToast("🚀 Iniciando automatismo...");
|
||||
const res = await fetch(`${API_URL}/providers/automate/${id}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${localStorage.getItem("token")}`
|
||||
},
|
||||
body: JSON.stringify({ guild_id, cp })
|
||||
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||
body: JSON.stringify({ guild_id: payload.guild_id, cp: payload.cp })
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.ok) {
|
||||
showToast("✅ ¡En cola! Se ha enviado el primer WhatsApp.");
|
||||
showToast("✅ ¡En cola! Se ha enviado a los operarios.");
|
||||
closeModal();
|
||||
loadInbox();
|
||||
} else {
|
||||
showToast("❌ " + data.error, true);
|
||||
}
|
||||
} catch (e) {
|
||||
showToast("❌ Error de conexión", true);
|
||||
}
|
||||
} else { showToast("❌ " + data.error, true); }
|
||||
} catch (e) { showToast("❌ Error de conexión", true); }
|
||||
}
|
||||
|
||||
function showToast(msg, isError = false) {
|
||||
|
||||
Reference in New Issue
Block a user