false, 'message' => 'User not logged in.']); exit; } $noteId = $_GET['note_id'] ?? null; if ($noteId) { try { $pdo = db(); // First, get the client_id for redirection $stmt = $pdo->prepare("SELECT client_id FROM notes WHERE note_id = ?"); $stmt->execute([$noteId]); $note = $stmt->fetch(PDO::FETCH_ASSOC); $clientId = $note['client_id'] ?? null; if ($clientId) { log_audit_event('note_delete', $_SESSION['user_id'], 'note', $noteId); // Now, delete the note $deleteStmt = $pdo->prepare("DELETE FROM notes WHERE note_id = ?"); $deleteStmt->execute([$noteId]); echo json_encode(['success' => true]); exit; } else { // Note not found or no client_id associated echo json_encode(['success' => false, 'message' => 'Note not found.']); exit; } } catch (PDOException $e) { // Optional: Log error echo json_encode(['success' => false, 'message' => 'Database error.']); exit; } } else { // Redirect if note_id is missing echo json_encode(['success' => false, 'message' => 'Note ID is required.']); exit; } ?>