<?php
require_once __DIR__ . '/../config/bootstrap.php';
admin_require_login();

$pdo = db();

// Métricas rápidas (para a tela de visão geral)
$totalEventos = (int)$pdo->query('SELECT COUNT(*) FROM eventos')->fetchColumn();
$totalFrames  = (int)$pdo->query('SELECT COUNT(*) FROM frames')->fetchColumn();
$totalClientes = (int)$pdo->query('SELECT COUNT(*) FROM clientes')->fetchColumn();

// Status dos eventos (mini-tabela no card)
$stS = $pdo->query("SELECT status, COUNT(*) AS qtd FROM eventos GROUP BY status");
$map = ['draft'=>0,'published'=>0,'archived'=>0];
foreach ($stS as $r) {
  $k = (string)$r['status'];
  if (isset($map[$k])) $map[$k] = (int)$r['qtd'];
}

$title = 'Admin - Visão Geral';
require __DIR__ . '/_partials/header.php';

$base = app_base_path();
$iconMagic = $base . '/assets/img/icon-magic.svg';
$iconPicture = $base . '/assets/img/icon-picture.svg';
$iconUser = $base . '/assets/img/icon-user.svg';
$iconBrand = $base . '/assets/img/icon-brand.svg';

$icoTotal = $base . '/assets/img/icon-check-gray.svg';
$icoDraft = $base . '/assets/img/icon-wait.svg';
$icoPub = $base . '/assets/img/icon-check-success.svg';
?>

<div class="">
  <div class="fb-card-h">
    <h1 class="fb-h1">Bem vindo, Admin</h1>
    <div class="fb-sub">Confira os dados registrados na plataforma</div>
  </div>
  <div class="fb-card-b">
    <div class="row g-3">
      <div class="col-md-3">
        <a class="text-decoration-none" href="events.php">
          <div class="p-3 border rounded-4 bg-white h-100">
            <div class="align-items-center text-center">
              <img src="<?= htmlspecialchars($iconMagic); ?>" alt="" style="margin-top:10px; width:44px;height:44px;">
              <div class="fw-bold mt-3" style="color: gray;">Eventos</div>
              <div class="text-muted small">Crie, Edite e Publique.</div>
            </div>
            <div class="mt-3 text-center">
              <div class="d-inline-block" style="min-width:170px;">
                <div class="d-flex align-items-center justify-content-center gap-2 mb-1">
                  <img src="<?= htmlspecialchars($icoTotal); ?>" alt="Total" style="width:20px;height:20px;">
                  <div class="small text-muted">Total</div>
                  <div class="fw-bold text-dark"><?= $totalEventos; ?></div>
                </div>
                <div class="d-flex align-items-center justify-content-center gap-2 mb-1">
                  <img src="<?= htmlspecialchars($icoDraft); ?>" alt="Rascunhos" style="width:20px;height:20px;">
                  <div class="small text-muted">Rascunho</div>
                  <div class="fw-bold text-dark"><?= (int)$map['draft']; ?></div>
                </div>
                <div class="d-flex align-items-center justify-content-center gap-2">
                  <img src="<?= htmlspecialchars($icoPub); ?>" alt="Publicados" style="width:20px;height:20px;">
                  <div class="small text-muted">Publicado</div>
                  <div class="fw-bold text-dark"><?= (int)$map['published']; ?></div>
                </div>
                <div class="d-flex align-items-center justify-content-center gap-2 mt-1">
                  <img src="<?= htmlspecialchars($icoTotal); ?>" alt="Arquivados" style="width:20px;height:20px;opacity:.75;">
                  <div class="small text-muted">Arquivado</div>
                  <div class="fw-bold text-dark"><?= (int)$map['archived']; ?></div>
                </div>
              </div>
            </div>
          </div>
        </a>
      </div>

      <div class="col-md-3">
        <a class="text-decoration-none" href="frames.php">
          <div class="p-3 border rounded-4 bg-white h-100">
            <div class="align-items-center text-center">
              <img src="<?= htmlspecialchars($iconPicture); ?>" alt="" style="margin-top:10px; width:44px;height:44px;">
              <div class="fw-bold mt-3" style="color: gray;">Frames</div>
              <div class="text-muted small">Gerados pelos clientes</div>
            </div>
            <div class="mt-3 text-center">
              <span class="draft"><?= $totalFrames; ?> total</span>
            </div>
          </div>
        </a>
      </div>

      <div class="col-md-3">
        <a class="text-decoration-none" href="clients.php">
          <div class="p-3 border rounded-4 bg-white h-100">
            <div class="align-items-center text-center">
              <img src="<?= htmlspecialchars($iconUser); ?>" alt="" style="margin-top:10px; width:44px;height:44px;">
              <div class="fw-bold mt-3" style="color: gray;">Clientes</div>
              <div class="text-muted small">Tokens / vínculos</div>
            </div>
            <div class="mt-3 text-center">
              <span class="draft"><?= $totalClientes; ?> total</span>
            </div>
          </div>
        </a>
      </div>
      <div class="col-md-3">
        <a class="text-decoration-none" href="brand.php">
          <div class="p-3 border rounded-4 bg-white h-100">
            <div class="align-items-center text-center">
              <img src="<?= htmlspecialchars($iconBrand); ?>" alt="" style="margin-top:10px; width:44px;height:44px;">
              <div class="fw-bold mt-3" style="color: gray;">Marca</div>
              <div class="text-muted small">Whitelabel</div>
            </div>
            <div class="mt-3 text-center">
              <span class="draft">Configurar</span>
            </div>
          </div>
        </a>
      </div>
    </div>
  </div>
</div>

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