(function () { 'use strict'; /* ========================= BOOTSTRAP SEGURO (GTM / SPA) ========================= */ if (window.__NPW_LOADED__) return; window.__NPW_LOADED__ = true; /* ========================= ESTADO GLOBAL ========================= */ var NPW = { config: { defaultWhatsLink: "https://wa.me/5511999254339?text=Estou no site do Dr. David Mognato e gostaria de mais informações", apiURL: "https://wapi.bernardo.app.br/v1/2026davidmognato", clientId: "2026davidmognato", interceptSelector: 'a.bnd-whats', minNameLength: 3, phoneDigits: 11 }, state: { pendingUrl: null, buttonHTML: null } }; /* ========================= READY ========================= */ function onReady(fn) { if (document.readyState !== 'loading') fn(); else document.addEventListener('DOMContentLoaded', fn); } onReady(initNPW); /* ========================= INIT ========================= */ function initNPW() { if (!document.body || document.getElementById('npw-btn')) return; injectCSS(); injectHTML(); bindStaticEvents(); observeWhatsLinks(); } /* ========================= CSS ========================= */ function injectCSS() { if (document.getElementById('npw-style')) return; var style = document.createElement('style'); style.id = 'npw-style'; style.textContent = ` #npw-btn img {width:16px;height:16px;} #npw-overlay{position:fixed;inset:0;background:rgba(0,0,0,.5);display:none;justify-content:center;align-items:center;z-index:9999} #npw-modal{background:#fff;padding:24px;border-radius:12px;max-width:400px;width:90%;font-family:Segoe UI,Tahoma,sans-serif;display:flex;flex-direction:column;gap:14px} #npw-modal h2{margin:0;font-size:17px;color:#333} #npw-modal input{padding:10px;border-radius:8px;border:1px solid #ccc;font-size:15px} #npw-modal button{background:#25d366;color:#fff;border:0;padding:10px;border-radius:8px;font-weight:700;cursor:pointer} #npw-btn{font-size:16px;display:flex;align-items:center;gap: 8px;position:fixed;bottom:20px;left:20px;z-index:10000;background:#854d0e;color:#fff;border:0;padding:12px 20px;border-radius:8px;font-weight:600;cursor:pointer;box-shadow:0 4px 12px rgba(0,0,0,.15)} `; document.head.appendChild(style); } /* ========================= HTML ========================= */ function injectHTML() { var html = `

Informe seus dados e fale com nossa equipe

`; document.body.insertAdjacentHTML('beforeend', html); } /* ========================= EVENTS ========================= */ function bindStaticEvents() { var overlay = document.getElementById('npw-overlay'); var btn = document.getElementById('npw-btn'); var nameInput = document.getElementById('npw-name'); var phoneInput = document.getElementById('npw-phone'); var submit = document.getElementById('npw-submit'); NPW.state.buttonHTML = btn.innerHTML; phoneInput.addEventListener('input', maskPhone); btn.addEventListener('click', toggleModal); overlay.addEventListener('click', function (e) { if (e.target === overlay) closeModal(); }); submit.addEventListener('click', function () { handleSubmit(nameInput, phoneInput); }); } /* ========================= MODAL ========================= */ function toggleModal() { var overlay = document.getElementById('npw-overlay'); var btn = document.getElementById('npw-btn'); var open = overlay.style.display === 'flex'; overlay.style.display = open ? 'none' : 'flex'; btn.innerHTML = open ? NPW.state.buttonHTML : 'Fechar'; if (!open) document.getElementById('npw-name').focus(); } function closeModal() { document.getElementById('npw-overlay').style.display = 'none'; document.getElementById('npw-btn').innerHTML = NPW.state.buttonHTML; NPW.state.pendingUrl = null; document.getElementById('npw-name').value = ''; document.getElementById('npw-phone').value = ''; } /* ========================= WHATS LINKS (SPA SAFE) ========================= */ function observeWhatsLinks() { bindWhatsLinks(); new MutationObserver(bindWhatsLinks) .observe(document.body, { childList: true, subtree: true }); } function bindWhatsLinks() { document.querySelectorAll(NPW.config.interceptSelector).forEach(function (link) { if (link.__npwBound) return; link.__npwBound = true; link.addEventListener('click', function (e) { e.preventDefault(); NPW.state.pendingUrl = link.href; openModal(); }); }); } function openModal() { document.getElementById('npw-overlay').style.display = 'flex'; document.getElementById('npw-btn').innerHTML = 'Fechar'; document.getElementById('npw-name').focus(); } /* ========================= FORM ========================= */ function getParam(key) { const params = new URLSearchParams(window.location.search); return params.get(key); } function maskPhone(e) { var v = e.target.value.replace(/\D/g, '').slice(0, 11); v = v.replace(/^(\d{2})(\d)/, '($1) $2'); v = v.replace(/(\d{5})(\d)/, '$1-$2'); e.target.value = v; } async function handleSubmit(nameInput, phoneInput) { var name = nameInput.value.trim(); var phoneDigits = phoneInput.value.replace(/\D/g, ''); if (name.length < NPW.config.minNameLength) return alert('Informe seu nome.'); if (phoneDigits.length !== NPW.config.phoneDigits) return alert('Número inválido.'); await fetch(NPW.config.apiURL, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'number=' + encodeURIComponent(phoneDigits) + '&name=' + encodeURIComponent(name) + '&id=' + encodeURIComponent(NPW.config.clientId) + '&gclid=' + encodeURIComponent(getParam('gclid')) + '&utm_source=' + encodeURIComponent(getParam('utm_source')) + '&utm_campaign=' + encodeURIComponent(getParam('utm_campaign')) + '&utm_medium=' + encodeURIComponent(getParam('utm_medium')) }); window.dataLayer = window.dataLayer || []; dataLayer.push({ event: 'whatsapp_lead_submit', user_phone: '+55' + phoneDigits, user_name: name, }); //window.open(NPW.state.pendingUrl || NPW.config.defaultWhatsLink, '_blank'); window.location.href = NPW.state.pendingUrl || NPW.config.defaultWhatsLink; closeModal(); } })();