(function () { 'use strict'; /* ========================= BOOTSTRAP SEGURO (GTM / SPA) ========================= */ if (window.__NPW_LOADED__) return; window.__NPW_LOADED__ = true; /* ========================= CONFIGURAÇÃO GLOBAL ========================= */ var NPW = { config: { defaultWhatsLink: "https://api.whatsapp.com/send/?phone=5511999254339&text=Estou no site do Dr. David Mognato e gostaria de mais informações", apiURL: "https://wapi.bernardo.app.br/v1/1", clientId: "1", interceptSelector: 'a.npw-whats', minNameLength: 3, phoneDigits: 11 }, state: { pendingUrl: null, buttonHTML: null, submitting: false } }; /* ========================= 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;position:relative} #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:#ff0000;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)} #npw-error{color:red;font-size:14px;min-height:18px;margin-bottom:6px} `; 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 () { if (!NPW.state.submitting) handleSubmit(nameInput, phoneInput, submit); }); document.addEventListener('keydown', function (e) { if (e.key === 'Escape') closeModal(); }); } /* ========================= 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(); clearError(); } function closeModal() { document.getElementById('npw-overlay').style.display = 'none'; document.getElementById('npw-btn').innerHTML = NPW.state.buttonHTML; NPW.state.pendingUrl = null; NPW.state.submitting = false; clearError(); document.getElementById('npw-name').value = ''; document.getElementById('npw-phone').value = ''; } function showError(msg, input) { const errorDiv = document.getElementById('npw-error'); if (errorDiv) errorDiv.textContent = msg; if (input) { input.style.borderColor = 'red'; input.focus(); } } function clearError() { const errorDiv = document.getElementById('npw-error'); if (errorDiv) errorDiv.textContent = ''; document.getElementById('npw-name').style.borderColor = ''; document.getElementById('npw-phone').style.borderColor = ''; } /* ========================= 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(); clearError(); } /* ========================= FORM ========================= */ function getParam(key) { const params = new URLSearchParams(window.location.search); const value = params.get(key); if (value) sessionStorage.setItem(key, value); return sessionStorage.getItem(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, submit) { var name = nameInput.value.trim(); var phoneDigits = phoneInput.value.replace(/\D/g, ''); clearError(); if (name.length < NPW.config.minNameLength) return showError('Informe seu nome.', nameInput); if (phoneDigits.length !== NPW.config.phoneDigits) return showError('Número de WhatsApp inválido.', phoneInput); NPW.state.submitting = true; submit.disabled = true; submit.textContent = 'Enviando...'; try { 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')) + '&page=' + encodeURIComponent(location.href) }); } catch (err) { showError('Erro ao enviar. Tente novamente.', null); } 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'); closeModal(); submit.disabled = false; submit.textContent = 'Enviar e esperar'; } })();