(function initChat() {
if (document.getElementById('horeca-chat-btn')) return;
const chatHTML = `
π¬
Π‘Π»ΡΠΆΠ±Π° ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΠΈ
`;
document.body.insertAdjacentHTML('beforeend', chatHTML);
const btn = document.getElementById('horeca-chat-btn');
const win = document.getElementById('horeca-chat-window');
const input = document.getElementById('horeca-chat-input');
const sendBtn = document.getElementById('horeca-chat-send');
const msgs = document.getElementById('horeca-chat-messages');
btn.onclick = () => {
win.style.display = win.style.display === 'none' ? 'flex' : 'none';
};
const addMsg = (text, isClient) => {
const align = isClient ? 'align-self:flex-end; background:#F1F5F9; color:#0288d1;' : 'align-self:flex-start; background:#fff; border:1px solid #ddd; color:#333;';
msgs.innerHTML += `${text}
`;
msgs.scrollTop = msgs.scrollHeight;
};
const script = document.createElement('script');
script.src = "https://cdn.socket.io/4.7.2/socket.io.min.js";
document.head.appendChild(script);
script.onload = () => {
// ΠΠΎΡΡΠ°Π΅ΠΌ ΡΠΎΠΊΠ΅Π½ ΠΈΠ· Ρ
ΡΠ°Π½ΠΈΠ»ΠΈΡΠ° Π±ΡΠ°ΡΠ·Π΅ΡΠ°
const savedToken = localStorage.getItem('token');
// ΠΠΎΠ΄ΠΊΠ»ΡΡΠ°Π΅ΠΌΡΡ Ρ ΠΏΡΠ΅Π΄ΡΡΠ²Π»Π΅Π½ΠΈΠ΅ΠΌ ΡΠΎΠΊΠ΅Π½Π°
const socket = io({
path: '/api/socket.io',
auth: { token: savedToken }
});
const sendMessage = () => {
if (!input.value.trim()) return;
addMsg(input.value, true);
socket.emit('client_message', input.value);
input.value = '';
};
sendBtn.onclick = sendMessage;
input.onkeypress = (e) => { if(e.key === 'Enter') sendMessage(); };
socket.on('server_message', (text) => addMsg(text, false));
};
})();