Actualizar servicios.html
This commit is contained in:
@@ -999,73 +999,58 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function copyClientPortalLink() {
|
async function copyClientPortalLink() {
|
||||||
// Aseguramos que leemos los datos correctos
|
// Cogemos los datos (funciona tanto si es input como texto normal)
|
||||||
const phoneInput = document.getElementById('editPhone');
|
const phoneEl = document.getElementById('editPhone') || document.getElementById('detPhone');
|
||||||
const nameInput = document.getElementById('editName');
|
const nameEl = document.getElementById('editName') || document.getElementById('detName');
|
||||||
const addrInput = document.getElementById('editAddr');
|
const addrEl = document.getElementById('editAddr') || document.getElementById('detAddrText');
|
||||||
|
|
||||||
const phone = phoneInput ? phoneInput.value : "";
|
const phone = phoneEl.value || phoneEl.innerText;
|
||||||
const name = nameInput ? nameInput.value : "Cliente";
|
const name = nameEl.value || nameEl.innerText;
|
||||||
const addr = addrInput ? addrInput.value : "";
|
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") {
|
if (!phone || phone === "Sin Teléfono") {
|
||||||
showToast("⚠️ Falta el teléfono para generar el portal", "warning");
|
showToast("No hay un teléfono válido para este cliente.", "warning");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const btn = document.getElementById('btnPortalLink');
|
const btn = document.getElementById('btnPortalLink');
|
||||||
const originalHtml = btn.innerHTML;
|
const originalHtml = btn.innerHTML;
|
||||||
|
|
||||||
btn.innerHTML = '<i data-lucide="loader-2" class="w-3 h-3 animate-spin"></i> Generando...';
|
btn.innerHTML = '<i data-lucide="loader-2" class="w-3 h-3 animate-spin"></i> Generando...';
|
||||||
lucide.createIcons();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Limpiamos el teléfono antes de enviarlo
|
|
||||||
const cleanPhone = phone.replace(/\D/g, "");
|
|
||||||
|
|
||||||
const res = await fetch(`${API_URL}/clients/ensure`, {
|
const res = await fetch(`${API_URL}/clients/ensure`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` },
|
||||||
"Content-Type": "application/json",
|
body: JSON.stringify({ phone: phone, name: name, address: addr })
|
||||||
"Authorization": `Bearer ${localStorage.getItem("token")}`
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ phone: cleanPhone, name: name, address: addr })
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
if (data.ok && data.client && data.client.portal_token) {
|
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
|
// PLAN ANTI-BLOQUEO HTTP (Evita que el navegador bloquee la copia)
|
||||||
try {
|
if (navigator.clipboard && window.isSecureContext) {
|
||||||
await navigator.clipboard.writeText(portalLink);
|
await navigator.clipboard.writeText(portalLink);
|
||||||
btn.innerHTML = '<i data-lucide="check-circle" class="w-3 h-3"></i> ¡Copiado!';
|
} else {
|
||||||
btn.classList.replace('text-blue-600', 'text-emerald-600');
|
const textArea = document.createElement("textarea");
|
||||||
btn.classList.replace('bg-blue-50', 'bg-emerald-50');
|
textArea.value = portalLink;
|
||||||
btn.classList.replace('border-blue-100', 'border-emerald-200');
|
document.body.appendChild(textArea);
|
||||||
showToast("✅ Enlace copiado al portapapeles");
|
textArea.select();
|
||||||
} catch (clipErr) {
|
document.execCommand('copy');
|
||||||
// Plan B si el navegador bloquea el portapapeles automático
|
textArea.remove();
|
||||||
prompt("Copia este enlace manualmente:", portalLink);
|
|
||||||
btn.innerHTML = '<i data-lucide="check-circle" class="w-3 h-3"></i> Generado';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
btn.innerHTML = '<i data-lucide="check-circle" class="w-3 h-3"></i> ¡Copiado!';
|
||||||
btn.innerHTML = originalHtml;
|
showToast("Enlace copiado. ¡Listo para pegar en WhatsApp!");
|
||||||
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";
|
setTimeout(() => { btn.innerHTML = originalHtml; }, 3000);
|
||||||
lucide.createIcons();
|
|
||||||
}, 3000);
|
|
||||||
} else {
|
} else {
|
||||||
showToast("❌ Error al generar el token", "warning");
|
showToast("Error generando token", "warning");
|
||||||
btn.innerHTML = originalHtml;
|
btn.innerHTML = originalHtml;
|
||||||
lucide.createIcons();
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error obteniendo token:", error);
|
showToast("Error de conexión", "warning");
|
||||||
showToast("❌ Error de conexión", "warning");
|
|
||||||
btn.innerHTML = originalHtml;
|
btn.innerHTML = originalHtml;
|
||||||
lucide.createIcons();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user