
<section id="cartoes" class="container mt-3" style="font-size:0.8rem;">
    <div class="row text-center">
        <div class="col-md-12 col-lg-12 col-sm-12 mt-4">
            <div class="card h-100 shadow">
                <div class="card">
                    <?php
                        $ficheiro = '../json/data.json';
                        if (!file_exists($ficheiro)) {
                            die("Ficheiro JSON não encontrado.");
                        }
                        $conteudo = file_get_contents($ficheiro);
                        $empresa = json_decode($conteudo, true);
                        if (json_last_error() !== JSON_ERROR_NONE) {
                            die("Erro ao decodificar o JSON: " . json_last_error_msg());
                        }
                        echo '<h2 class="mt-md-4 mb-md-0" style="font-size: 1rem;">' . $empresa["company"]["nome"] . '</h2>'
                    ?>
                    <div class="table-responsive p-md-5 p-lg-5 mt-md-0 mb-md-0">
                        <table class="table table-striped mt-md-0 mb-md-0">
                            <tr class="text-center tb-md-header">
                                <th>Foto Perfil</th>
                                <th>NIF Empresa</th>
                                <th>Nome Empresa</th>
                                <th>Funcionários</th>
                            </tr>
                            <?php 
                                include 'connect.php';

                                $query = "
                                    SELECT e.id, e.nif_cnpj AS nif, e.nome AS nome, COUNT(CASE WHEN u.active = '1' THEN 1 END) AS users FROM empresas e LEFT JOIN utilizadores u ON u.id_empresa = e.id WHERE e.id > 0 GROUP BY e.id, e.nif_cnpj, e.nome;
                                ";
                                $result = $TraceStockDB->query($query);

                                if ($result && $result->rowCount() > 0) {
                                    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
                                        echo "<tr class='text-center tb-md-footer'>";
                                            echo "<td>
                                                    <a href='#' class='d-flex align-items-center justify-content-center text-white text-decoration-none dropdown-toggle' id='dropdownUser1' data-bs-toggle='dropdown' aria-expanded='false'>
                                                        <img src='./img/default_user.png' alt='Imagem do Usuário' width='32' height='32' class='rounded-circle'>
                                                    </a>
                                                </td>";
                                            echo "<td>" . htmlspecialchars($row['nif']) . "</td>";
                                            echo "<td>" . htmlspecialchars($row['nome']) . "</td>";
                                            echo "<td>" . htmlspecialchars($row['users']) . "</td>";
                                        echo "</tr>";
                                    }
                                } else {
                                    echo "<tr><td colspan='4'>Nenhum produto vendido ainda.</td></tr>";
                                }
                            ?>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>