<?php

$servername = "149.56.34.221";
$username = "cegosa24h";
$password = "hospital@1234!";
$database = "cegosa24h";

// Criar a conexão
$conn = new mysqli($servername, $username, $password, $database);

// Verificar a conexão
if ($conn->connect_error) {
    // Erro na conexão
    echo '<span style="color: red;">Erro na conexão: ' . $conn->connect_error . '</span>';
} else {
    // Sucesso na conexão
    echo '<span style="color: blue;">Conexão estabelecida com sucesso!</span><br>';

    // Executar SELECT na tabela speedtest_sala19
    $sql = "SELECT h.hostname, scob.ip, scob.timestamp, round(avg(scob.dl),0) as dl, round(avg(scob.ul),0) as ul, round(avg(scob.ping),0) as ping, round(avg(scob.jitter),0) as jitter
FROM hosts as h, speedtest_adm as scob 
Where h.ip = scob.ip
group by h.hostname 
order by h.hostname asc, scob.timestamp desc";
    $result = $conn->query($sql);

    // Verificar se há resultados
    if ($result->num_rows > 0) {
        // Exibir os registros em verde
        echo '<span style="color: green;">Registros encontrados:</span><br>';

        // Exibir os registros
echo "<table border='1' style='width:100%;'>";
echo "<tr>";
echo "<th style='width:15%;'>HOSTNAME/IP</th><th style='width:10%;'>IP</th><th style='width:15%;'>DATETIME</th><th style='width:10%;'>DOWNLOAD</th><th style='width:10%;'>UPLOAD</th><th style='width:10%;'>PING</th><th style='width:10%;'>JITTER</th>";
echo "</tr>";


while ($row = $result->fetch_assoc()) {
echo "<table border='1' style='width:100%;'>";
    echo "<tr>";
    echo "<td style='text-align:center; width:15%;'>" . $row["hostname"] . "</td><td style='text-align:center; width:10%;'>" . $row["ip"] . "</td><td style='text-align:center; width:15%;'>" . $row["timestamp"] . "</td><td style='text-align:center; width:10%;'>" . $row["dl"] . "</td><td style='text-align:center; width:10%;'>" . $row["ul"] . "</td><td style='text-align:center; width:10%;'>" . $row["ping"] . "</td><td style='text-align:center; width:10%;'>" . $row["jitter"] . "</td>";
    echo "</tr>";
    echo "</table>";
        }
    } else {
        // Caso não haja registros
        echo '<span style="color: orange;">Nenhum registro encontrado.</span>';
    }

    // Fechar a conexão
    $conn->close();
}

?>
