<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Conexão com o banco de dados SQLite
$db1 = new SQLite3('a/.eggziedb.db');

// Consulta para obter o total de clientes
$totalClientesQuery = "SELECT COUNT(*) as total_clientes FROM ibo";
$totalClientesResult = $db1->querySingle($totalClientesQuery);

// Consulta para obter as URLs e a contagem de clientes
$query = "
    SELECT url, COUNT(*) as cliente_count
    FROM ibo
    GROUP BY url
    ORDER BY url ASC
";
$result = $db1->query($query);

include 'includes/header.php';
?>
<main role="main" class="col-15 pt-4 px-5">
    <div class="container">
        <h2 class="mb-4 text-primary"><i class="fas fa-globe"></i> Lista de Clientes & DNS</h2>
        <div class="alert alert-info">
            <strong>Total de clientes cadastrados:</strong> <?= $totalClientesResult ?>
        </div>
        <div class="table-responsive">
            <table class="table table-striped table-sm">
                <thead class="text-primary">
                    <tr>
                        <th>#</th>
                        <th>DNS (URL)</th>
                        <th>Quantidade de Clientes</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $count = 1;
                    while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
                        $dns = $row['url'];
                        $cliente_count = $row['cliente_count'];
                        if (!empty($dns)) {
                            echo "<tr>
                                    <td>{$count}</td>
                                    <td>{$dns}</td>
                                    <td>{$cliente_count}</td>
                                  </tr>";
                            $count++;
                        }
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</main>
<?php
include 'includes/footer.php';
?>
