diff --git a/calendario.html b/calendario.html index 26881a4..10077f7 100644 --- a/calendario.html +++ b/calendario.html @@ -126,8 +126,11 @@

Compañía

-

Nombre Cliente

+

Nombre Cliente

+ @@ -528,6 +531,54 @@ function callClient() { const p = document.getElementById('detPhoneRaw').value; if (p) window.location.href = `tel:+34${p}`; else alert("Sin teléfono"); } function openWhatsApp() { const p = document.getElementById('detPhoneRaw').value; if (p) window.open(`https://wa.me/34${p}`, '_blank'); else alert("Sin teléfono"); } + + async function copyAndOpenPortal() { + const btn = document.getElementById('btnPortal'); + const originalHtml = btn.innerHTML; + btn.innerHTML = ' Generando...'; + lucide.createIcons(); + + try { + const phone = document.getElementById('detPhoneRaw').value; + const name = document.getElementById('detName').innerText; + const address = document.getElementById('detAddress').innerText; + + if (!phone) { + showToast("Sin teléfono no hay portal", true); + return; + } + + // Usamos la API que ya tienes para obtener el token mágico del cliente + const res = await fetch(`${API_URL}/clients/ensure`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem("token")}` }, + body: JSON.stringify({ phone, name, address }) + }); + const data = await res.json(); + + if (data.ok && data.client.portal_token) { + const link = `https://portal.integrarepara.es/?token=${data.client.portal_token}&service=${currentServiceId}`; + + // Intentamos copiar al portapapeles + try { + await navigator.clipboard.writeText(link); + showToast("¡Enlace copiado al portapapeles!"); + } catch (err) { + console.warn("Navegador bloqueó el copiado automático"); + } + + // Y lo abrimos inmediatamente en otra pestaña + window.open(link, '_blank'); + } else { + showToast("Error al obtener el enlace", true); + } + } catch (e) { + showToast("Error de conexión", true); + } finally { + btn.innerHTML = originalHtml; + lucide.createIcons(); + } + } function openMaps() { const a = document.getElementById('detAddress').innerText; if (a) {