File size: 3,608 Bytes
cf7fdde
83a43f6
a637953
83a43f6
a637953
3366ed8
 
 
 
 
 
 
 
 
 
 
 
 
 
258b67a
a637953
 
 
 
 
 
 
 
 
 
 
 
83a43f6
a637953
 
 
 
 
 
 
 
 
3366ed8
83a43f6
 
a637953
 
3366ed8
a637953
 
 
 
 
 
 
 
3366ed8
 
 
a637953
 
fa18a7d
a637953
 
 
 
3366ed8
a637953
3366ed8
 
a637953
 
3366ed8
 
a637953
3366ed8
 
83a43f6
a637953
 
 
 
 
83a43f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

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);
});