document.addEventListener('DOMContentLoaded', function() { const agentCards = document.querySelectorAll('.agent-card'); const confirmation = document.getElementById('confirmation'); function showToast(message, type = 'success') { const toast = document.createElement('div'); toast.className = `fixed bottom-4 right-4 px-4 py-2 rounded-md shadow-lg text-white ${ type === 'success' ? 'bg-green-500' : 'bg-red-500' }`; toast.textContent = message; document.body.appendChild(toast); setTimeout(() => { toast.classList.add('opacity-0', 'transition-opacity', 'duration-300'); setTimeout(() => toast.remove(), 300); }, 3000); } agentCards.forEach(card => { // Toggle form visibility when card is clicked card.addEventListener('click', function(e) { if (!e.target.closest('.agent-form')) { const form = this.querySelector('.agent-form'); document.querySelectorAll('.agent-form').forEach(f => { if (f !== form) f.classList.add('hidden'); }); form.classList.toggle('hidden'); } }); // Handle activation button click const activateBtn = card.querySelector('.agent-activate-btn'); if (activateBtn) { activateBtn.addEventListener('click', async function(e) { e.stopPropagation(); const phoneInput = card.querySelector('.agent-phone'); const phone = phoneInput.value.trim(); const agentType = card.dataset.agent; const webhookUrl = card.dataset.webhook; if (!phone) { showToast('Please enter a phone number', 'error'); return; } if (!webhookUrl || webhookUrl.includes('YOUR-')) { showToast('Webhook URL not configured for this agent', 'error'); return; } activateBtn.disabled = true; activateBtn.textContent = 'Activating...'; try { const response = await fetch(webhookUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ agent: agentType, phone, timestamp: new Date().toISOString() }) }); if (response.ok) { confirmation.classList.remove('hidden'); confirmation.scrollIntoView({ behavior: 'smooth', block: 'center' }); phoneInput.value = ''; card.querySelector('.agent-form').classList.add('hidden'); setTimeout(() => confirmation.classList.add('hidden'), 5000); } else { showToast('Failed to activate agent', 'error'); } } catch (error) { console.error('Error:', error); showToast('Connection error - please try again', 'error'); } finally { activateBtn.disabled = false; activateBtn.textContent = 'Activate Agent'; } }); } }); // Initialize feather icons after dynamic content loads setTimeout(() => feather.replace(), 100); });