<?php
include_once("conexao.php");
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <!-- Meta tags Obrigat¨®rias -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>DADOS</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        .table {
            font-size: 16px;
        }
        .thead-dark th {
            background-color: #343a40;
            color: #fff;
            text-align: center;
        }
        .table-bordered th, .table-bordered td {
            border: 1px solid #dee2e6;
        }
    </style>
</head>
<body>
<div style='width: 80%; margin-left: auto; margin-right: auto; margin-top: 20px; padding-bottom: 20px;'>    
    <form action="#" method="post">
        <label for="nome">Buscar por nome:</label>
        <input type="text" id="nome" name="nome" class="form-control" style="width: 300px; display: inline-block;">
        <input type="submit" value="Buscar" class="btn btn-primary">
    </form>
</div>

<table class='table table-bordered table-hover table-striped' style='width: 80%; margin-left: auto; margin-right: auto;'>
    <thead class='thead-dark'>
        <tr>
            <th scope='col'>LOGIN</th>
            <th scope='col'>NOME</th>
            <th scope='col'>DATA/HOR&Aacute;RIO</th>
        </tr>
    </thead>
    <tbody>
    <?php
    $total_linhas = 0; // Contador para o total de linhas
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $pesquisar = mysqli_real_escape_string($conn, $_POST['nome']); // Escapa o input do usu¨¢rio
        $result_usuario = "SELECT * FROM acessos WHERE nome LIKE '%$pesquisar%' ORDER BY data DESC LIMIT 10";
        $resultado_usuario = mysqli_query($conn, $result_usuario);
        $total_linhas = mysqli_num_rows($resultado_usuario); // Conta o total de linhas encontradas

        if ($total_linhas > 0) {
            while ($row_usuario = mysqli_fetch_assoc($resultado_usuario)) {    
                echo "<tr>";
                echo "<td>" . $row_usuario['login'] . "</td>";
                echo "<td style='text-transform: uppercase'>" . $row_usuario['nome'] . "</td>";
                $data = $row_usuario['data'];
                $data = strtotime($data);
                echo "<td>" . date('d/m/Y H:i:s', $data) . "</td>";
                echo "</tr>";
            }
        } else {
            echo "<tr>";
            echo "<td colspan='3'><div class='alert alert-danger text-center' role='alert'>Nenhum registro encontrado!</div></td>";
            echo "</tr>";
        }
    }
    ?>
    </tbody>
</table>

<div style='width: 80%; margin-left: auto; margin-right: auto; text-align: center;'>
    <p>Total de registros encontrados: <strong><?php echo $total_linhas; ?></strong></p>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js
