false, 'message' => 'Invalid request method']); exit; } try { $pdo = getClientDBConnection(); $selection_id = (int)$_POST['selection_id']; if (!$selection_id) { throw new Exception('Invalid selection ID'); } // Verify selection belongs to this client and is draft $stmt = $pdo->prepare(" SELECT ps.id, ps.status, ps.selection_name FROM project_selections ps JOIN projects p ON ps.project_id = p.id WHERE ps.id = ? AND p.client_id = ? AND ps.client_id = ? "); $stmt->execute([$selection_id, $_SESSION['client_id'], $_SESSION['client_id']]); $selection = $stmt->fetch(); if (!$selection) { throw new Exception('Selection not found or unauthorized'); } if ($selection['status'] !== 'draft') { throw new Exception('Can only delete draft selections'); } // Start transaction $pdo->beginTransaction(); // Delete selection members $stmt = $pdo->prepare("DELETE FROM selection_members WHERE selection_id = ?"); $stmt->execute([$selection_id]); // Delete selection criteria $stmt = $pdo->prepare("DELETE FROM selection_criteria WHERE selection_id = ?"); $stmt->execute([$selection_id]); // Delete activity log $stmt = $pdo->prepare("DELETE FROM selection_activity_log WHERE selection_id = ?"); $stmt->execute([$selection_id]); // Delete selection $stmt = $pdo->prepare("DELETE FROM project_selections WHERE id = ?"); $stmt->execute([$selection_id]); $pdo->commit(); echo json_encode([ 'success' => true, 'message' => 'Selection deleted successfully' ]); } catch (Exception $e) { if (isset($pdo) && $pdo->inTransaction()) { $pdo->rollBack(); } error_log("Delete selection error: " . $e->getMessage()); echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); }