document.addEventListener('DOMContentLoaded', function () { console.log('main.js: DOMContentLoaded event fired.'); const themeToggle = document.getElementById('theme-toggle'); const sidebarToggle = document.getElementById('sidebar-toggle'); const sidebar = document.querySelector('.sidebar'); const mainContent = document.querySelector('.main-content'); const currentTheme = localStorage.getItem('theme'); if (currentTheme) { document.body.classList.add(currentTheme); if (currentTheme === 'dark-mode') { themeToggle.checked = true; } } themeToggle.addEventListener('change', function () { if (this.checked) { document.body.classList.remove('light-mode'); document.body.classList.add('dark-mode'); localStorage.setItem('theme', 'dark-mode'); } else { document.body.classList.remove('dark-mode'); document.body.classList.add('light-mode'); localStorage.setItem('theme', 'light-mode'); } }); sidebarToggle.addEventListener('click', function () { sidebar.classList.toggle('collapsed'); // Adjust main content margin only if not on mobile if (window.innerWidth > 768) { mainContent.classList.toggle('expanded'); } }); const addTaskForm = document.getElementById('addTaskForm'); if (addTaskForm) { addTaskForm.addEventListener('submit', async function (e) { console.log('main.js: addTaskForm submitted.'); e.preventDefault(); const formData = new FormData(this); formData.append('action', 'addTask'); try { const response = await fetch('index.php', { method: 'POST', body: formData }); const result = await response.json(); if (result.success) { alert(result.message); this.reset(); // Clear form fields // Close modal const addTaskModal = bootstrap.Modal.getInstance(document.getElementById('addTaskModal')); if (addTaskModal) { addTaskModal.hide(); } loadTasks(); // Refresh tasks list } else { alert('Error: ' + result.message); } } catch (error) { console.error('Error adding task:', error); alert('An unexpected error occurred while adding the task. Check console for details.'); } }); } // Function to load and display tasks async function loadTasks() { console.log('main.js: loadTasks() called.'); const tasksListDiv = document.getElementById('tasksList'); if (!tasksListDiv) return; tasksListDiv.innerHTML = '
No tasks yet. Add one!
${task.description}
${task.status}Error loading tasks: ' + (result.message || 'Unknown error') + '
An unexpected error occurred while fetching tasks. Check console for details.