/** * Logger Manager - Modern activity log management for the unified logger view */ class LoggerManager { constructor(options = {}) { this.categories = options.categories || []; this.initialFilters = options.initialFilters || {}; // Pagination state this.currentPage = 1; this.perPage = 50; this.totalPages = 1; this.totalEntries = 0; // Current filter state this.filters = { ...this.initialFilters }; // DOM elements this.initElements(); this.initEventListeners(); // Apply initial filters and load data this.applyInitialFilters(); this.loadLogs(); } initElements() { // Form elements this.filtersForm = document.getElementById("logFiltersForm"); this.categorySelect = document.getElementById("category"); this.statusSelect = document.getElementById("status"); this.startDateInput = document.getElementById("start_date"); this.endDateInput = document.getElementById("end_date"); this.searchTermInput = document.getElementById("search_term"); this.clearFiltersBtn = document.getElementById("clearFilters"); this.downloadLogsBtn = document.getElementById("downloadLogs"); this.refreshLogsBtn = document.getElementById("refreshLogs"); // Logs display elements this.logsTableBody = document.getElementById("logsTableBody"); this.pageSizeSelect = document.getElementById("logPageSize"); // Pagination elements this.paginationContainer = document.getElementById("logsPagination"); this.paginationInfo = document.getElementById("logsPaginationInfo"); this.prevPageBtn = document.getElementById("logsPrevPage"); this.nextPageBtn = document.getElementById("logsNextPage"); this.currentPageSpan = document.getElementById("logsCurrentPage"); // Modal this.logModal = new ModalHandler("logDetailModal", "log-detail-content"); } initEventListeners() { // Filter form submission if (this.filtersForm) { this.filtersForm.addEventListener("submit", (e) => { e.preventDefault(); this.applyFilters(); }); } // Individual filter changes for immediate application [ this.categorySelect, this.statusSelect, this.startDateInput, this.endDateInput, ].forEach((element) => { if (element) { element.addEventListener("change", () => { this.applyFilters(); }); } }); // Search term with debounce if (this.searchTermInput) { let searchTimeout; this.searchTermInput.addEventListener("input", () => { clearTimeout(searchTimeout); searchTimeout = setTimeout(() => { this.applyFilters(); }, 500); }); } // Clear filters if (this.clearFiltersBtn) { this.clearFiltersBtn.addEventListener("click", () => { this.clearAllFilters(); }); } // Download logs if (this.downloadLogsBtn) { this.downloadLogsBtn.addEventListener("click", (e) => { e.preventDefault(); this.downloadLogs(); }); } // Refresh logs if (this.refreshLogsBtn) { this.refreshLogsBtn.addEventListener("click", () => { this.loadLogs(); }); } // Page size change if (this.pageSizeSelect) { this.pageSizeSelect.addEventListener("change", () => { this.perPage = parseInt(this.pageSizeSelect.value); this.currentPage = 1; // Reset to first page this.loadLogs(); }); } // Pagination buttons if (this.prevPageBtn) { this.prevPageBtn.addEventListener("click", (e) => { e.preventDefault(); if (this.currentPage > 1) { this.currentPage--; this.loadLogs(); } }); } if (this.nextPageBtn) { this.nextPageBtn.addEventListener("click", (e) => { e.preventDefault(); if (this.currentPage < this.totalPages) { this.currentPage++; this.loadLogs(); } }); } } applyInitialFilters() { // Set form values from initial filters if (this.categorySelect && this.initialFilters.category) { this.categorySelect.value = this.initialFilters.category; } if (this.startDateInput && this.initialFilters.start_date) { this.startDateInput.value = this.initialFilters.start_date; } if (this.endDateInput && this.initialFilters.end_date) { this.endDateInput.value = this.initialFilters.end_date; } if (this.searchTermInput && this.initialFilters.search_term) { this.searchTermInput.value = this.initialFilters.search_term; } } applyFilters() { // Collect current filter values this.filters = { category: this.categorySelect?.value || "", status: this.statusSelect?.value || "", start_date: this.startDateInput?.value || "", end_date: this.endDateInput?.value || "", search_term: this.searchTermInput?.value || "", }; // Reset to first page when filters change this.currentPage = 1; // Load logs with new filters this.loadLogs(); // Update URL to reflect current filters (for bookmarking/sharing) this.updateUrl(); } clearAllFilters() { // Clear all form fields if (this.categorySelect) this.categorySelect.value = ""; if (this.statusSelect) this.statusSelect.value = ""; if (this.startDateInput) this.startDateInput.value = ""; if (this.endDateInput) this.endDateInput.value = ""; if (this.searchTermInput) this.searchTermInput.value = ""; // Apply empty filters this.applyFilters(); } async loadLogs() { if (!this.logsTableBody) return; try { // Show loading state this.logsTableBody.innerHTML = '