$maxId) { $maxId = $t['id']; } } $todos[] = [ 'id' => $maxId + 1, 'title' => $title, 'completed' => false, 'created_at' => date('H:i d/m/Y') ]; file_put_contents($todoFile, json_encode($todos, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), LOCK_EX); } } elseif ($action === 'toggle') { $id = (int)($_POST['id'] ?? ($body['id'] ?? 0)); foreach ($todos as &$t) { if ((int)$t['id'] === $id) { $t['completed'] = !$t['completed']; break; } } unset($t); file_put_contents($todoFile, json_encode($todos, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), LOCK_EX); } elseif ($action === 'delete') { $id = (int)($_POST['id'] ?? ($body['id'] ?? 0)); $todos = array_values(array_filter($todos, function ($t) use ($id) { return (int)$t['id'] !== $id; })); file_put_contents($todoFile, json_encode($todos, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), LOCK_EX); } elseif ($action === 'clear_completed') { $todos = array_values(array_filter($todos, function ($t) { return empty($t['completed']); })); file_put_contents($todoFile, json_encode($todos, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), LOCK_EX); } } echo json_encode([ 'status' => 'success', 'todos' => $todos ], JSON_UNESCAPED_UNICODE);