Actualizar servicios.html

This commit is contained in:
2026-03-04 07:53:54 +00:00
parent 6248503f9d
commit 57e645b301

View File

@@ -999,73 +999,58 @@
}
async function copyClientPortalLink() {
// Aseguramos que leemos los datos correctos
const phoneInput = document.getElementById('editPhone');
const nameInput = document.getElementById('editName');
const addrInput = document.getElementById('editAddr');
// Cogemos los datos (funciona tanto si es input como texto normal)
const phoneEl = document.getElementById('editPhone') || document.getElementById('detPhone');
const nameEl = document.getElementById('editName') || document.getElementById('detName');
const addrEl = document.getElementById('editAddr') || document.getElementById('detAddrText');
const phone = phoneInput ? phoneInput.value : "";
const name = nameInput ? nameInput.value : "Cliente";
const addr = addrInput ? addrInput.value : "";
const phone = phoneEl.value || phoneEl.innerText;
const name = nameEl.value || nameEl.innerText;
const addr = addrEl.value || addrEl.innerText;
const serviceId = document.getElementById('detId').value; // Añadimos el ID del servicio
if (!phone || phone.trim() === "" || phone === "Sin Teléfono") {
showToast("⚠️ Falta el teléfono para generar el portal", "warning");
if (!phone || phone === "Sin Teléfono") {
showToast("No hay un teléfono válido para este cliente.", "warning");
return;
}
const btn = document.getElementById('btnPortalLink');
const originalHtml = btn.innerHTML;
btn.innerHTML = '<i data-lucide="loader-2" class="w-3 h-3 animate-spin"></i> Generando...';
lucide.createIcons();
try {
// Limpiamos el teléfono antes de enviarlo
const cleanPhone = phone.replace(/\D/g, "");
const res = await fetch(`${API_URL}/clients/ensure`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${localStorage.getItem("token")}`
},
body: JSON.stringify({ phone: cleanPhone, name: name, address: addr })
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
body: JSON.stringify({ phone: phone, name: name, address: addr })
});
const data = await res.json();
if (data.ok && data.client && data.client.portal_token) {
const portalLink = `https://portal.integrarepara.es/?token=${data.client.portal_token}`;
const portalLink = `https://portal.integrarepara.es/?token=${data.client.portal_token}&service=${serviceId}`;
// Copiar al portapapeles
try {
// PLAN ANTI-BLOQUEO HTTP (Evita que el navegador bloquee la copia)
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(portalLink);
btn.innerHTML = '<i data-lucide="check-circle" class="w-3 h-3"></i> ¡Copiado!';
btn.classList.replace('text-blue-600', 'text-emerald-600');
btn.classList.replace('bg-blue-50', 'bg-emerald-50');
btn.classList.replace('border-blue-100', 'border-emerald-200');
showToast("✅ Enlace copiado al portapapeles");
} catch (clipErr) {
// Plan B si el navegador bloquea el portapapeles automático
prompt("Copia este enlace manualmente:", portalLink);
btn.innerHTML = '<i data-lucide="check-circle" class="w-3 h-3"></i> Generado';
} else {
const textArea = document.createElement("textarea");
textArea.value = portalLink;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
textArea.remove();
}
setTimeout(() => {
btn.innerHTML = originalHtml;
btn.className = "text-[10px] font-black bg-blue-50 border border-blue-100 hover:bg-blue-600 hover:text-white text-blue-600 px-3 py-1.5 rounded-lg flex items-center gap-1.5 transition-all shadow-sm active:scale-95 shrink-0";
lucide.createIcons();
}, 3000);
btn.innerHTML = '<i data-lucide="check-circle" class="w-3 h-3"></i> ¡Copiado!';
showToast("Enlace copiado. ¡Listo para pegar en WhatsApp!");
setTimeout(() => { btn.innerHTML = originalHtml; }, 3000);
} else {
showToast("Error al generar el token", "warning");
showToast("Error generando token", "warning");
btn.innerHTML = originalHtml;
lucide.createIcons();
}
} catch (error) {
console.error("Error obteniendo token:", error);
showToast("❌ Error de conexión", "warning");
showToast("Error de conexión", "warning");
btn.innerHTML = originalHtml;
lucide.createIcons();
}
}