nano_bot / templates /messages.html
Dooratre's picture
Upload 7 files
3090b5a verified
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>نانو بوت - الرسائل</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;700;800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<style>
:root {
--primary-color: #4361ee;
--primary-light: #4895ef;
--primary-dark: #3f37c9;
--secondary-color: #4cc9f0;
--success-color: #4caf50;
--danger-color: #f72585;
--warning-color: #ff9e00;
--info-color: #3a86ff;
--light-color: #f8f9fa;
--dark-color: #212529;
--body-bg: #f0f5ff;
--card-bg: #ffffff;
--border-color: #e9ecef;
--text-color: #333;
--text-muted: #6c757d;
--shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.05);
--shadow-md: 0 5px 15px rgba(0, 0, 0, 0.07);
--shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.1);
--transition: all 0.3s ease;
--border-radius: 12px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Tajawal', sans-serif;
}
body {
background-color: var(--body-bg);
color: var(--text-color);
min-height: 100vh;
display: flex;
flex-direction: column;
}
/* Scrollbar Styling */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: #f1f1f1;
border-radius: 10px;
}
::-webkit-scrollbar-thumb {
background: var(--primary-light);
border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--primary-color);
}
/* Navigation */
.navbar {
background-color: var(--card-bg);
color: var(--text-color);
padding: 15px 20px;
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: var(--shadow-md);
position: sticky;
top: 0;
z-index: 100;
}
.navbar-brand {
display: flex;
align-items: center;
font-size: 22px;
font-weight: 700;
color: var(--primary-color);
}
.navbar-brand svg {
width: 28px;
height: 28px;
margin-left: 10px;
fill: var(--primary-color);
}
.navbar-nav {
display: flex;
align-items: center;
}
.nav-item {
margin-right: 20px;
}
.nav-link {
color: var(--text-color);
text-decoration: none;
font-weight: 500;
transition: var(--transition);
display: flex;
align-items: center;
}
.nav-link:hover {
color: var(--primary-color);
}
.nav-link svg {
margin-left: 5px;
width: 18px;
height: 18px;
}
.container {
max-width: 1200px;
width: 100%;
margin: 0 auto;
padding: 20px;
flex: 1;
}
/* Page Header */
.page-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
flex-wrap: wrap;
gap: 15px;
}
.page-title {
font-size: 24px;
font-weight: 700;
color: var(--primary-color);
display: flex;
align-items: center;
}
.page-title svg {
margin-left: 10px;
width: 24px;
height: 24px;
}
.page-actions {
display: flex;
gap: 10px;
}
/* Messages */
.messages-container {
display: grid;
grid-template-columns: 1fr;
gap: 20px;
}
@media (min-width: 768px) {
.messages-container {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 1200px) {
.messages-container {
grid-template-columns: repeat(3, 1fr);
}
}
.message-card {
background-color: var(--card-bg);
border-radius: var(--border-radius);
box-shadow: var(--shadow-sm);
transition: var(--transition);
overflow: hidden;
position: relative;
}
.message-card:hover {
box-shadow: var(--shadow-md);
transform: translateY(-3px);
}
.message-card.unread::before {
content: '';
position: absolute;
top: 15px;
left: 15px;
width: 10px;
height: 10px;
background-color: var(--primary-color);
border-radius: 50%;
}
.message-header {
padding: 15px 20px;
border-bottom: 1px solid var(--border-color);
display: flex;
align-items: center;
}
.user-avatar {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: var(--primary-light);
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: 700;
margin-left: 15px;
flex-shrink: 0;
overflow: hidden;
}
.user-avatar img {
width: 100%;
height: 100%;
object-fit: cover;
}
.user-info {
flex: 1;
}
.user-name {
font-weight: 700;
margin-bottom: 5px;
}
.message-time {
font-size: 12px;
color: var(--text-muted);
}
.message-body {
padding: 20px;
min-height: 100px;
}
.message-text {
line-height: 1.6;
margin-bottom: 15px;
}
.message-footer {
padding: 15px 20px;
border-top: 1px solid var(--border-color);
display: flex;
justify-content: space-between;
align-items: center;
}
.message-actions {
display: flex;
gap: 10px;
}
.btn {
display: inline-block;
padding: 8px 16px;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 500;
cursor: pointer;
transition: var(--transition);
text-align: center;
text-decoration: none;
}
.btn-primary {
background-color: var(--primary-color);
color: white;
}
.btn-primary:hover {
background-color: var(--primary-dark);
}
.btn-danger {
background-color: var(--danger-color);
color: white;
}
.btn-danger:hover {
background-color: #d61a6c;
}
.btn-outline {
background-color: transparent;
border: 1px solid var(--border-color);
color: var(--text-color);
}
.btn-outline:hover {
background-color: var(--light-color);
}
.btn-sm {
padding: 6px 12px;
font-size: 12px;
}
.btn svg {
width: 16px;
height: 16px;
margin-left: 5px;
vertical-align: middle;
}
/* Empty State */
.empty-state {
text-align: center;
padding: 50px 20px;
background-color: var(--card-bg);
border-radius: var(--border-radius);
box-shadow: var(--shadow-sm);
}
.empty-icon {
width: 80px;
height: 80px;
margin: 0 auto 20px;
color: var(--text-muted);
}
.empty-title {
font-size: 20px;
font-weight: 700;
margin-bottom: 10px;
color: var(--text-color);
}
.empty-description {
color: var(--text-muted);
margin-bottom: 20px;
}
/* Loading */
.loading-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(255, 255, 255, 0.8);
display: flex;
justify-content: center;
align-items: center;
z-index: 2000;
opacity: 0;
visibility: hidden;
transition: var(--transition);
}
.loading-overlay.active {
opacity: 1;
visibility: visible;
}
.spinner {
width: 50px;
height: 50px;
border: 5px solid rgba(67, 97, 238, 0.2);
border-radius: 50%;
border-top-color: var(--primary-color);
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* Notifications */
.notification-container {
position: fixed;
top: 20px;
left: 20px;
right: 20px;
z-index: 1001;
display: flex;
flex-direction: column;
align-items: center;
pointer-events: none;
}
.notification {
background-color: var(--card-bg);
border-radius: 8px;
box-shadow: var(--shadow-md);
padding: 15px 20px;
margin-bottom: 10px;
display: flex;
align-items: center;
width: 100%;
max-width: 400px;
transform: translateY(-20px);
opacity: 0;
transition: all 0.3s ease;
pointer-events: auto;
}
.notification.show {
transform: translateY(0);
opacity: 1;
}
.notification-icon {
width: 24px;
height: 24px;
margin-left: 15px;
flex-shrink: 0;
}
.notification-content {
flex: 1;
}
.notification-title {
font-weight: 700;
margin-bottom: 5px;
}
.notification-message {
font-size: 14px;
color: var(--text-muted);
}
.notification-close {
background: none;
border: none;
cursor: pointer;
color: var(--text-muted);
margin-right: -5px;
}
.notification.success .notification-icon {
color: var(--success-color);
}
.notification.error .notification-icon {
color: var(--danger-color);
}
.notification.info .notification-icon {
color: var(--info-color);
}
/* Responsive Adjustments */
@media (max-width: 768px) {
.navbar {
padding: 10px 15px;
}
.navbar-brand {
font-size: 18px;
}
.container {
padding: 15px;
}
.page-title {
font-size: 20px;
}
.message-header {
padding: 12px 15px;
}
.message-body {
padding: 15px;
}
.message-footer {
padding: 12px 15px;
}
}
.message-time {
font-size: 12px;
color: var(--text-muted);
display: flex;
align-items: center;
gap: 5px;
}
.seen-status {
color: var(--text-muted);
}
.seen-status.new {
color: var(--primary-color);
font-weight: 500;
}
.navbar .logo-icon {
width: 40px;
height: 40px;
background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 5px 15px rgba(67, 97, 238, 0.2);
position: relative;
overflow: hidden;
margin-left: 10px;
}
.navbar .logo-icon::before {
content: '';
position: absolute;
top: -10px;
left: -10px;
right: -10px;
bottom: -10px;
background: linear-gradient(135deg, rgba(255,255,255,0.2), rgba(255,255,255,0));
transform: rotate(35deg);
}
.navbar .logo-icon i {
color: white;
font-size: 20px;
position: relative;
z-index: 2;
}
.navbar .logo-text h1 {
font-size: 18px;
font-weight: 800;
margin: 0;
}
.navbar .logo-text p {
font-size: 12px;
margin: 0;
}
.navbar-brand {
display: flex;
align-items: center;
}
</style>
</head>
<body>
<!-- Loading Overlay -->
<div class="loading-overlay" id="loadingOverlay">
<div class="spinner"></div>
</div>
<!-- Notification Container -->
<div class="notification-container" id="notificationContainer"></div>
<!-- Navbar -->
<nav class="navbar">
<div class="navbar-brand">
<div class="logo-icon">
<i class="fas fa-robot"></i>
</div>
<div class="logo-text">
<h1>NANO BOT</h1>
</div>
</div>
<div class="navbar-nav">
<div class="nav-item">
<a href="{{ url_for('dashboard') }}" class="nav-link">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M4 13h6a1 1 0 0 0 1-1V4a1 1 0 0 0-1-1H4a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1zm-1 7a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1v-4a1 1 0 0 0-1-1H4a1 1 0 0 0-1 1v4zm10 0a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1v-7a1 1 0 0 0-1-1h-6a1 1 0 0 0-1 1v7zm1-10h6a1 1 0 0 0 1-1V4a1 1 0 0 0-1-1h-6a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1z"/>
</svg>
لوحة التحكم
</a>
</div>
<div class="nav-item">
<a href="{{ url_for('logout') }}" class="nav-link">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M16 13v-2H7V8l-5 4 5 4v-3z"/>
<path d="M20 3h-9c-1.103 0-2 .897-2 2v4h2V5h9v14h-9v-4H9v4c0 1.103.897 2 2 2h9c1.103 0 2-.897 2-2V5c0-1.103-.897-2-2-2z"/>
</svg>
تسجيل الخروج
</a>
</div>
</div>
</nav>
<!-- Main Container -->
<div class="container">
<div class="page-header">
<h1 class="page-title">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M20 4H4c-1.103 0-2 .897-2 2v12c0 1.103.897 2 2 2h16c1.103 0 2-.897 2-2V6c0-1.103-.897-2-2-2zm0 2v.511l-8 6.223-8-6.222V6h16zM4 18V9.044l7.386 5.745a.994.994 0 0 0 1.228 0L20 9.044 20.002 18H4z"/>
</svg>
رسائل الإدارة
</h1>
<div class="page-actions">
<button class="btn btn-primary" id="refreshBtn">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M10 11H7.101l.001-.009a4.956 4.956 0 0 1 .752-1.787 5.054 5.054 0 0 1 2.2-1.811c.302-.128.617-.226.938-.291a5.078 5.078 0 0 1 2.018 0 4.978 4.978 0 0 1 2.525 1.361l1.416-1.412a7.036 7.036 0 0 0-2.224-1.501 6.921 6.921 0 0 0-1.315-.408 7.079 7.079 0 0 0-2.819 0 6.94 6.94 0 0 0-1.316.409 7.04 7.04 0 0 0-3.08 2.534 6.978 6.978 0 0 0-1.054 2.505c-.028.135-.043.273-.063.41H2l4 4 4-4zm4 2h2.899l-.001.008a4.976 4.976 0 0 1-2.103 3.138 4.943 4.943 0 0 1-1.787.752 5.073 5.073 0 0 1-2.017 0 4.956 4.956 0 0 1-1.787-.752 5.072 5.072 0 0 1-.74-.61L7.05 16.95a7.032 7.032 0 0 0 2.225 1.5c.424.18.867.317 1.315.408a7.07 7.07 0 0 0 2.818 0 7.031 7.031 0 0 0 4.395-2.945 6.974 6.974 0 0 0 1.053-2.503c.027-.135.043-.273.063-.41H22l-4-4-4 4z"/>
</svg>
تحديث
</button>
</div>
</div>
{% if messages|length > 0 %}
<div class="messages-container">
{% for message in messages %}
<div class="message-card {% if 'seen' not in message %}unread{% endif %}" data-index="{{ loop.index0 }}">
<div class="message-header">
<div class="user-avatar">
{{ message.full_name[0] }}
</div>
<div class="user-info">
<div class="user-name">{{ message.full_name }}</div>
<div class="message-time">
{% if message.timestamp %}
{% set timestamp = message.timestamp.split('T') %}
<span>{{ timestamp[0] }} {{ timestamp[1].split('.')[0] }}</span>
{% if 'seen' in message %}
<span class="seen-status">• تم القراءة</span>
{% else %}
<span class="seen-status new">• جديد</span>
{% endif %}
{% else %}
{% if 'seen' in message %}
<span>تم القراءة</span>
{% else %}
<span>جديد</span>
{% endif %}
{% endif %}
</div>
</div>
</div>
<div class="message-body">
<div class="message-text">{{ message.message }}</div>
</div>
<div class="message-footer">
<div class="message-actions">
{% if 'seen' not in message %}
<button class="btn btn-outline btn-sm mark-seen-btn" data-index="{{ loop.index0 }}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/>
</svg>
تعليم كمقروء
</button>
{% endif %}
</div>
<button class="btn btn-danger btn-sm delete-btn" data-index="{{ loop.index0 }}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M5 20a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8h2V6h-4V4a2 2 0 0 0-2-2H9a2 2 0 0 0-2 2v2H3v2h2zM9 4h6v2H9zM8 8h9v12H7V8z"/>
<path d="M9 10h2v8H9zm4 0h2v8h-2z"/>
</svg>
حذف
</button>
</div>
</div>
{% endfor %}
</div>
{% else %}
<div class="empty-state">
<svg class="empty-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M20 4H4c-1.103 0-2 .897-2 2v12c0 1.103.897 2 2 2h16c1.103 0 2-.897 2-2V6c0-1.103-.897-2-2-2zm0 2v.511l-8 6.223-8-6.222V6h16zM4 18V9.044l7.386 5.745a.994.994 0 0 0 1.228 0L20 9.044 20.002 18H4z"/>
</svg>
<h3 class="empty-title">لا توجد رسائل</h3>
<p class="empty-description">لم يتم العثور على أي رسائل من العملاء في الوقت الحالي.</p>
<button class="btn btn-primary" id="emptyRefreshBtn">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
<path d="M10 11H7.101l.001-.009a4.956 4.956 0 0 1 .752-1.787 5.054 5.054 0 0 1 2.2-1.811c.302-.128.617-.226.938-.291a5.078 5.078 0 0 1 2.018 0 4.978 4.978 0 0 1 2.525 1.361l1.416-1.412a7.036 7.036 0 0 0-2.224-1.501 6.921 6.921 0 0 0-1.315-.408 7.079 7.079 0 0 0-2.819 0 6.94 6.94 0 0 0-1.316.409 7.04 7.04 0 0 0-3.08 2.534 6.978 6.978 0 0 0-1.054 2.505c-.028.135-.043.273-.063.41H2l4 4 4-4zm4 2h2.899l-.001.008a4.976 4.976 0 0 1-2.103 3.138 4.943 4.943 0 0 1-1.787.752 5.073 5.073 0 0 1-2.017 0 4.956 4.956 0 0 1-1.787-.752 5.072 5.072 0 0 1-.74-.61L7.05 16.95a7.032 7.032 0 0 0 2.225 1.5c.424.18.867.317 1.315.408a7.07 7.07 0 0 0 2.818 0 7.031 7.031 0 0 0 4.395-2.945 6.974 6.974 0 0 0 1.053-2.503c.027-.135.043-.273.063-.41H22l-4-4-4 4z"/>
</svg>
تحديث
</button>
</div>
{% endif %}
</div>
<!-- Confirmation Dialog -->
<div class="confirmation-dialog" id="confirmationDialog">
<div class="confirmation-content">
<div class="confirmation-header">
<h3>تأكيد الحذف</h3>
<button class="close-btn" id="closeConfirmBtn">&times;</button>
</div>
<div class="confirmation-body">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="confirmation-icon">
<path d="M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486 10-10S17.514 2 12 2zm0 18c-4.411 0-8-3.589-8-8s3.589-8 8-8 8 3.589 8 8-3.589 8-8 8z"/>
<path d="M13 7h-2v6h2zm0 8h-2v2h2z"/>
</svg>
<p>هل أنت متأكد من حذف هذه الرسالة؟</p>
<p class="confirmation-note">لا يمكن التراجع عن هذا الإجراء.</p>
</div>
<div class="confirmation-footer">
<button class="btn btn-outline" id="cancelDeleteBtn">إلغاء</button>
<button class="btn btn-danger" id="confirmDeleteBtn">تأكيد الحذف</button>
</div>
</div>
</div>
<style>
/* Confirmation Dialog */
.confirmation-dialog {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 2000;
opacity: 0;
visibility: hidden;
transition: var(--transition);
}
.confirmation-dialog.active {
opacity: 1;
visibility: visible;
}
.confirmation-content {
background-color: var(--card-bg);
border-radius: var(--border-radius);
box-shadow: var(--shadow-lg);
width: 90%;
max-width: 400px;
transform: translateY(-20px);
transition: transform 0.3s ease;
}
.confirmation-dialog.active .confirmation-content {
transform: translateY(0);
}
.confirmation-header {
padding: 15px 20px;
border-bottom: 1px solid var(--border-color);
display: flex;
justify-content: space-between;
align-items: center;
}
.confirmation-header h3 {
margin: 0;
color: var(--danger-color);
font-weight: 700;
}
.close-btn {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
color: var(--text-muted);
}
.confirmation-body {
padding: 20px;
text-align: center;
}
.confirmation-icon {
width: 60px;
height: 60px;
margin-bottom: 15px;
color: var(--danger-color);
}
.confirmation-note {
font-size: 14px;
color: var(--text-muted);
margin-top: 10px;
}
.confirmation-footer {
padding: 15px 20px;
border-top: 1px solid var(--border-color);
display: flex;
justify-content: flex-end;
gap: 10px;
}
</style>
<script>
// DOM Elements
const loadingOverlay = document.getElementById('loadingOverlay');
const notificationContainer = document.getElementById('notificationContainer');
const refreshBtn = document.getElementById('refreshBtn');
const emptyRefreshBtn = document.getElementById('emptyRefreshBtn');
const markSeenBtns = document.querySelectorAll('.mark-seen-btn');
const deleteBtns = document.querySelectorAll('.delete-btn');
let confirmationDialog;
let closeConfirmBtn;
let cancelDeleteBtn;
let confirmDeleteBtn;
let messageToDelete = null;
// WebSocket Connection
let socket;
let recentActions = new Set(); // Track recent actions to prevent duplicate notifications
function connectWebSocket() {
socket = io.connect(window.location.origin);
socket.on('connect', function() {
console.log('WebSocket connected');
});
socket.on('message_seen', function(data) {
const actionKey = `seen_${data.page_id}_${data.index}`;
// Only show notification if this wasn't a local action
if (!recentActions.has(actionKey)) {
showNotification('تم تحديث الحالة', 'تم تعليم الرسالة كمقروءة', 'success');
// Update UI if needed
const messageCard = document.querySelector(`.message-card[data-index="${data.index}"]`);
if (messageCard) {
messageCard.classList.remove('unread');
const markSeenBtn = messageCard.querySelector('.mark-seen-btn');
if (markSeenBtn) {
markSeenBtn.remove();
}
const seenStatus = messageCard.querySelector('.seen-status');
if (seenStatus) {
seenStatus.textContent = '• تم القراءة';
seenStatus.classList.remove('new');
}
}
}
});
socket.on('message_deleted', function(data) {
const actionKey = `delete_${data.page_id}_${data.index}`;
// Only show notification if this wasn't a local action
if (!recentActions.has(actionKey)) {
showNotification('تم الحذف', 'تم حذف الرسالة بنجاح', 'success');
// Remove message card from UI if needed
const messageCard = document.querySelector(`.message-card[data-index="${data.index}"]`);
if (messageCard) {
messageCard.style.opacity = '0';
messageCard.style.transform = 'translateY(20px)';
setTimeout(() => {
messageCard.remove();
// Check if there are no more messages
const remainingMessages = document.querySelectorAll('.message-card');
if (remainingMessages.length === 0) {
// Reload the page to show empty state
window.location.reload();
}
}, 300);
}
}
});
socket.on('data_refreshed', function(data) {
const actionKey = `refresh_${data.type}`;
// Only show notification if this wasn't a local action
if (!recentActions.has(actionKey)) {
showNotification('تم التحديث', 'تم تحديث الرسائل بنجاح', 'info');
setTimeout(() => {
window.location.reload();
}, 1000);
}
});
}
// Helper function to track recent actions
function trackAction(actionType, pageId, index) {
const actionKey = `${actionType}_${pageId}_${index}`;
recentActions.add(actionKey);
// Remove from tracking after 5 seconds
setTimeout(() => {
recentActions.delete(actionKey);
}, 5000);
return actionKey;
}
// Initialize
document.addEventListener('DOMContentLoaded', function() {
// Connect WebSocket
connectWebSocket();
// Get confirmation dialog elements
confirmationDialog = document.getElementById('confirmationDialog');
closeConfirmBtn = document.getElementById('closeConfirmBtn');
cancelDeleteBtn = document.getElementById('cancelDeleteBtn');
confirmDeleteBtn = document.getElementById('confirmDeleteBtn');
// Set up confirmation dialog buttons
closeConfirmBtn.addEventListener('click', closeConfirmation);
cancelDeleteBtn.addEventListener('click', closeConfirmation);
confirmDeleteBtn.addEventListener('click', function() {
if (messageToDelete !== null) {
proceedWithDelete(messageToDelete);
closeConfirmation();
}
});
// Refresh Button
if (refreshBtn) {
refreshBtn.addEventListener('click', function() {
refreshMessages();
});
}
// Empty Refresh Button
if (emptyRefreshBtn) {
emptyRefreshBtn.addEventListener('click', function() {
refreshMessages();
});
}
// Mark Seen Buttons
markSeenBtns.forEach(btn => {
btn.addEventListener('click', function() {
const index = this.getAttribute('data-index');
markMessageAsSeen(index);
});
});
// Delete Buttons
deleteBtns.forEach(btn => {
btn.addEventListener('click', function() {
const index = this.getAttribute('data-index');
showDeleteConfirmation(index);
});
});
});
// Functions
function showLoading() {
loadingOverlay.classList.add('active');
}
function hideLoading() {
loadingOverlay.classList.remove('active');
}
function showNotification(title, message, type = 'info') {
const notification = document.createElement('div');
notification.className = `notification ${type}`;
let iconSvg;
if (type === 'success') {
iconSvg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="notification-icon">
<path d="M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486 10-10S17.514 2 12 2zm0 18c-4.411 0-8-3.589-8-8s3.589-8 8-8 8 3.589 8 8-3.589 8-8 8z"/>
<path d="M9.999 13.587 7.7 11.292l-1.412 1.416 3.713 3.705 6.706-6.706-1.414-1.414z"/>
</svg>`;
} else if (type === 'error') {
iconSvg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="notification-icon">
<path d="M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486 10-10S17.514 2 12 2zm0 18c-4.411 0-8-3.589-8-8s3.589-8 8-8 8 3.589 8 8-3.589 8-8 8z"/>
<path d="M13 7h-2v7h2zm0 8h-2v2h2z"/>
</svg>`;
} else {
iconSvg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="notification-icon">
<path d="M12 2C6.486 2 2 6.486 2 12s4.486 10 10 10 10-4.486 10-10S17.514 2 12 2zm0 18c-4.411 0-8-3.589-8-8s3.589-8 8-8 8 3.589 8 8-3.589 8-8 8z"/>
<path d="M11 11h2v6h-2zm0-4h2v2h-2z"/>
</svg>`;
}
notification.innerHTML = `
${iconSvg}
<div class="notification-content">
<div class="notification-title">${title}</div>
<div class="notification-message">${message}</div>
</div>
<button class="notification-close">&times;</button>
`;
notificationContainer.appendChild(notification);
// Show notification with animation
setTimeout(() => {
notification.classList.add('show');
}, 10);
// Auto close after 5 seconds
setTimeout(() => {
closeNotification(notification);
}, 5000);
// Close button
notification.querySelector('.notification-close').addEventListener('click', function() {
closeNotification(notification);
});
}
function closeNotification(notification) {
notification.classList.remove('show');
setTimeout(() => {
notification.remove();
}, 300);
}
function refreshMessages() {
// Track this action to prevent duplicate notifications
trackAction('refresh', 'messages', null);
showLoading();
fetch('/api/refresh_data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ type: 'messages' }),
})
.then(response => response.json())
.then(data => {
hideLoading();
if (data.success) {
showNotification('تم التحديث', 'تم تحديث الرسائل بنجاح', 'success');
setTimeout(() => {
window.location.reload();
}, 1500);
} else {
showNotification('خطأ', data.message, 'error');
}
})
.catch(error => {
hideLoading();
showNotification('خطأ', 'حدث خطأ أثناء تحديث الرسائل', 'error');
console.error('Error:', error);
});
}
function markMessageAsSeen(index) {
// Ensure index is a number
index = parseInt(index, 10);
// Track this action to prevent duplicate notifications
const pageId = '{{ page.page_id }}'; // Get page_id from template
trackAction('seen', pageId, index);
showLoading();
fetch('/api/mark_message_seen', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ index: index }),
})
.then(response => response.json())
.then(data => {
hideLoading();
if (data.success) {
showNotification('تم التحديث', 'تم تعليم الرسالة كمقروءة', 'success');
// Update UI
const messageCard = document.querySelector(`.message-card[data-index="${index}"]`);
if (messageCard) {
messageCard.classList.remove('unread'); // Remove unread class
const markSeenBtn = messageCard.querySelector('.mark-seen-btn');
if (markSeenBtn) {
markSeenBtn.remove(); // Remove the "Mark as Seen" button
}
const seenStatus = messageCard.querySelector('.seen-status');
if (seenStatus) {
seenStatus.textContent = '• تم القراءة';
seenStatus.classList.remove('new');
}
}
} else {
showNotification('خطأ', data.message, 'error');
}
})
.catch(error => {
hideLoading();
showNotification('خطأ', 'حدث خطأ أثناء تعليم الرسالة كمقروءة', 'error');
console.error('Error:', error);
});
}
function showDeleteConfirmation(index) {
messageToDelete = index;
confirmationDialog.classList.add('active');
// Prevent scrolling on the body
document.body.style.overflow = 'hidden';
}
function closeConfirmation() {
confirmationDialog.classList.remove('active');
// Re-enable scrolling
document.body.style.overflow = '';
// Reset the message to delete
setTimeout(() => {
messageToDelete = null;
}, 300);
}
function proceedWithDelete(index) {
// Track this action to prevent duplicate notifications
const pageId = '{{ page.page_id }}'; // Get page_id from template
trackAction('delete', pageId, index);
showLoading();
fetch('/api/delete_message', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ index: index }),
})
.then(response => response.json())
.then(data => {
hideLoading();
if (data.success) {
showNotification('تم الحذف', 'تم حذف الرسالة بنجاح', 'success');
// Remove message card from UI
const messageCard = document.querySelector(`.message-card[data-index="${index}"]`);
if (messageCard) {
messageCard.style.opacity = '0';
messageCard.style.transform = 'translateY(20px)';
setTimeout(() => {
messageCard.remove();
// Check if there are no more messages
const remainingMessages = document.querySelectorAll('.message-card');
if (remainingMessages.length === 0) {
// Reload the page to show empty state
window.location.reload();
}
}, 300);
}
} else {
showNotification('خطأ', data.message, 'error');
}
})
.catch(error => {
hideLoading();
showNotification('خطأ', 'حدث خطأ أثناء حذف الرسالة', 'error');
console.error('Error:', error);
});
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
</body>
</html>