| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656 |
- {% extends "layouts/base.html" %}
- {% block styles %}
- <style>
- .notification-fade-in {
- animation: fadeIn 0.3s ease-in-out;
- }
-
- @keyframes fadeIn {
- from { opacity: 0; transform: translateY(-10px); }
- to { opacity: 1; transform: translateY(0); }
- }
-
- .notification-item {
- transition: all 0.2s ease;
- }
-
- .notification-read {
- opacity: 0.7;
- background-color: #f9fafb;
- }
-
- .notification-removing {
- opacity: 0;
- transform: translateX(-100%);
- transition: all 0.3s ease;
- }
-
- .pagination-btn:disabled {
- opacity: 0.5;
- cursor: not-allowed;
- }
- </style>
- {% endblock %}
- {% block header_actions %}
- <div class="flex flex-col sm:flex-row items-stretch sm:items-center gap-2 w-full sm:w-auto">
- <!-- Counter -->
- <div class="bg-blue-50 px-3 py-1.5 rounded-lg border border-blue-200 flex-shrink-0 text-center">
- <span class="text-xs sm:text-sm text-blue-700 font-medium">
- <span id="unread-count">{{ unread_count }}</span> {{ _("No leídas") }}
- </span>
- </div>
-
- <!-- Mark all as read button -->
- <button id="mark-all-read" class="bg-green-600 hover:bg-green-700 text-white px-3 sm:px-4 py-2 rounded-lg transition-colors duration-200 flex items-center justify-center gap-2 text-sm flex-1 sm:flex-none">
- <i class="fas fa-check-double"></i>
- <span class="hidden xs:inline">{{ _("Marcar todas") }}</span>
- </button>
- <!-- Delete all button -->
- <button id="delete-all" class="bg-red-600 hover:bg-red-700 text-white px-3 sm:px-4 py-2 rounded-lg transition-colors duration-200 flex items-center justify-center gap-2 text-sm flex-1 sm:flex-none">
- <i class="fas fa-trash"></i>
- <span class="hidden xs:inline">{{ _("Eliminar todas") }}</span>
- </button>
- </div>
- {% endblock %}
- {% block content %}
- <div class="w-full space-y-6">
- <!-- Filters -->
- <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
- <div class="flex flex-wrap gap-4 items-center">
- <div class="flex items-center gap-2">
- <span class="text-sm font-medium text-gray-700">{{ _("Filtrar por:") }}</span>
- <select id="filter-type" class="text-sm border border-gray-300 rounded-lg px-3 py-1 focus:outline-none focus:ring-2 focus:ring-blue-500 min-w-[140px]">
- <option value="all">{{ _("Todos los tipos") }}</option>
- <option value="error">{{ _("Errores") }}</option>
- <option value="warning">{{ _("Advertencias") }}</option>
- <option value="success">{{ _("Éxitos") }}</option>
- <option value="info">{{ _("Información") }}</option>
- </select>
- </div>
-
- <div class="flex items-center gap-2">
- <select id="filter-source" class="text-sm border border-gray-300 rounded-lg px-3 py-1 focus:outline-none focus:ring-2 focus:ring-blue-500 min-w-[150px]">
- <option value="all">{{ _("Todas las fuentes") }}</option>
- <option value="squid">{{ _("Squid") }}</option>
- <option value="system">{{ _("Sistema") }}</option>
- <option value="security">{{ _("Seguridad") }}</option>
- <option value="users">{{ _("Usuarios") }}</option>
- <option value="git">{{ _("Git") }}</option>
- </select>
- </div>
-
- <div class="flex items-center gap-2">
- <select id="filter-status" class="text-sm border border-gray-300 rounded-lg px-3 py-1 focus:outline-none focus:ring-2 focus:ring-blue-500 min-w-[155px]">
- <option value="all">{{ _("Todos los estados") }}</option>
- <option value="unread">{{ _("Sin leer") }}</option>
- <option value="read">{{ _("Leída") }}</option>
- </select>
- </div>
-
- <button id="clear-filters" class="text-sm text-gray-600 hover:text-gray-800 flex items-center gap-1">
- <i class="fas fa-times"></i>
- <span>{{ _("Limpiar filtros") }}</span>
- </button>
- </div>
- </div>
- <!-- Pagination information -->
- <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4">
- <div class="flex flex-col sm:flex-row justify-between items-center gap-4">
- <div class="text-sm text-gray-600">
- {{ _("Mostrando") }} <span id="current-range">1-{{ notifications|length }}</span> de
- <span id="total-notifications">{{ pagination.total_notifications if pagination else notifications|length }}</span> notificaciones
- </div>
-
- <div class="flex items-center gap-2">
- <button id="prev-page" class="pagination-btn px-3 py-1 bg-gray-200 rounded hover:bg-gray-300 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
- {% if not pagination or not pagination.has_prev %}disabled{% endif %}>
- <i class="fas fa-chevron-left"></i>
- </button>
- <span id="page-info" class="px-3 py-1 text-gray-700 text-sm">
- {{ _("Página") }} {{ pagination.current_page if pagination else 1 }} de {{ pagination.total_pages if pagination else 1 }}
- </span>
- <button id="next-page" class="pagination-btn px-3 py-1 bg-gray-200 rounded hover:bg-gray-300 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
- {% if not pagination or not pagination.has_next %}disabled{% endif %}>
- <i class="fas fa-chevron-right"></i>
- </button>
- </div>
- </div>
- </div>
- <!-- Notifications List -->
- <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
- {% if notifications %}
- <div id="notifications-list">
- {% for notification in notifications %}
- <div class="notification-item border-b border-gray-100 last:border-b-0 {% if notification.read %}notification-read{% endif %}"
- data-id="{{ notification.id }}"
- data-type="{{ notification.type }}"
- data-source="{{ notification.source }}"
- data-read="{{ notification.read|lower }}">
- <div class="p-6 flex items-start gap-4">
- <!-- Icon -->
- <div class="flex-shrink-0">
- <div class="w-12 h-12 rounded-full flex items-center justify-center
- {% if notification.type == 'error' %}bg-red-100 text-red-600
- {% elif notification.type == 'warning' %}bg-yellow-100 text-yellow-600
- {% elif notification.type == 'success' %}bg-green-100 text-green-600
- {% else %}bg-blue-100 text-blue-600{% endif %}">
- <i class="fas {{ notification.icon }} text-lg"></i>
- </div>
- </div>
-
- <!-- Content -->
- <div class="flex-1 min-w-0">
- <div class="flex items-start justify-between mb-2">
- <div>
- <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium
- {% if notification.type == 'error' %}bg-red-100 text-red-800
- {% elif notification.type == 'warning' %}bg-yellow-100 text-yellow-800
- {% elif notification.type == 'success' %}bg-green-100 text-green-800
- {% else %}bg-blue-100 text-blue-800{% endif %}">
- {{ notification.source|upper }}
- </span>
- <span class="ml-2 text-sm text-gray-500">{{ notification.time }}</span>
- </div>
- {% if not notification.read %}
- <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
- {{ _("No leída") }}
- </span>
- {% endif %}
- </div>
-
- <p class="text-gray-900 font-medium mb-1">{{ notification.message }}</p>
-
- <div class="flex items-center gap-4 mt-3 text-sm text-gray-500">
- <span class="flex items-center gap-1">
- <i class="fas fa-clock"></i>
- <span>{{ notification.timestamp|datetime_format }}</span>
- </span>
- <span class="flex items-center gap-1">
- <i class="fas fa-{{ notification.icon }}"></i>
- <span class="capitalize">{{ notification.type }}</span>
- </span>
- </div>
- </div>
-
- <!-- Actions -->
- <div class="flex-shrink-0 flex items-center gap-2">
- {% if not notification.read %}
- <button class="mark-read-btn text-green-600 hover:text-green-800 p-2 rounded-lg hover:bg-green-50 transition-colors duration-150"
- data-id="{{ notification.id }}"
- title="{{ _("Marcar como leída") }}">
- <i class="fas fa-check"></i>
- </button>
- {% endif %}
- <button class="delete-btn text-red-600 hover:text-red-800 p-2 rounded-lg hover:bg-red-50 transition-colors duration-150"
- data-id="{{ notification.id }}"
- title="{{ _("Eliminar notificación") }}">
- <i class="fas fa-trash"></i>
- </button>
- </div>
- </div>
- </div>
- {% endfor %}
- </div>
- {% else %}
- <!-- Empty state -->
- <div class="text-center py-16">
- <div class="w-24 h-24 mx-auto mb-4 rounded-full bg-gray-100 flex items-center justify-center">
- <i class="fas fa-bell-slash text-gray-400 text-3xl"></i>
- </div>
- <h3 class="text-lg font-medium text-gray-900 mb-2">{{ _("No hay notificaciones") }}</h3>
- <p class="text-gray-500 max-w-md mx-auto">{{ _("Todas las notificaciones aparecerán aquí. ¡Mantente atento a las actualizaciones del sistema!") }}</p>
- </div>
- {% endif %}
- </div>
- </div>
- {% endblock %}
- {% block scripts %}
- <script>
- document.addEventListener('DOMContentLoaded', function() {
- const i18n = {
- errorLoadNotifications: "{{ _('Error al cargar notificaciones') }}",
- noNotifications: "{{ _('No hay notificaciones') }}",
- noNotificationsDescription: "{{ _('Todas las notificaciones aparecerán aquí. ¡Mantente atento a las actualizaciones del sistema!') }}",
- unreadLabel: "{{ _('No leída') }}",
- markAsReadTitle: "{{ _('Marcar como leída') }}",
- deleteNotificationTitle: "{{ _('Eliminar notificación') }}",
- noNotificationsUnread: "{{ _('No hay notificaciones sin leer') }}",
- confirmDeleteNotification: "{{ _('¿Estás seguro de que quieres eliminar esta notificación?') }}",
- notificationDeleted: "{{ _('Notificación eliminada') }}",
- errorDeleteNotification: "{{ _('Error al eliminar notificación:') }}",
- confirmDeleteAll: "{{ _('¿Estás seguro de que quieres eliminar TODAS las notificaciones? Esta acción no se puede deshacer.') }}",
- allNotificationsDeleted: "{{ _('Todas las notificaciones han sido eliminadas') }}",
- errorDeleteNotifications: "{{ _('Error al eliminar notificaciones:') }}",
- filtersCleared: "{{ _('Filtros limpiados') }}",
- errorConnectionPrefix: "{{ _('Error de conexión:') }}",
- errorUnknown: "{{ _('Error desconocido') }}",
- markReadSuccess: "{{ _('{count} notificación(es) marcada(s) como leída(s)') }}",
- errorMarkReadNotifications: "{{ _('Error al marcar notificaciones como leídas:') }}",
- agoJustNow: "{{ _('Hace unos momentos') }}",
- agoMinutesSingular: "{{ _('Hace {count} minuto') }}",
- agoMinutesPlural: "{{ _('Hace {count} minutos') }}",
- agoHoursSingular: "{{ _('Hace {count} hora') }}",
- agoHoursPlural: "{{ _('Hace {count} horas') }}",
- agoDaysSingular: "{{ _('Hace {count} día') }}",
- agoDaysPlural: "{{ _('Hace {count} días') }}",
- systemSource: "{{ _('Sistema') }}",
- squidSource: "{{ _('Squid') }}",
- gitSource: "{{ _('Git') }}",
- securitySource: "{{ _('Seguridad') }}",
- usersSource: "{{ _('Usuarios') }}",
- errorType: "{{ _('Error') }}",
- warningType: "{{ _('Advertencia') }}",
- successType: "{{ _('Éxito') }}",
- infoType: "{{ _('Información') }}"
- };
- const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
- let currentPage = {{ pagination.current_page if pagination else 1 }};
- const totalPages = {{ pagination.total_pages if pagination else 1 }};
- const perPage = {{ pagination.per_page if pagination else 20 }};
-
- // Función para mostrar toast usando toastr
- function showToast(message, type = 'info') {
- if (window.toastr) {
- if (type === 'error') {
- toastr.error(message);
- } else if (type === 'success') {
- toastr.success(message);
- } else if (type === 'warning') {
- toastr.warning(message);
- } else {
- toastr.info(message);
- }
- }
- }
- // Function to load notifications with pagination
- function loadNotifications(page = currentPage) {
- const url = `/api/notifications?page=${page}&per_page=${perPage}`;
-
- fetch(url)
- .then(response => response.json())
- .then(data => {
- if (data.notifications && data.notifications.length > 0) {
- renderNotifications(data.notifications);
- updatePagination(data.pagination);
- } else {
- showEmptyState();
- }
- })
- .catch(error => {
- console.error('Error loading notifications:', error);
- showToast(i18n.errorLoadNotifications || 'Error loading notifications:', 'error');
- });
- }
- // Function to render notifications
- function renderNotifications(notifications) {
- const container = document.getElementById('notifications-list');
- if (!container) return;
- const sourceMap = {
- squid: i18n.squidSource || 'Squid',
- git: i18n.gitSource || 'Git',
- system: i18n.systemSource || 'System',
- security: i18n.securitySource || 'Security',
- users: i18n.usersSource || 'Users'
- };
- const typeLabels = {
- error: i18n.errorType || 'Error',
- warning: i18n.warningType || 'Warning',
- success: i18n.successType || 'Success',
- info: i18n.infoType || 'Info'
- };
- container.innerHTML = notifications.map(notification => `
- <div class="notification-item border-b border-gray-100 last:border-b-0 ${notification.read ? 'notification-read' : ''}"
- data-id="${notification.id}"
- data-type="${notification.type}"
- data-source="${notification.source}"
- data-read="${notification.read}">
- <div class="p-6 flex items-start gap-4">
- <div class="flex-shrink-0">
- <div class="w-12 h-12 rounded-full flex items-center justify-center
- ${notification.type === 'error' ? 'bg-red-100 text-red-600' :
- notification.type === 'warning' ? 'bg-yellow-100 text-yellow-600' :
- notification.type === 'success' ? 'bg-green-100 text-green-600' :
- 'bg-blue-100 text-blue-600'}">
- <i class="fas ${notification.icon} text-lg"></i>
- </div>
- </div>
-
- <div class="flex-1 min-w-0">
- <div class="flex items-start justify-between mb-2">
- <div>
- <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium
- ${notification.type === 'error' ? 'bg-red-100 text-red-800' :
- notification.type === 'warning' ? 'bg-yellow-100 text-yellow-800' :
- notification.type === 'success' ? 'bg-green-100 text-green-800' :
- 'bg-blue-100 text-blue-800'}">
- ${(() => {
- const sourceMap = {
- squid: i18n.squidSource || 'Squid',
- git: i18n.gitSource || 'Git',
- system: i18n.systemSource || 'Sistema',
- security: i18n.securitySource || 'Seguridad',
- users: i18n.usersSource || 'Users'
- };
- return sourceMap[notification.source] || notification.source.toUpperCase();
- })()}
- </span>
- <span class="ml-2 text-sm text-gray-500">${formatTimeAgo(notification.timestamp)}</span>
- </div>
- ${!notification.read ? `
- <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
- ${i18n.unreadLabel || 'No leída'}
- </span>
- ` : ''}
- </div>
-
- <p class="text-gray-900 font-medium mb-1">${notification.message}</p>
-
- <div class="flex items-center gap-4 mt-3 text-sm text-gray-500">
- <span class="flex items-center gap-1">
- <i class="fas fa-clock"></i>
- <span>${new Date(notification.timestamp).toLocaleString('es-ES')}</span>
- </span>
- <span class="flex items-center gap-1">
- <i class="fas fa-${notification.icon}"></i>
- <span class="capitalize">${typeLabels[notification.type] || notification.type}</span>
- </span>
- </div>
- </div>
-
- <div class="flex-shrink-0 flex items-center gap-2">
- ${!notification.read ? `
- <button class="mark-read-btn text-green-600 hover:text-green-800 p-2 rounded-lg hover:bg-green-50 transition-colors duration-150"
- data-id="${notification.id}"
- title="Marcar como leída">
- <i class="fas fa-check"></i>
- </button>
- ` : ''}
- <button class="delete-btn text-red-600 hover:text-red-800 p-2 rounded-lg hover:bg-red-50 transition-colors duration-150"
- data-id="${notification.id}"
- title="${i18n.deleteNotificationTitle}">
- <i class="fas fa-trash"></i>
- </button>
- </div>
- </div>
- </div>
- `).join('');
- // Re-aplicar filtros después de cargar
- applyFilters();
- }
- // Función para actualizar la paginación
- function updatePagination(pagination) {
- currentPage = pagination.current_page;
- const totalPages = pagination.total_pages;
- const totalNotifications = pagination.total_notifications;
-
- // Actualizar información de paginación
- document.getElementById('current-range').textContent =
- `${((currentPage - 1) * perPage) + 1}-${Math.min(currentPage * perPage, totalNotifications)}`;
- document.getElementById('total-notifications').textContent = totalNotifications;
- document.getElementById('page-info').textContent = `Página ${currentPage} de ${totalPages}`;
-
- // Actualizar botones de paginación
- document.getElementById('prev-page').disabled = currentPage <= 1;
- document.getElementById('next-page').disabled = currentPage >= totalPages;
- }
- // Función para mostrar estado vacío
- function showEmptyState() {
- const container = document.getElementById('notifications-list');
- if (!container) return;
-
- container.innerHTML = `
- <div class="text-center py-16">
- <div class="w-24 h-24 mx-auto mb-4 rounded-full bg-gray-100 flex items-center justify-center">
- <i class="fas fa-bell-slash text-gray-400 text-3xl"></i>
- </div>
- <h3 class="text-lg font-medium text-gray-900 mb-2">${i18n.noNotifications || 'No hay notificaciones'}</h3>
- <p class="text-gray-500 max-w-md mx-auto">${i18n.noNotificationsDescription || 'Todas las notificaciones aparecerán aquí. ¡Mantente atento a las actualizaciones del sistema!'}</p>
- </div>
- `;
- }
- // Función para formatear "hace X tiempo"
- function formatTimeAgo(timestamp) {
- const now = new Date();
- const notificationTime = new Date(timestamp);
- const diffMs = now - notificationTime;
- const diffMins = Math.floor(diffMs / 60000);
- const diffHours = Math.floor(diffMs / 3600000);
- const diffDays = Math.floor(diffMs / 86400000);
-
- if (diffMins < 1) return i18n.agoJustNow || 'Hace unos momentos';
- if (diffMins < 60) return (diffMins === 1 ? i18n.agoMinutesSingular : i18n.agoMinutesPlural)
- .replace('{count}', diffMins);
- if (diffHours < 24) return (diffHours === 1 ? i18n.agoHoursSingular : i18n.agoHoursPlural)
- .replace('{count}', diffHours);
- return (diffDays === 1 ? i18n.agoDaysSingular : i18n.agoDaysPlural)
- .replace('{count}', diffDays);
- }
- // Function to mark notification as read
- function markAsRead(notificationIds) {
- fetch('/api/notifications/mark-read', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-CSRFToken': csrfToken,
- },
- body: JSON.stringify({
- notification_ids: notificationIds // Cambiado de 'ids' a 'notification_ids'
- })
- })
- .then(response => {
- if (!response.ok) {
- throw new Error(`Error HTTP: ${response.status}`);
- }
- return response.json();
- })
- .then(data => {
- if (data.success || data.status === 'success') {
- // Actualizar contador en el header
- document.getElementById('unread-count').textContent = data.unread_count;
-
- // Reload notifications to reflect changes
- loadNotifications(currentPage);
-
- showToast(i18n.markReadSuccess.replace('{count}', notificationIds.length), 'success');
- } else {
- showToast(`${i18n.errorMarkReadNotifications || 'Error al marcar notificaciones como leídas:'} ${data.error || i18n.errorUnknown || 'Error desconocido'}`, 'error');
- }
- })
- .catch(error => {
- console.error('Error:', error);
- showToast(`${i18n.errorConnectionPrefix || 'Error de conexión:'} ${error.message}`, 'error');
- });
- }
- // Función para marcar TODAS las notificaciones como leídas
- function markAllAsRead() {
- const unreadNotifications = document.querySelectorAll('.notification-item[data-read="false"]');
- const unreadIds = Array.from(unreadNotifications).map(el => parseInt(el.getAttribute('data-id')));
-
- if (unreadIds.length > 0) {
- markAsRead(unreadIds);
- } else {
- showToast(i18n.noNotificationsUnread, 'info');
- }
- }
- // Función para ELIMINAR notificación
- function deleteNotification(notificationId) {
- if (!confirm(i18n.confirmDeleteNotification)) {
- return;
- }
-
- fetch(`/api/notifications/${notificationId}`, {
- method: 'DELETE',
- headers: {
- 'X-CSRFToken': csrfToken,
- }
- })
- .then(response => {
- if (!response.ok) {
- throw new Error(`Error HTTP: ${response.status}`);
- }
- return response.json();
- })
- .then(data => {
- if (data.success || data.status === 'success') {
- // Recargar notificaciones
- loadNotifications(currentPage);
-
- // Actualizar contador
- document.getElementById('unread-count').textContent = data.unread_count;
-
- showToast(i18n.notificationDeleted, 'success');
- } else {
- showToast(`${i18n.errorDeleteNotification || 'Error al eliminar notificación:'} ${data.error || i18n.errorUnknown || 'Error desconocido'}`, 'error');
- }
- })
- .catch(error => {
- console.error('Error:', error);
- showToast(`${i18n.errorConnectionPrefix || 'Error de conexión:'} ${error.message}`, 'error');
- });
- }
- // Función para ELIMINAR TODAS las notificaciones
- function deleteAllNotifications() {
- if (!confirm(i18n.confirmDeleteAll)) {
- return;
- }
-
- fetch('/api/notifications/delete-all', {
- method: 'DELETE',
- headers: {
- 'X-CSRFToken': csrfToken,
- }
- })
- .then(response => {
- if (!response.ok) {
- throw new Error(`Error HTTP: ${response.status}`);
- }
- return response.json();
- })
- .then(data => {
- if (data.success || data.status === 'success') {
- // Mostrar estado vacío
- showEmptyState();
-
- // Actualizar contador
- document.getElementById('unread-count').textContent = '0';
-
- // Actualizar paginación
- document.getElementById('current-range').textContent = '0-0';
- document.getElementById('total-notifications').textContent = '0';
- document.getElementById('page-info').textContent = 'Página 1 de 1';
-
- // Deshabilitar botones de paginación
- document.getElementById('prev-page').disabled = true;
- document.getElementById('next-page').disabled = true;
-
- showToast(i18n.allNotificationsDeleted, 'success');
- } else {
- showToast(`${i18n.errorDeleteNotifications || 'Error al eliminar notificaciones:'} ${data.error || i18n.errorUnknown || 'Error desconocido'}`, 'error');
- }
- })
- .catch(error => {
- console.error('Error:', error);
- showToast(`${i18n.errorConnectionPrefix || 'Error de conexión:'} ${error.message}`, 'error');
- });
- }
-
- // Filtros
- function applyFilters() {
- const typeFilter = document.getElementById('filter-type').value;
- const sourceFilter = document.getElementById('filter-source').value;
- const statusFilter = document.getElementById('filter-status').value;
-
- const notifications = document.querySelectorAll('.notification-item');
- let visibleCount = 0;
-
- notifications.forEach(notification => {
- const type = notification.getAttribute('data-type');
- const source = notification.getAttribute('data-source');
- const read = notification.getAttribute('data-read') === 'true';
-
- const typeMatch = typeFilter === 'all' || type === typeFilter;
- const sourceMatch = sourceFilter === 'all' || source === sourceFilter;
- const statusMatch = statusFilter === 'all' ||
- (statusFilter === 'unread' && !read) ||
- (statusFilter === 'read' && read);
-
- if (typeMatch && sourceMatch && statusMatch) {
- notification.style.display = 'block';
- visibleCount++;
- } else {
- notification.style.display = 'none';
- }
- });
-
- if (visibleCount === 0 && notifications.length > 0) {
- // No mostrar toast aquí para evitar spam al aplicar filtros
- }
- }
-
- // Event Listeners
- document.addEventListener('click', function(e) {
- if (e.target.closest('.mark-read-btn')) {
- const button = e.target.closest('.mark-read-btn');
- const notificationId = parseInt(button.getAttribute('data-id'));
- markAsRead([notificationId]);
- }
-
- if (e.target.closest('.delete-btn')) {
- const button = e.target.closest('.delete-btn');
- const notificationId = parseInt(button.getAttribute('data-id'));
- deleteNotification(notificationId);
- }
- });
-
- document.getElementById('mark-all-read').addEventListener('click', markAllAsRead);
- document.getElementById('delete-all').addEventListener('click', deleteAllNotifications);
-
- // Paginación
- document.getElementById('prev-page').addEventListener('click', function() {
- if (currentPage > 1) {
- currentPage--;
- loadNotifications(currentPage);
- }
- });
-
- document.getElementById('next-page').addEventListener('click', function() {
- if (currentPage < totalPages) {
- currentPage++;
- loadNotifications(currentPage);
- }
- });
-
- // Filtros
- document.getElementById('filter-type').addEventListener('change', applyFilters);
- document.getElementById('filter-source').addEventListener('change', applyFilters);
- document.getElementById('filter-status').addEventListener('change', applyFilters);
-
- document.getElementById('clear-filters').addEventListener('click', function() {
- document.getElementById('filter-type').value = 'all';
- document.getElementById('filter-source').value = 'all';
- document.getElementById('filter-status').value = 'all';
- applyFilters();
- showToast(i18n.filtersCleared, 'info');
- });
- });
- </script>
- {% endblock %}
|