<?php
include('../include/headscript.php');

// Connessione al database
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Recupera i dati dei fornitori con dettagli aggiuntivi
$query = "
    SELECT 
        id,
        name,
        total_products,
        total_analyses,
        pass_analyses,
        fail_analyses,
        data_analyses,
        rating,
        calculation_date
    FROM supplier_ratings
    ORDER BY rating DESC
";
$result = $conn->query($query);

$data = [];
while ($row = $result->fetch_assoc()) {
    $color = $row['rating'] >= 8 ? '#28a745' : ($row['rating'] >= 5 ? '#ffc107' : '#dc3545');
    $data[] = [
        'id' => $row['id'],
        'name' => $row['name'],
        'total_products' => $row['total_products'],
        'total_analyses' => $row['total_analyses'],
        'pass_analyses' => $row['pass_analyses'],
        'fail_analyses' => $row['fail_analyses'],
        'data_analyses' => $row['data_analyses'],
        'rating' => $row['rating'],
        'date' => $row['calculation_date'],
        'color' => $color
    ];
}

header('Content-Type: application/json');
echo json_encode($data);

$conn->close();
