<?php

use App\Admin\Modules;
use App\Core\Auth;
use App\Core\View;

Auth::start();
$flash = $_SESSION['flash'] ?? null;
unset($_SESSION['flash']);
$currentUser = Auth::user();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex">
<title>CMS — Touch Therapies Antigua</title>
<link rel="stylesheet" href="<?= View::url('/assets/admin.css') ?>">
</head>
<body>
<div class="shell">
  <aside class="sidebar">
    <a class="brand" href="<?= View::url('/admin') ?>">Touch <em>Therapies</em><span>CMS</span></a>
    <nav>
      <a href="<?= View::url('/admin') ?>" class="<?= ($active ?? '') === 'dashboard' ? 'on' : '' ?>">📊 Dashboard</a>
      <?php foreach (Modules::all() as $key => $m): ?>
        <?php if (!empty($m['hidden'])) continue; ?>
        <a href="<?= View::url("/admin/$key") ?>" class="<?= ($active ?? '') === $key ? 'on' : '' ?>"><?= $m['icon'] ?> <?= View::e($m['label']) ?></a>
      <?php endforeach; ?>
      <a href="<?= View::url('/admin/contacts') ?>" class="<?= ($active ?? '') === 'contacts' ? 'on' : '' ?>">📥 Enquiries</a>
      <a href="<?= View::url('/admin/settings') ?>" class="<?= ($active ?? '') === 'settings' ? 'on' : '' ?>">⚙️ Settings</a>
    </nav>
    <?php if ($currentUser): ?>
    <form class="logout" method="post" action="<?= View::url('/admin/logout') ?>">
      <input type="hidden" name="_csrf" value="<?= Auth::csrfToken() ?>">
      <span><?= View::e($currentUser['name']) ?></span>
      <button type="submit">Sign out</button>
    </form>
    <?php endif; ?>
  </aside>
  <main class="content">
    <?php if ($flash): ?>
      <div class="flash <?= $flash['type'] === 'ok' ? 'ok' : 'err' ?>"><?= View::e($flash['message']) ?></div>
    <?php endif; ?>
