<?php send_csp(); ?>
<?php send_csp(); ?>
<!DOCTYPE html>
<html lang="pt">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= h($title??'Admin') ?> — <?= h(config('app.name')) ?></title>
<link rel="stylesheet" href="<?= base_url('css/admin.css') ?>">
</head>
<body>

<?php if (!empty($_SESSION['admin'])): ?>
<?php
$cur  = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$base = parse_url(config('app.url'), PHP_URL_PATH) ?? '';
$path = $base ? substr($cur, strlen($base)) : $cur;
$path = '/' . trim($path, '/');

function isActive(string $match, string $path): string {
    return (str_starts_with($path, $match) || $path === $match) ? 'active' : '';
}
?>

<div class="layout">
  <!-- Sidebar -->
  <aside class="sidebar">
    <div class="sb-brand">
      <div style="display:flex;align-items:center;gap:10px;margin-bottom:4px;">
        <span style="font-size:24px;">📸</span>
        <div>
          <div class="sb-brand-name"><?= h(config('app.name')) ?></div>
          <div class="sb-brand-sub">Painel de Administração</div>
        </div>
      </div>
    </div>

    <div class="sb-section">
      <div class="sb-label">Menu</div>
      <a href="<?= base_url('admin') ?>" class="sb-link <?= $path==='/admin'||$path==='/admin/'?'active':'' ?>">
        <span class="icon">▦</span> Dashboard
      </a>
      <a href="<?= base_url('admin/eventos') ?>" class="sb-link <?= isActive('/admin/eventos', $path) ?>">
        <span class="icon">🖼</span> Eventos
      </a>
      <a href="<?= base_url('admin/encomendas') ?>" class="sb-link <?= isActive('/admin/encomendas', $path) ?>">
        <span class="icon">📦</span> Encomendas
      </a>
      <a href="<?= base_url('admin/definicoes') ?>" class="sb-link <?= isActive('/admin/definicoes', $path) ?>">
        <span class="icon">⚙️</span> Definições
      </a>
    </div>

    <div class="sb-footer">
      <a href="<?= base_url() ?>" target="_blank" class="sb-link" style="font-size:12px;">
        <span class="icon">↗</span> Ver Site Público
      </a>
      <a href="<?= base_url('admin/logout') ?>" class="sb-link" style="font-size:12px;color:#F87171;">
        <span class="icon">⎋</span> Terminar Sessão
      </a>
    </div>
  </aside>

  <!-- Main -->
  <div class="main">
    <!-- Topbar -->
    <header class="topbar">
      <div>
        <?php if(isset($breadcrumbs) && $breadcrumbs): ?>
        <div class="breadcrumb">
          <?php foreach($breadcrumbs as $i=>[$label,$url]): ?>
          <?php if($i>0): ?><span class="breadcrumb-sep">›</span><?php endif; ?>
          <?php if($url && $i < count($breadcrumbs)-1): ?>
            <a href="<?= $url ?>"><?= h($label) ?></a>
          <?php else: ?>
            <span style="color:#1D1D1F;font-weight:600;"><?= h($label) ?></span>
          <?php endif; ?>
          <?php endforeach; ?>
        </div>
        <?php else: ?>
        <div class="page-title"><?= h($title??'Admin') ?></div>
        <?php endif; ?>
      </div>
      <div class="tb-actions">
        <?php if(isset($topActions)): echo $topActions; endif; ?>
        <div style="display:flex;align-items:center;gap:8px;padding:6px 12px;background:#F2F2F7;border-radius:999px;">
          <span style="font-size:12px;color:#6E6E73;">👤</span>
          <span style="font-size:13px;font-weight:600;"><?= h($_SESSION['admin_user']??'Admin') ?></span>
        </div>
      </div>
    </header>

    <!-- Content -->
    <div class="content">
      <?php if($m=flash('success')): ?>
      <div class="alert alert-success">✓ <?= h($m) ?></div>
      <?php endif; ?>
      <?php if($m=flash('error')): ?>
      <div class="alert alert-error">✕ <?= h($m) ?></div>
      <?php endif; ?>
      <?= $content ?>
    </div>
  </div>
</div>

<?php else: ?>
<?= $content ?>
<?php endif; ?>

</body>
</html>
