<?php
require_once __DIR__ . '/includes/bootstrap.php';

$pageTitle = 'Dashboard OmniMax';
$pageSubtitle = 'Visão geral do painel, das conexões e das APIs utilizadas pelo aplicativo.';
$pageIcon = 'fa-chart-line';

require __DIR__ . '/includes/header.php';

$totalServers = (int)db()->query('SELECT COUNT(*) FROM servers')->fetchColumn();
$activeServers = (int)db()->query('SELECT COUNT(*) FROM servers WHERE active=1 AND authorized=1')->fetchColumn();
$totalDevices = (int)db()->query('SELECT COUNT(*) FROM devices')->fetchColumn();

$recent = db()->query('SELECT * FROM servers ORDER BY priority ASC, name ASC LIMIT 5')->fetchAll();

$maintenance = get_config('maintenance');
$core = get_config('core');
?>

<div class="row max-stat-row">
    <div class="col-sm-6 col-xl-4 mb-4">
        <div class="card dashboard-stat-card h-100">
            <div class="card-body">
                <div class="dashboard-stat-icon">
                    <i class="fa-solid fa-server"></i>
                </div>
                <span>DNS cadastradas</span>
                <strong><?=$totalServers?></strong>
            </div>
        </div>
    </div>

    <div class="col-sm-6 col-xl-4 mb-4">
        <div class="card dashboard-stat-card h-100">
            <div class="card-body">
                <div class="dashboard-stat-icon">
                    <i class="fa-solid fa-circle-check"></i>
                </div>
                <span>DNS liberadas no app</span>
                <strong><?=$activeServers?></strong>
            </div>
        </div>
    </div>

    <div class="col-sm-6 col-xl-4 mb-4">
        <div class="card dashboard-stat-card h-100">
            <div class="card-body">
                <div class="dashboard-stat-icon">
                    <i class="fa-solid fa-tv"></i>
                </div>
                <span>Dispositivos registrados</span>
                <strong><?=$totalDevices?></strong>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-xl-8 mb-4">
        <div class="card bg-primary text-white h-100">
            <div class="card-header card-header-warning">
                <div class="max-card-title">
                    <div class="max-card-title-icon">
                        <i class="fa-solid fa-server"></i>
                    </div>
                    <div>
                        <h2>Servidores prioritários</h2>
                        <p>Primeiras conexões que serão enviadas ao aplicativo.</p>
                    </div>
                </div>
            </div>

            <div class="card-body p-0">
                <?php if (!$recent): ?>
                    <div class="empty-state">
                        <i class="fa-solid fa-server"></i>
                        Nenhuma DNS cadastrada.
                    </div>
                <?php else: ?>
                    <div class="table-responsive dashboard-table-wrap">
                        <table class="table table-striped table-sm">
                            <thead>
                                <tr>
                                    <th>Prioridade</th>
                                    <th>Nome</th>
                                    <th>URL</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($recent as $server): ?>
                                    <tr>
                                        <td><strong><?=e($server['priority'])?></strong></td>
                                        <td><?=e($server['name'])?></td>
                                        <td><code><?=e($server['url'])?></code></td>
                                        <td>
                                            <span class="badge <?=$server['active'] && $server['authorized'] ? 'active' : 'inactive'?>">
                                                <?=$server['active'] && $server['authorized'] ? 'Liberado' : 'Bloqueado'?>
                                            </span>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-xl-4 mb-4">
        <div class="card bg-primary text-white h-100">
            <div class="card-header card-header-warning">
                <div class="max-card-title">
                    <div class="max-card-title-icon">
                        <i class="fa-solid fa-bolt"></i>
                    </div>
                    <div>
                        <h2>Acesso rápido</h2>
                        <p>Atalhos para os ajustes mais utilizados.</p>
                    </div>
                </div>
            </div>

            <div class="card-body">
                <div class="max-quick-grid">
                    <a class="max-quick-link" href="<?=e(panel_url('dns.php'))?>">
                        <i class="fa-solid fa-plus"></i>
                        <span>Nova DNS</span>
                    </a>

                    <a class="max-quick-link" href="<?=e(panel_url('app.php'))?>">
                        <i class="fa-solid fa-mobile-screen"></i>
                        <span>Aplicativo</span>
                    </a>

                    <a class="max-quick-link" href="<?=e(panel_url('messages.php'))?>">
                        <i class="fa-solid fa-bullhorn"></i>
                        <span>Mensagens</span>
                    </a>
                </div>

                <div class="dashboard-status-box">
                    <p>
                        <strong>Status do aplicativo:</strong>
                        <span class="badge <?=$core['status']==='ACTIVE'?'active':'inactive'?>">
                            <?=e($core['status'])?>
                        </span>
                    </p>

                    <p>
                        <strong>Manutenção:</strong>
                        <span class="badge <?=$maintenance['mnt_status']==='ACTIVE'?'inactive':'active'?>">
                            <?=e($maintenance['mnt_status'])?>
                        </span>
                    </p>

                    <p class="muted mb-0">
                        As alterações salvas são lidas pelo aplicativo na próxima atualização da configuração.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require __DIR__ . '/includes/footer.php'; ?>