(function(window, document) { 'use strict'; var SCOPE = 'RelaysLoader'; var activeHost = null; var SB_URL = 'https://pnvmcckptqftinwlzixi.supabase.co'; var SB_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBudm1jY2twdHFmdGlud2x6aXhpIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Njc5OTA4NTQsImV4cCI6MjA4MzU2Njg1NH0.33Yfhr9IxsHXLFIkaa3iBX-GNR0-0dEK6azn5Ojmm6g'; // CSS var WIDGET_CSS = ` .widget-wrapper { font-family: 'Inter', sans-serif; color: #374151; box-sizing: border-box; } .widget-wrapper * { box-sizing: border-box; } .launcher-btn { position: fixed; bottom: 20px; right: 20px; width: 60px; height: 60px; border-radius: 50%; border: none; cursor: pointer; box-shadow: 0 4px 14px rgba(0,0,0,0.25); display: flex; align-items: center; justify-content: center; transition: transform 0.2s; z-index: 2147483647; } .launcher-btn:hover { transform: scale(1.05); } .launcher-icon { width: 28px; height: 28px; fill: white; } .chat-window { position: fixed; bottom: 100px; right: 20px; width: 380px; max-width: calc(100vw - 40px); height: 600px; max-height: calc(100vh - 120px); background: white; border-radius: 16px; box-shadow: 0 20px 50px rgba(0,0,0,0.15); display: flex; flex-direction: column; opacity: 0; transform: translateY(20px) scale(0.95); pointer-events: none; transition: all 0.2s; z-index: 2147483646; } .chat-window.open { opacity: 1; transform: translateY(0) scale(1); pointer-events: auto; } .chat-header { padding: 20px; color: white; display: flex; align-items: center; gap: 12px; flex-shrink: 0; } .header-icon { width: 42px; height: 42px; background: rgba(255,255,255,0.2); border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 24px; line-height: 1; } .header-info h3 { margin: 0; font-size: 16px; font-weight: 700; } .header-info p { margin: 0; font-size: 12px; opacity: 0.9; } .nav-tabs { display: flex; background: #f3f4f6; padding: 4px; border-bottom: 1px solid #e5e7eb; } .nav-btn { flex: 1; padding: 10px; border: none; background: transparent; font-size: 13px; font-weight: 600; color: #6b7280; cursor: pointer; border-radius: 6px; } .nav-btn.active { background: white; color: #111827; box-shadow: 0 1px 2px rgba(0,0,0,0.05); } .nav-btn.hidden { display: none; } .chat-body { flex: 1; overflow-y: auto; background: #fff; position: relative; display: flex; flex-direction: column; } .view-section { padding: 20px; display: none; flex: 1; flex-direction: column; } .view-section.active { display: flex; } .msg-list { flex: 1; overflow-y: auto; padding-bottom: 10px; } .chat-msg { margin-bottom: 12px; max-width: 85%; padding: 12px 16px; border-radius: 12px; font-size: 14px; line-height: 1.5; } .msg-bot { background: #f3f4f6; color: #1f2937; border-bottom-left-radius: 2px; } .msg-user { background: #4F46E5; color: white; border-bottom-right-radius: 2px; margin-left: auto; } .chat-input-area { margin-top: auto; display: flex; gap: 8px; border-top: 1px solid #e5e7eb; padding-top: 15px; } .chat-input { flex: 1; padding: 10px; border: 1px solid #e5e7eb; border-radius: 20px; outline: none; font-size: 14px; } .send-btn { background: transparent; border: none; cursor: pointer; color: #4F46E5; font-weight: bold; } .cal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; font-weight: bold; font-size: 14px; } .cal-nav { background: #f3f4f6; border: none; border-radius: 4px; padding: 4px 8px; cursor: pointer; } .calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 16px; } .cal-day { aspect-ratio: 1; display: flex; align-items: center; justify-content: center; font-size: 13px; border-radius: 6px; cursor: pointer; } .cal-day:hover:not(.empty) { background: #f3f4f6; } .cal-day.selected { background: #4F46E5; color: white; } .time-slots { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; max-height: 150px; overflow-y: auto; } .time-slot { padding: 8px; text-align: center; border: 1px solid #e5e7eb; border-radius: 6px; font-size: 12px; cursor: pointer; } .time-slot.selected { border-color: #4F46E5; background: #eef2ff; color: #4F46E5; } .form-group { margin-bottom: 12px; } .form-label { display: block; font-size: 12px; font-weight: 600; margin-bottom: 4px; } .form-input { width: 100%; padding: 10px; border: 1px solid #e5e7eb; border-radius: 8px; font-size: 14px; } .primary-btn { width: 100%; padding: 12px; background: #4F46E5; color: white; border: none; border-radius: 8px; font-weight: 600; cursor: pointer; margin-top: 10px; transition: opacity 0.2s; } .primary-btn:disabled { opacity: 0.5; cursor: not-allowed; } .success-view { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; text-align: center; animation: fadeIn 0.3s; } .success-icon { width: 48px; height: 48px; background: #dcfce7; color: #166534; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin-bottom: 15px; font-size: 24px; } .success-title { font-size: 18px; font-weight: 700; margin-bottom: 8px; } .success-msg { font-size: 14px; color: #6b7280; margin-bottom: 20px; } .text-link { color: #4F46E5; font-size: 13px; font-weight: 600; cursor: pointer; text-decoration: underline; } @keyframes fadeIn { from{opacity:0} to{opacity:1} } `; var InternalWidget = { root: null, config: {}, state: { view: 'chat', isOpen: false, messages: [], currentDate: new Date(), selectedDate: null, selectedTime: null, takenSlots: [], successState: null }, mount: function(root, config) { this.root = root; this.config = config; // Default view logic if(config.enableChat !== false) this.state.view = 'chat'; else if(config.enableBooking !== false) this.state.view = 'booking'; else if(config.enableContact !== false) this.state.view = 'contact'; if(this.state.messages.length === 0) { this.state.messages = [{ text: config.chatbotResponses?.default || "Hi! How can we help?", sender: 'bot' }]; } this.render(); }, render: function() { var c = this.config; var s = this.state; var color = c.primaryColor || '#4F46E5'; var chatIcon = ``; var closeIcon = ``; this.root.innerHTML = `
${c.businessIcon ? `
${c.businessIcon}
` : ''}

${c.businessName || 'Us'}

${c.phone || 'Welcome'}

${s.messages.map(m => `
${m.text}
`).join('')}
${s.successState === 'booking' ? this.renderSuccess('Booking Confirmed!', 'We will confirm via email shortly.') : `
${s.currentDate.toLocaleString('default',{month:'long', year:'numeric'})}
${this.renderCalendar()}

Available Times

${this.renderTimeSlots()}
`}
${s.successState === 'contact' ? this.renderSuccess('Message Sent!', 'Thanks for reaching out.') : `
`}
`; this.attachEvents(); }, // --- HELPERS --- renderSuccess: function(title, sub) { return `
${title}
${sub}
`; }, renderCalendar: function() { var year = this.state.currentDate.getFullYear(); var month = this.state.currentDate.getMonth(); var firstDay = new Date(year, month, 1).getDay(); var daysInMonth = new Date(year, month + 1, 0).getDate(); var html = ''; for(let i=0; i`; for(let i=1; i<=daysInMonth; i++) { let isSel = this.state.selectedDate === i; html += `
${i}
`; } return html; }, renderTimeSlots: function() { if(!this.state.selectedDate) return '
Select a date first
'; var start = this.config.bookingStart || "09:00"; var end = this.config.bookingEnd || "17:00"; var dur = parseInt(this.config.slotMinutes) || 60; var buf = parseInt(this.config.bufferMinutes) || 0; var toMin = t => parseInt(t.split(':')[0])*60 + parseInt(t.split(':')[1]); var fromMin = m => { var h = Math.floor(m/60); var mn = m%60; var am = h < 12 ? 'AM' : 'PM'; if(h>12) h-=12; if(h==0) h=12; return `${h}:${mn<10?'0'+mn:mn} ${am}`; }; var current = toMin(start); var endMin = toMin(end); var html = ''; while(current + dur <= endMin) { var timeStr = fromMin(current); let isSel = this.state.selectedTime === timeStr; html += `
${timeStr}
`; current += (dur + buf); } return html; }, attachEvents: function() { var self = this; var root = this.root; root.querySelector('#launcher').onclick = () => { self.state.isOpen = !self.state.isOpen; self.render(); }; root.querySelectorAll('.nav-btn').forEach(b => b.onclick = () => { self.state.view = b.dataset.tab; self.state.isOpen=true; self.render(); }); if(self.state.view === 'booking' && !self.state.successState) { root.querySelector('#prev-month').onclick = () => { self.state.currentDate.setMonth(self.state.currentDate.getMonth()-1); self.render(); }; root.querySelector('#next-month').onclick = () => { self.state.currentDate.setMonth(self.state.currentDate.getMonth()+1); self.render(); }; root.querySelectorAll('.cal-day').forEach(d => d.onclick = function() { if(this.classList.contains('empty')) return; self.state.selectedDate = parseInt(this.dataset.day); self.render(); }); root.querySelectorAll('.time-slot').forEach(t => t.onclick = function() { self.state.selectedTime = this.dataset.time; self.checkBookButton(); self.render(); }); var emailInput = root.querySelector('#book-email'); emailInput.oninput = function() { self.checkBookButton(); }; emailInput.focus(); root.querySelector('#book-btn').onclick = function() { var email = root.querySelector('#book-email').value; this.innerText = "Booking..."; var dateStr = `${self.state.currentDate.getMonth()+1}/${self.state.selectedDate}/${self.state.currentDate.getFullYear()}`; self.submitLead('booking', { date: dateStr, time: self.state.selectedTime, email: email }) .then(() => { self.state.successState = 'booking'; self.render(); }); }; } if(self.state.view === 'contact' && !self.state.successState) { root.querySelector('#contact-btn').onclick = function() { var data = { name: root.querySelector('#c-name').value, email: root.querySelector('#c-email').value, msg: root.querySelector('#c-msg').value }; this.innerText = "Sending..."; self.submitLead('contact', data).then(() => { self.state.successState = 'contact'; self.render(); }); }; } if(self.state.view === 'chat') { var chatInput = root.querySelector('#chat-input'); var sendChat = () => { var txt = chatInput.value.trim(); if(!txt) return; self.state.messages.push({text:txt, sender:'user'}); self.submitLead('chat', {message:txt}); chatInput.value = ''; self.render(); setTimeout(() => { self.state.messages.push({text:"Thanks! We'll be with you shortly.", sender:'bot'}); self.render(); }, 1000); }; root.querySelector('#chat-send').onclick = sendChat; } window.RelaysScope = { resetView: function() { self.state.successState = null; self.state.selectedDate = null; self.state.selectedTime = null; self.render(); } }; }, checkBookButton: function() { var root = this.root; var btn = root.querySelector('#book-btn'); var email = root.querySelector('#book-email').value; var hasTime = this.state.selectedTime !== null; var hasEmail = email.includes('@') && email.includes('.'); if (hasTime && hasEmail) btn.disabled = false; else btn.disabled = true; }, submitLead: function(type, content) { var currentScript = document.querySelector('script[src*="loader.js"]'); var widgetId = currentScript ? currentScript.getAttribute('data-widget-id') : null; if(!widgetId) return Promise.resolve(); return fetch(`${SB_URL}/rest/v1/leads`, { method: 'POST', headers: { 'apikey': SB_KEY, 'Authorization': 'Bearer ' + SB_KEY, 'Content-Type': 'application/json', 'Prefer': 'return=minimal' }, body: JSON.stringify({ widget_id: widgetId, type: type, content: content }) }); } }; function mount(config) { if (activeHost) activeHost.remove(); activeHost = document.createElement('div'); document.body.appendChild(activeHost); var shadow = activeHost.attachShadow({ mode: 'open' }); var root = document.createElement('div'); shadow.appendChild(root); if(config) { InternalWidget.mount(root, config); } else { var script = document.querySelector('script[src*="loader.js"]'); var id = script ? script.getAttribute('data-widget-id') : null; if(id) { fetch(`${SB_URL}/rest/v1/widgets?id=eq.${id}&select=*`, { headers: { 'apikey': SB_KEY, 'Authorization': 'Bearer ' + SB_KEY } }) .then(r => r.json()) .then(data => { if(data && data.length > 0) { var d = data[0]; var dbConfig = { businessName: d.business_name, phone: d.phone, primaryColor: d.primary_color, businessIcon: d.business_icon, chatbotResponses: { default: d.welcome_message }, bookingStart: d.booking_start, bookingEnd: d.booking_end, slotMinutes: d.slot_minutes, bufferMinutes: d.buffer_minutes, // New Toggles enableChat: d.enable_chat, enableBooking: d.enable_booking, enableContact: d.enable_contact }; InternalWidget.mount(root, dbConfig); } }); } } } window[SCOPE] = { mount: mount }; })(window, document);