38676-vm/assets/js/main.js
2026-03-16 22:54:13 +00:00

58 lines
2.0 KiB
JavaScript

document.addEventListener('DOMContentLoaded', () => {
const chatForm = document.getElementById('chat-form');
const chatInput = document.getElementById('chat-input');
const chatMessages = document.getElementById('chat-messages');
const appendMessage = (text, sender) => {
const msgDiv = document.createElement('div');
msgDiv.classList.add('message', sender);
msgDiv.textContent = text;
chatMessages.appendChild(msgDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
};
if (chatForm) {
chatForm.addEventListener('submit', async (e) => {
e.preventDefault();
const message = chatInput.value.trim();
if (!message) return;
appendMessage(message, 'visitor');
chatInput.value = '';
try {
const response = await fetch('api/chat.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message })
});
const data = await response.json();
// Artificial delay for realism
setTimeout(() => {
appendMessage(data.reply, 'bot');
}, 500);
} catch (error) {
console.error('Error:', error);
appendMessage("Désolé, une erreur est survenue. Veuillez réessayer.", 'bot');
}
});
}
});
async function loadProfile(userId) {
const modal = document.getElementById('profileModal');
const content = document.getElementById('profileModalContent');
if (!modal || !content) return;
try {
const response = await fetch('api/get_profile.php?user_id=' + userId);
const data = await response.json();
if (data.html) {
content.innerHTML = data.html;
modal.style.display = 'flex';
}
} catch (error) {
console.error('Error loading profile:', error);
}
}