diff --git a/backend/public/index.html b/backend/public/index.html index 6c9807e..ab8198a 100644 --- a/backend/public/index.html +++ b/backend/public/index.html @@ -1159,6 +1159,37 @@ pollingIntervalId: null, actionLoading: false, + // Modals + showAddContactModal: false, + showNewTemplateModal: false, + showLaunchCampaignModal: false, + + // Forms + contactName: '', + contactPhone: '', + contactEmail: '', + contactNotes: '', + + templateName: '', + templateBody: '', + templateType: 'text', + templateMediaUrl: '', + + campaignName: '', + campaignGroupId: '', + campaignSessionId: '', + campaignTemplateId: '', + + groups: [], + + chatbotSettings: { + is_active: '0', + trigger_type: 'keyword', + keyword: '', + ai_prompt: '', + gemini_api_key: '' + }, + // Methods checkAuth() { this.token = localStorage.getItem('nabeh_token'); @@ -1405,6 +1436,196 @@ } catch (err) { console.error('Error fetching campaigns:', err); } + }, + + async fetchGroups() { + try { + const response = await fetch('/api/groups', { + headers: { 'Authorization': `Bearer ${this.token}` } + }); + const data = await response.json(); + if (response.ok) { + this.groups = data.groups || data.data || []; + } + } catch (err) { + console.error('Error fetching groups:', err); + } + }, + + async fetchChatbotSettings() { + try { + const response = await fetch('/api/chatbot/rules', { + headers: { 'Authorization': `Bearer ${this.token}` } + }); + const data = await response.json(); + if (response.ok && data.data && data.data.length > 0) { + const rule = data.data[0]; + this.chatbotSettings.is_active = rule.is_active.toString(); + this.chatbotSettings.trigger_type = rule.trigger_type; + this.chatbotSettings.keyword = rule.keyword || ''; + this.chatbotSettings.ai_prompt = rule.ai_prompt || ''; + this.chatbotSettings.gemini_api_key = rule.gemini_api_key ? '••••••••' : ''; + } else { + this.chatbotSettings = { + is_active: '0', + trigger_type: 'keyword', + keyword: '', + ai_prompt: '', + gemini_api_key: '' + }; + } + } catch (err) { + console.error('Error fetching chatbot settings:', err); + } + }, + + async saveChatbotSettings() { + this.actionLoading = true; + try { + const payload = { + is_active: this.chatbotSettings.is_active === '1', + trigger_type: this.chatbotSettings.trigger_type, + keyword: this.chatbotSettings.keyword, + ai_prompt: this.chatbotSettings.ai_prompt, + gemini_api_key: this.chatbotSettings.gemini_api_key, + session_id: this.whatsappSession ? this.whatsappSession.id : null + }; + const response = await fetch('/api/chatbot/rules', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${this.token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(payload) + }); + const data = await response.json(); + if (response.ok) { + alert('Chatbot settings saved successfully.'); + await this.fetchChatbotSettings(); + } else { + alert(data.message || 'Failed to save chatbot settings.'); + } + } catch (err) { + alert('Error communicating with backend API.'); + } finally { + this.actionLoading = false; + } + }, + + openAddContactModal() { + this.contactName = ''; + this.contactPhone = ''; + this.contactEmail = ''; + this.contactNotes = ''; + this.showAddContactModal = true; + }, + + async submitAddContact() { + this.actionLoading = true; + try { + const response = await fetch('/api/contacts', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${this.token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + name: this.contactName, + phone: this.contactPhone, + email: this.contactEmail, + notes: this.contactNotes + }) + }); + const data = await response.json(); + if (response.ok) { + this.showAddContactModal = false; + await this.fetchContacts(); + } else { + alert(data.error || 'Failed to create contact.'); + } + } catch (err) { + alert('Connection error.'); + } finally { + this.actionLoading = false; + } + }, + + openNewTemplateModal() { + this.templateName = ''; + this.templateBody = ''; + this.templateType = 'text'; + this.templateMediaUrl = ''; + this.showNewTemplateModal = true; + }, + + async submitNewTemplate() { + this.actionLoading = true; + try { + const response = await fetch('/api/templates', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${this.token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + name: this.templateName, + body: this.templateBody, + type: this.templateType, + media_url: this.templateMediaUrl + }) + }); + const data = await response.json(); + if (response.ok) { + this.showNewTemplateModal = false; + await this.fetchTemplates(); + } else { + alert(data.error || 'Failed to create template.'); + } + } catch (err) { + alert('Connection error.'); + } finally { + this.actionLoading = false; + } + }, + + async openLaunchCampaignModal() { + this.campaignName = ''; + this.campaignGroupId = ''; + this.campaignSessionId = this.whatsappSession ? this.whatsappSession.id : ''; + this.campaignTemplateId = ''; + await this.fetchGroups(); + await this.fetchTemplates(); + this.showLaunchCampaignModal = true; + }, + + async submitLaunchCampaign() { + this.actionLoading = true; + try { + const response = await fetch('/api/campaigns', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${this.token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + name: this.campaignName, + group_id: this.campaignGroupId, + session_id: this.campaignSessionId, + template_id: this.campaignTemplateId + }) + }); + const data = await response.json(); + if (response.ok) { + this.showLaunchCampaignModal = false; + await this.fetchCampaigns(); + } else { + alert(data.error || 'Failed to launch campaign.'); + } + } catch (err) { + alert('Connection error.'); + } finally { + this.actionLoading = false; + } } } }