// AutoBackups JavaScript Application class AutoBackupsApp { constructor() { this.init(); } init() { this.initializeTheme(); this.updateCurrentTime(); this.setupEventListeners(); // Update time every minute setInterval(() => this.updateCurrentTime(), 60000); } updateCurrentTime() { const now = new Date(); const timeString = now.toLocaleString('es-ES', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }); const timeElement = document.getElementById('current-time'); if (timeElement) { timeElement.textContent = timeString; } } // Theme Management initializeTheme() { // Load saved theme from localStorage or default to light const savedTheme = localStorage.getItem('theme') || 'light'; this.setTheme(savedTheme); } setTheme(theme) { const htmlElement = document.documentElement; const themeIcon = document.getElementById('theme-icon'); if (theme === 'dark') { htmlElement.setAttribute('data-theme', 'dark'); if (themeIcon) { themeIcon.className = 'bi bi-moon-fill'; } } else { htmlElement.removeAttribute('data-theme'); if (themeIcon) { themeIcon.className = 'bi bi-sun-fill'; } } localStorage.setItem('theme', theme); } toggleTheme() { const currentTheme = document.documentElement.getAttribute('data-theme'); const newTheme = currentTheme === 'dark' ? 'light' : 'dark'; this.setTheme(newTheme); } setupEventListeners() { // Theme toggle button const themeToggleBtn = document.getElementById('theme-toggle'); if (themeToggleBtn) { themeToggleBtn.addEventListener('click', () => this.toggleTheme()); } // Global backup all button const backupAllBtn = document.getElementById('backup-all-btn'); if (backupAllBtn) { backupAllBtn.addEventListener('click', () => this.backupAllProjects()); } // Save project configuration const saveConfigBtn = document.getElementById('save-project-config-btn'); if (saveConfigBtn) { saveConfigBtn.addEventListener('click', () => this.saveProjectConfig()); } } backupAllProjects() { this.showAlert('info', 'Iniciando backup de todos los proyectos habilitados...'); // Get all enabled projects and trigger backup fetch('/api/projects') .then(response => response.json()) .then(data => { if (data.error) { this.showAlert('danger', `Error: ${data.error}`); return; } const enabledProjects = data.projects.filter(project => project.schedule_config && project.schedule_config.enabled ); if (enabledProjects.length === 0) { this.showAlert('warning', 'No hay proyectos habilitados para backup.'); return; } // Backup each enabled project let completed = 0; enabledProjects.forEach(project => { fetch(`/api/projects/${project.id}/backup`, { method: 'POST', headers: { 'Content-Type': 'application/json' } }) .then(response => response.json()) .then(backupData => { completed++; if (completed === enabledProjects.length) { this.showAlert('success', `Backup completado para ${enabledProjects.length} proyectos.`); } }) .catch(error => { console.error(`Error backing up project ${project.id}:`, error); }); }); }) .catch(error => { this.showAlert('danger', `Error de conexión: ${error.message}`); }); } saveProjectConfig() { const projectId = document.getElementById('modal-project-id').value; const schedule = document.getElementById('modal-project-schedule').value; const time = document.getElementById('modal-project-time').value; const enabled = document.getElementById('modal-project-enabled').checked; const config = { schedule_config: { schedule: schedule, schedule_time: time, enabled: enabled } }; fetch(`/api/projects/${projectId}/config`, { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(config) }) .then(response => response.json()) .then(data => { if (data.error) { this.showAlert('danger', `Error: ${data.error}`); } else { this.showAlert('success', 'Configuración guardada correctamente.'); // Close modal const modal = bootstrap.Modal.getInstance( document.getElementById('projectConfigModal') ); if (modal) { modal.hide(); } // Refresh page after a short delay setTimeout(() => location.reload(), 1500); } }) .catch(error => { this.showAlert('danger', `Error de conexión: ${error.message}`); }); } showAlert(type, message, duration = 5000) { const alertsContainer = document.getElementById('alerts-container'); if (!alertsContainer) return; const alertId = 'alert-' + Date.now(); const alertHtml = `