<?php
$title = 'Dashboard';
$breadcrumbs = [['Dashboard', null]];
?>

<!-- Stats -->
<div class="stats-grid">
  <?php foreach([
    ['🖼', 'Eventos',    $eventCount,          '#FFF7ED', 'Ativos e ocultos'],
    ['📷', 'Fotografias',$photoCount,           '#F0F9FF', 'Total carregadas'],
    ['📦', 'Encomendas', $stats['total']??0,   '#FAF5FF', 'Total recebidas'],
    ['⏳', 'Pendentes',  $stats['pending']??0,  '#FFFBEB', 'Aguardam aprovação'],
    ['💶', 'Receita',    money($stats['revenue']??0), '#ECFDF5', 'Encomendas aprovadas'],
  ] as [$icon, $label, $val, $bg, $hint]): ?>
  <div class="stat-card">
    <div class="stat-icon" style="background:<?= $bg ?>"><?= $icon ?></div>
    <div class="stat-val"><?= $val ?></div>
    <div class="stat-lbl"><?= $label ?></div>
    <div style="font-size:11px;color:#AEAEB2;margin-top:2px;"><?= $hint ?></div>
  </div>
  <?php endforeach; ?>
</div>

<!-- Acesso rápido -->
<div style="display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-bottom:24px;">
  <a href="<?= base_url('admin/eventos') ?>"
     style="background:#1C1C1E;border-radius:14px;padding:18px 20px;color:#fff;display:flex;align-items:center;justify-content:space-between;transition:background .15s;"
     onmouseover="this.style.background='#2C2C2E'" onmouseout="this.style.background='#1C1C1E'">
    <div>
      <div style="font-weight:700;font-size:15px;">Gerir Eventos</div>
      <div style="font-size:12px;color:#AEAEB2;margin-top:2px;">Criar eventos e carregar fotos</div>
    </div>
    <span style="font-size:24px;opacity:.6;">🖼</span>
  </a>
  <a href="<?= base_url('admin/encomendas?status=pending') ?>"
     style="background:#FFFBEB;border:1px solid #FDE68A;border-radius:14px;padding:18px 20px;color:#1D1D1F;display:flex;align-items:center;justify-content:space-between;transition:all .15s;"
     onmouseover="this.style.background='#FEF3C7'" onmouseout="this.style.background='#FFFBEB'">
    <div>
      <div style="font-weight:700;font-size:15px;">Encomendas Pendentes</div>
      <div style="font-size:12px;color:#92400E;margin-top:2px;"><?= $stats['pending']??0 ?> a aguardar aprovação</div>
    </div>
    <span style="font-size:24px;">⏳</span>
  </a>
</div>

<!-- Tabela últimas encomendas -->
<div class="card">
  <div class="card-header">
    <div>
      <div class="card-title">Últimas Encomendas</div>
      <div class="card-sub">As 10 mais recentes</div>
    </div>
    <a href="<?= base_url('admin/encomendas') ?>" class="btn btn-ghost btn-sm">Ver todas →</a>
  </div>
  <div class="tbl-wrap">
    <table class="tbl">
      <thead><tr>
        <th>Número</th>
        <th>Cliente</th>
        <th>Evento</th>
        <th>Total</th>
        <th>Estado</th>
        <th>Data</th>
        <th></th>
      </tr></thead>
      <tbody>
      <?php if(empty($recent)): ?>
      <tr><td colspan="7" class="tbl-empty">Ainda não há encomendas. Quando os clientes fizerem pedidos aparecem aqui.</td></tr>
      <?php else: ?>
      <?php foreach($recent as $o): ?>
      <tr>
        <td><span class="tbl-mono" style="color:#7C3AED;"><?= h($o['order_number']) ?></span></td>
        <td>
          <div class="tbl-name"><?= h($o['customer_name']) ?></div>
          <div class="tbl-sub"><?= h($o['email']) ?></div>
        </td>
        <td class="text-muted"><?= h($o['event_name']?:'—') ?></td>
        <td><strong><?= money($o['total']) ?></strong></td>
        <td><span class="badge badge-<?= $o['status'] ?>"><?= match($o['status']){'pending'=>'Pendente','paid'=>'Pago','sent'=>'Enviado',default=>'Cancelado'} ?></span></td>
        <td class="text-muted" style="font-size:12px;white-space:nowrap;"><?= date('d/m H:i',strtotime($o['created_at'])) ?></td>
        <td><a href="<?= base_url('admin/encomendas/'.$o['id']) ?>" class="btn btn-ghost btn-sm">Ver →</a></td>
      </tr>
      <?php endforeach; ?>
      <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
