'Inhoud is verplicht']); exit; } $stmt = $db->prepare(" INSERT INTO brainstorm_cards (session_id, content, category, color, author_name, votes) VALUES (?, ?, ?, ?, ?, 0) "); $stmt->execute([$sessionId, $content, $category, $color, $user['name']]); $cardId = (int)$db->lastInsertId(); echo json_encode([ 'success' => true, 'card' => [ 'id' => $cardId, 'content' => $content, 'category' => $category, 'color' => $color, 'author_name' => $user['name'], 'votes' => 0, 'converted_to_roadmap_item_id' => null ] ]); exit; } // 2. Stemmen / Upvote op kaart if ($action === 'vote_card') { $data = json_decode(file_get_contents('php://input'), true); $cardId = (int)($data['card_id'] ?? 0); $stmt = $db->prepare("UPDATE brainstorm_cards SET votes = votes + 1 WHERE id = ?"); $stmt->execute([$cardId]); $stmtVotes = $db->prepare("SELECT votes FROM brainstorm_cards WHERE id = ?"); $stmtVotes->execute([$cardId]); $votes = (int)$stmtVotes->fetchColumn(); echo json_encode(['success' => true, 'votes' => $votes]); exit; } // 3. Kaart verwijderen if ($action === 'delete_card') { $data = json_decode(file_get_contents('php://input'), true); $cardId = (int)($data['card_id'] ?? 0); $stmt = $db->prepare("DELETE FROM brainstorm_cards WHERE id = ?"); $stmt->execute([$cardId]); echo json_encode(['success' => true]); exit; } // 4. Omzetten naar Roadmap! if ($action === 'convert_to_roadmap') { $data = json_decode(file_get_contents('php://input'), true); $cardIds = $data['card_ids'] ?? []; $quarter = $data['quarter'] ?? 'Q3'; $type = $data['type'] ?? 'FEATURE'; $priority = $data['priority'] ?? 'MEDIUM'; $dueDate = !empty($data['due_date']) ? $data['due_date'] : null; if (empty($cardIds) || !is_array($cardIds)) { echo json_encode(['error' => 'Selecteer ten minste één kaart om om te zetten.']); exit; } $db->beginTransaction(); $convertedCount = 0; foreach ($cardIds as $cardId) { $cardId = (int)$cardId; $stmtCard = $db->prepare("SELECT * FROM brainstorm_cards WHERE id = ?"); $stmtCard->execute([$cardId]); $card = $stmtCard->fetch(); if ($card) { // Maak titel (eerste zin of max 80 tekens) $firstLine = explode("\n", trim($card['content']))[0]; $title = mb_substr($firstLine, 0, 80); $stmtRoadmap = $db->prepare(" INSERT INTO roadmap_items (title, description, type, status, quarter, priority, progress, due_date, origin_brainstorm_card_id, created_by_id) VALUES (?, ?, ?, 'PLANNED', ?, ?, 0, ?, ?, ?) "); $stmtRoadmap->execute([ $title, $card['content'], $type, $quarter, $priority, $dueDate, $cardId, $user['id'] ]); $roadmapId = (int)$db->lastInsertId(); // Koppel terug aan brainstorm kaart $stmtUpdateCard = $db->prepare("UPDATE brainstorm_cards SET converted_to_roadmap_item_id = ? WHERE id = ?"); $stmtUpdateCard->execute([$roadmapId, $cardId]); $convertedCount++; } } $db->commit(); echo json_encode([ 'success' => true, 'message' => "{$convertedCount} brainstorm items succesvol omgezet naar de Roadmap!", 'converted_count' => $convertedCount ]); exit; } echo json_encode(['error' => 'Onbekende actie']); } catch (Exception $e) { if ($db->inTransaction()) $db->rollBack(); http_response_code(500); echo json_encode(['error' => $e->getMessage()]); }