-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbox.php
More file actions
379 lines (320 loc) · 12.2 KB
/
Copy pathchatbox.php
File metadata and controls
379 lines (320 loc) · 12.2 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<!-- Chat Icon -->
<div class="chat-icon" onclick="toggleChatbox()">
<i class="fa-solid fa-message"></i>
<!-- Red Dot Notification -->
<span class="red-dot" id="redDotNotification" style="display: none;"></span>
</div>
<style>
/* Red dot notification */
.red-dot {
position: absolute;
top: -1px;
right: -1px;
width: 12px;
height: 12px;
background-color: red;
border-radius: 50%;
z-index: 10;
}
</style>
<!-- Chatbox Container -->
<div class="chatbox-container" id="chatbox" style="display: none;">
<div class="chatbox-header">
<i class="fa-brands fa-web-awesome"></i>
<h3>R O Y A L E </h3>
<button class="chatbox-close" onclick="toggleChatbox()">X</button>
</div>
<div class="chatbox-body">
<em style="font-size:1rem; padding-left: 10px;">use this chat box for important concerns only</em>
<div class="messages">
<!-- Messages will go here -->
</div>
</div>
<div class="chatbox-footer">
<input type="text" class="chatbox-input" placeholder="Type your message..." />
<button class="chatbox-send">Send</button>
</div>
</div>
<style>
/* Chat Icon */
.chat-icon {
position: fixed;
bottom: 30px;
right: 30px;
background-color: var(--first-bgcolor);
border-radius: 50%;
padding: 13px 12px 9px 13px;
cursor: pointer;
border: var(--box-shadow) 1px solid;
box-shadow: 0 2px 4px var(--hover-color);
transition: transform 0.3s ease;
/* Smooth scaling transition */
color: var(--text-color);
font-size: 3rem;
z-index: 999;
}
/* Chatbox Container */
.chatbox-container {
width: 300px;
height: 400px;
background-color: var(--first-bgcolor);
border: 1px solid var(--box-shadow);
border-radius: 8px;
display: flex;
flex-direction: column;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
position: fixed;
bottom: 70px;
right: 20px;
z-index: 1000;
display: none;
/* Initially hidden */
opacity: 0;
transform: translateY(20px);
/* Start with a slight translation */
transition: opacity 0.3s ease, transform 0.3s ease;
/* Smooth transition */
}
.chatbox-container.show {
opacity: 1;
transform: translateY(0);
/* Animate to the original position */
}
.chatbox-header {
background-color: var(--text-color);
color: var(--first-bgcolor);
padding: 10px;
text-align: center;
border-top-left-radius: 8px;
border-top-right-radius: 8px;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 1.5rem;
}
.chatbox-header h3 {
margin: 0;
font-weight: bold;
}
.chatbox-close {
background-color: transparent;
color: var(--first-bgcolor);
border: none;
font-size: 18px;
cursor: pointer;
}
.chatbox-body {
flex-grow: 1;
overflow-y: auto;
background-color: var(--first-bgcolor);
}
/* Messages container */
.messages {
display: flex;
flex-direction: column;
gap: 10px;
padding: 10px;
max-height: 300px;
overflow-y: auto;
background-color: var(--first-bgcolor);
scrollbar-width: thin;
scrollbar-color: var(--box-shadow) var(--first-bgcolor);
}
/* Scrollbar styling */
.messages::-webkit-scrollbar {
width: 8px;
}
.messages::-webkit-scrollbar-thumb {
background-color: var(--box-shadow);
border-radius: 4px;
}
.messages::-webkit-scrollbar-track {
background-color: var(--first-bgcolor);
}
/* User's message (align right) */
.message.user-message {
align-self: flex-end;
background-color: #001C31;
/* Light green for user */
color: white;
padding: 8px 12px;
border-radius: 15px 15px 0 15px;
max-width: 70%;
font-size: 1.5rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
border: 1px solid var(--box-shadow);
}
/* Admin's message (align left) */
.message.admin-message {
align-self: flex-start;
background-color: #f1f1f1;
/* Light gray for admin */
color: #333;
padding: 8px 12px;
border-radius: 15px 15px 15px 0;
max-width: 70%;
font-size: 1.5rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
border: 1px solid var(--box-shadow);
}
.chatbox-footer {
padding: 10px;
display: flex;
justify-content: space-between;
background-color: #f1f1f1;
border-bottom-left-radius: 8px;
border-bottom-right-radius: 8px;
align-items: center;
}
.chatbox-input {
width: 80%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
.chatbox-send {
width: 15%;
padding: 8px;
background-color: #001C31;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.chatbox-send:hover {
background-color: #003f6f;
}
.message-timestamp {
font-size: 0.8rem;
color:gray;
text-align: right;
margin-top: 5px;
}
</style>
<script>
const userId = <?php echo isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 'null'; ?>;
function loadMessages(userId) {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'fetch_messages.php?user_id=' + userId, true);
xhr.onload = function() {
if (xhr.status === 200) {
const messages = JSON.parse(xhr.responseText);
const messagesContainer = document.querySelector('.messages');
messagesContainer.innerHTML = ''; // Clear the container before adding new messages
let lastMessageFromAdmin = false; // Flag to check if the last message is from admin
messages.forEach(function(message) {
const messageDiv = document.createElement('div');
messageDiv.classList.add('message');
// Apply message class based on admin_id
if (message.is_admin) {
messageDiv.classList.add('admin-message');
lastMessageFromAdmin = true; // Set flag if it's an admin's message
} else {
messageDiv.classList.add('user-message');
}
// Create message text
const messageText = document.createElement('div');
messageText.classList.add('message-text');
messageText.textContent = message.message;
messageDiv.appendChild(messageText);
// Create timestamp div
const timestampDiv = document.createElement('div');
timestampDiv.classList.add('message-timestamp');
timestampDiv.textContent = formatTimestamp(message.timestamp); // Format and display timestamp
messageDiv.appendChild(timestampDiv);
// Append messageDiv to container
messagesContainer.appendChild(messageDiv);
});
// Scroll to the bottom of the chat
messagesContainer.scrollTop = messagesContainer.scrollHeight;
// Show or hide the red dot based on the last message
const redDot = document.getElementById('redDotNotification');
if (lastMessageFromAdmin) {
if (!sessionStorage.getItem('hasNewAdminMessage')) {
// If it's a new admin message and not viewed, show the red dot
redDot.style.display = 'block';
sessionStorage.setItem('hasNewAdminMessage', 'true'); // Mark it as viewed
}
} else {
redDot.style.display = 'none'; // Hide if last message isn't from admin
}
}
};
xhr.send();
}
// Function to format the timestamp with date and time (e.g., "2024-11-21 14:35:22")
function formatTimestamp(timestamp) {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0'); // Months are zero-indexed
const day = date.getDate().toString().padStart(2, '0');
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
// Call loadMessages initially to fetch the latest chat messages when the page loads
loadMessages();
</script>
<script>
function toggleChatbox() {
const chatbox = document.getElementById('chatbox');
const chatIcon = document.querySelector('.chat-icon');
const redDot = document.getElementById('redDotNotification');
const messagesContainer = document.querySelector('.messages'); // Get the messages container
if (chatbox.style.display === 'none' || chatbox.style.display === '') {
chatbox.style.display = 'flex'; // Show the chatbox
setTimeout(() => {
chatbox.classList.add('show'); // Add animation class after display change
}, 10); // Small delay for the transition to take effect
chatIcon.style.display = 'none'; // Hide the chat icon
chatIcon.style.transform = 'scale(0)'; // Shrink the chat icon
// Clear the red dot flag when chatbox is opened
sessionStorage.removeItem('hasNewAdminMessage');
redDot.style.display = 'none'; // Hide the red dot as it's being viewed
// Scroll to the bottom when the chatbox is shown
messagesContainer.scrollTop = messagesContainer.scrollHeight;
} else {
chatbox.classList.remove('show'); // Remove the animation class to hide
setTimeout(() => {
chatbox.style.display = 'none'; // Hide the chatbox after the animation
}, 300); // Matches the transition duration
chatIcon.style.display = 'block'; // Show the chat icon again
chatIcon.style.transform = 'scale(1)'; // Reset the chat icon size
}
}
document.querySelector('.chatbox-send').addEventListener('click', function() {
const inputField = document.querySelector('.chatbox-input');
const message = inputField.value.trim();
if (!userId) {
// If the user is not logged in, show SweetAlert and return early
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Please login first to send a message!'
});
return; // Prevent further execution
}
if (message) {
// Send the message to the server using AJAX
const xhr = new XMLHttpRequest();
xhr.open('POST', 'store_message.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if (xhr.status === 200) {
// Display the new message in the chatbox
const messageDiv = document.createElement('div');
messageDiv.classList.add('message', 'user-message'); // Add 'user-message' class
messageDiv.textContent = message;
const messagesContainer = document.querySelector('.messages');
messagesContainer.appendChild(messageDiv);
// Scroll to the bottom of the chat
messagesContainer.scrollTop = messagesContainer.scrollHeight;
// Clear the input field after sending the message
inputField.value = '';
}
};
xhr.send('message=' + encodeURIComponent(message));
}
});
</script>