Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const taskForm = document.getElementById('taskForm');
+ const taskInput = document.getElementById('taskInput');
+ const reminderTime = document.getElementById('reminderTime');
+ const pendingTasks = document.getElementById('pendingTasks');
+ const completedTasks = document.getElementById('completedTasks');
+ const themeToggle = document.getElementById('themeToggle');
+
+ // Load tasks from localStorage
+ let tasks = JSON.parse(localStorage.getItem('tasks')) || [];
+
+ // Theme handling
+ const theme = localStorage.getItem('theme') || 'light';
+ document.documentElement.setAttribute('data-theme', theme);
+ themeToggle.textContent = theme === 'light' ? '🌙' : '☀️';
+
+ themeToggle.addEventListener('click', () => {
+ const currentTheme = document.documentElement.getAttribute('data-theme');
+ const newTheme = currentTheme === 'light' ? 'dark' : 'light';
+ document.documentElement.setAttribute('data-theme', newTheme);
+ localStorage.setItem('theme', newTheme);
+ themeToggle.textContent = newTheme === 'light' ? '🌙' : '☀️';
+ });
+
+ // Render tasks
+ function renderTasks() {
+ pendingTasks.innerHTML = '';
+ completedTasks.innerHTML = '';
+
+ tasks.forEach((task, index) => {
+ const li = document.createElement('li');
+ li.className = `task-item ${task.completed ? 'completed' : ''}`;
+
+ li.innerHTML = `
+
+ ${task.text}
+ ${task.reminder ? `${new Date(task.reminder).toLocaleString()} ` : ''}
+ Delete
+ `;
+
+ li.querySelector('input').addEventListener('change', () => toggleTask(index));
+
+ if (task.completed) {
+ completedTasks.appendChild(li);
+ } else {
+ pendingTasks.appendChild(li);
+ }
+ });
+
+ localStorage.setItem('tasks', JSON.stringify(tasks));
+ }
+
+ // Add task
+ taskForm.addEventListener('submit', (e) => {
+ e.preventDefault();
+
+ const text = taskInput.value.trim();
+ const reminder = reminderTime.value;
+
+ if (text) {
+ tasks.push({
+ text,
+ reminder: reminder || null,
+ completed: false,
+ createdAt: new Date().toISOString()
+ });
+
+ if (reminder) {
+ scheduleReminder(text, reminder);
+ }
+
+ taskInput.value = '';
+ reminderTime.value = '';
+ renderTasks();
+ }
+ });
+
+ // Toggle task completion
+ window.toggleTask = (index) => {
+ tasks[index].completed = !tasks[index].completed;
+ renderTasks();
+ };
+
+ // Delete task
+ window.deleteTask = (index) => {
+ tasks.splice(index, 1);
+ renderTasks();
+ };
+
+ // Schedule reminder
+ function scheduleReminder(text, time) {
+ const reminderTime = new Date(time).getTime();
+ const now = new Date().getTime();
+
+ if (reminderTime > now) {
+ setTimeout(() => {
+ if (Notification.permission === 'granted') {
+ new Notification('Task Reminder', { body: text });
+ }
+ }, reminderTime - now);
+ }
+ }
+
+ // Request notification permission
+ if ('Notification' in window) {
+ Notification.requestPermission();
+ }
+
+ // Initial render
+ renderTasks();
+ });