75 lines
2.3 KiB
JavaScript
75 lines
2.3 KiB
JavaScript
// FinMox Flow main.js
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const chatContainer = document.getElementById('chat-container');
|
|
const chatToggle = document.getElementById('chat-toggle');
|
|
const closeChat = document.getElementById('close-chat');
|
|
const chatInput = document.getElementById('chat-input');
|
|
const sendChat = document.getElementById('send-chat');
|
|
const chatBody = document.getElementById('chat-body');
|
|
|
|
// Toggle chat window
|
|
if (chatToggle) {
|
|
chatToggle.addEventListener('click', function() {
|
|
chatContainer.style.display = (chatContainer.style.display === 'flex') ? 'none' : 'flex';
|
|
});
|
|
}
|
|
|
|
// Close chat window
|
|
if (closeChat) {
|
|
closeChat.addEventListener('click', function() {
|
|
chatContainer.style.display = 'none';
|
|
});
|
|
}
|
|
|
|
// Send message
|
|
const sendMessage = () => {
|
|
const message = chatInput.value.trim();
|
|
if (message === '') return;
|
|
|
|
appendMessage(message, 'user');
|
|
chatInput.value = '';
|
|
|
|
fetch('api/chat.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ message: message })
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.reply) {
|
|
appendMessage(data.reply, 'ai');
|
|
} else {
|
|
appendMessage('Sorry, something went wrong.', 'ai');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
appendMessage('Sorry, something went wrong.', 'ai');
|
|
});
|
|
};
|
|
|
|
if (sendChat) {
|
|
sendChat.addEventListener('click', sendMessage);
|
|
}
|
|
|
|
if (chatInput) {
|
|
chatInput.addEventListener('keypress', function(e) {
|
|
if (e.key === 'Enter') {
|
|
sendMessage();
|
|
}
|
|
});
|
|
}
|
|
|
|
// Append message to chat body
|
|
const appendMessage = (message, sender) => {
|
|
const messageElement = document.createElement('div');
|
|
messageElement.classList.add('chat-message', `chat-message-${sender}`);
|
|
messageElement.textContent = message;
|
|
chatBody.appendChild(messageElement);
|
|
chatBody.scrollTop = chatBody.scrollHeight; // Scroll to bottom
|
|
};
|
|
});
|