getConnection(); $production_id = isset($_GET['production_id']) ? (int)$_GET['production_id'] : 0; $photo_type = $_GET['photo_type'] ?? ''; $slot = isset($_GET['param_position']) ? (int)$_GET['param_position'] : null; if ($production_id <= 0 || $photo_type === '') { throw new Exception("Parametri non validi"); } // QUERY BASE $sql = " SELECT id, filename, photo_type, created_at, elaborato FROM production_photos WHERE production_id = :prod AND photo_type = :ptype "; // SE È PARAMETRO MACCHINA → FILTRO PER POSIZIONE if ($photo_type === 'parametri_macchina') { $sql .= " AND param_position = :slot"; } $sql .= " ORDER BY created_at DESC, id DESC"; $stmt = $pdo->prepare($sql); $params = [ ':prod' => $production_id, ':ptype' => $photo_type ]; if ($photo_type === 'parametri_macchina') { $params[':slot'] = $slot; } $stmt->execute($params); $photos = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode([ 'success' => true, 'photos' => $photos ]); } catch (Exception $e) { echo json_encode([ 'success' => false, 'message' => $e->getMessage() ]); }