<?php

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

// Conexão com o MySQL
$conn = new mysqli($hostname, $username, $password, $database);


// Verifica a conexão
if ($conn->connect_error) {
    die("Erro de conexão: " . $conn->connect_error);
}

// Inicializa a variável de filtro (se fornecida)
$filterHost = isset($_POST['filter_host']) ? $_POST['filter_host'] : '';

// Se o formulário foi enviado, atualiza o valor do filtro
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $filterHost = $_POST['filter_host'];
}

// Constrói a query com base no filtro de host
$query = "SELECT st.id as ID, st.timestamp as DateTime, h.hostname as Host, h.ip as IP, ROUND(dl,0) as Down, ROUND(ul,0) as UP, ROUND(ping,0) as Ping, ROUND(jitter,0) as Jitter
          FROM speedtest_cobranca as st, hosts as h
          WHERE h.ip = st.ip AND h.hostname LIKE '%$filterHost%'
          ORDER BY 1 DESC";

// Executa a query
$result = $conn->query($query);

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Consulta Speedtest</title>
</head>
<body>
<p>

<!-- Formulário para inserir o host e escolher a tabela -->
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <label for="filter_host">Digite o Host:</label>
    <input type="text" id="filter_host" name="filter_host" value="<?php echo $filterHost; ?>">

    <label for="select_table">Escolha a Tabela:</label>
    <select id="select_table" name="select_table">
        <option value="speedtest_cobranca">speedtest_adm</option>
        <option value="speedtest_cobranca">speedtest_cobranca</option>
        <option value="speedtest_cobranca">speedtest_cpd</option>
        <option value="speedtest_cobranca">speedtest_imagem</option>
        <option value="speedtest_cobranca">speedtest_recepcao_terreo</option>
        <option value="speedtest_cobranca">speedtest_sala19</option>        
        <!-- Adicione mais opções conforme necessário -->
    </select>

    <input type="submit" value="Consultar">
</form>

<center>
<!-- Formulário para inserir o host -->
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <h1><center><label for="filter_host">Digite o Host:</label></center></h1>
    <input type="text" id="filter_host" name="filter_host" value="<?php echo $filterHost; ?>">
    <input type="submit" value="Consultar">
</form>
</center>
<p>
   <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Arial', sans-serif;
            background-image: url('http://7system.com.br/img/cloud3.jpg'); /* Substitua 'sua_imagem_de_nuvem.jpg' pelo caminho da sua imagem de nuvem */
            background-size: cover;
            background-attachment: fixed;
            background-color: rgba(255, 255, 255, 0.9); /* Opacidade de 50% - ajuste conforme necessário */
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        table, th, td {
            border: 1px solid #ddd;
        }

        th, td {
            padding: 10px;
            text-align: center;
            background-color: rgba(#4CAF50, 0.5);
        }

        th {
            background-color: #4CAF50;
            color: white;
        }
    </style>
<?php
// Verifica se há resultados
if ($result->num_rows > 0) {
    echo "<table border='1'>
            <tr>
                <th>ID</th>
                <th>Data e Hora</th>
                <th>Host</th>
                <th>IP</th>
                <th>Down</th>
                <th>UP</th>
                <th>Ping</th>
                <th>Jitter</th>
            </tr>";

    // Exibe os resultados em uma tabela
    while ($row = $result->fetch_assoc()) {
        echo "<tr>
                <td>{$row['ID']}</td>
                <td>{$row['DateTime']}</td>
                <td>{$row['Host']}</td>
                <td>{$row['IP']}</td>
                <td>{$row['Down']}</td>
                <td>{$row['UP']}</td>
                <td>{$row['Ping']}</td>
                <td>{$row['Jitter']}</td>
              </tr>";
    }

    echo "</table>";
} else {
    echo "Nenhum resultado encontrado.";
}

// Fecha a conexão com o MySQL
$conn->close();
?>

</body>
</html>