<?php
include('../include/headscript.php');
include("../class/company.php");
$conn = new mysqli($servername, $username, $password, $database);

// Recupera l'ID dell'analisi dalla richiesta
$analysisId = $_POST['id'] ?? null;

if ($analysisId) {
    $sql = "SELECT c.idcompoundsvocabulary, c.namecompoundsvocabulary, c.cascompoundvocabulary
            FROM analysis_compounds ac
            INNER JOIN compundsvocabulary c ON ac.idcompoundsvocabulary = c.idcompoundsvocabulary
            WHERE ac.idanalysisvocabulary = ?";

    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $analysisId);
    $stmt->execute();
    $result = $stmt->get_result();

    $data = [];
    while ($row = $result->fetch_assoc()) {
        $data[] = [
            'idcompoundsvocabulary' => $row['idcompoundsvocabulary'],
            'namecompoundsvocabulary' => $row['namecompoundsvocabulary'],
            'cascompoundvocabulary' => $row['cascompoundvocabulary'],
        ];
    }

    echo json_encode($data);
} else {
    echo json_encode(['error' => 'Invalid analysis ID']);
}

$conn->close();
