false, 'message' => 'Not authenticated']); exit; } $client_id = $_SESSION['client_id']; // Get JSON input $input = json_decode(file_get_contents('php://input'), true); $project_id = $input['project_id'] ?? ''; $batch_number = (int)($input['batch_number'] ?? 0); $url_ids = $input['url_ids'] ?? []; if (empty($project_id) || $batch_number < 1 || empty($url_ids)) { echo json_encode(['success' => false, 'message' => 'Invalid parameters']); exit; } try { $shopPdo = getShopDBConnection(); // Verify project belongs to client $stmt = $shopPdo->prepare(" SELECT id FROM projects WHERE project_id = ? AND client_id = ? "); $stmt->execute([$project_id, $client_id]); if (!$stmt->fetch()) { echo json_encode(['success' => false, 'message' => 'Access denied']); exit; } $shopPdo->beginTransaction(); try { // Delete only unsent URLs $placeholders = str_repeat('?,', count($url_ids) - 1) . '?'; $stmt = $shopPdo->prepare(" DELETE FROM survey_urls WHERE id IN ($placeholders) AND project_id = ? AND batch_number = ? AND is_sent = 0 "); $params = array_merge($url_ids, [$project_id, $batch_number]); $stmt->execute($params); $deleted = $stmt->rowCount(); // Update batch totals $stmt = $shopPdo->prepare(" UPDATE survey_url_batches SET total_urls = total_urls - ? WHERE project_id = ? AND batch_number = ? "); $stmt->execute([$deleted, $project_id, $batch_number]); $shopPdo->commit(); echo json_encode([ 'success' => true, 'deleted' => $deleted, 'message' => "Successfully deleted $deleted URLs" ]); } catch (Exception $e) { $shopPdo->rollBack(); throw $e; } } catch (Exception $e) { echo json_encode([ 'success' => false, 'message' => 'Error: ' . $e->getMessage() ]); }