<?php
declare(strict_types=1);
require_once __DIR__ . '/bootstrap.php';
require_once __DIR__ . '/flash.php';
require_once __DIR__ . '/crud-helpers.php';
require_admin();

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$pg = $page ?? 'dash';
$pageTitleSafe = isset($pageTitle) ? (string)$pageTitle : 'Admin';
$pageSubtitle = $pageSubtitle ?? '';
$breadcrumbs = $breadcrumbs ?? [
    ['label' => 'Dashboard', 'href' => 'index.php'],
    ['label' => $pageTitleSafe, 'href' => null],
];
$headerActions = $headerActions ?? '';
$flash = admin_flash_pull();
$adminName = $_SESSION['admin_username'] ?? 'Admin';
$adminCssVersion = (string)@filemtime(__DIR__ . '/../assets/admin.css');

/** @return int */
$countRow = static function (string $table): int {
    try {
        $v = db()->query('SELECT COUNT(*) FROM ' . $table)->fetchColumn();
        return (int)$v;
    } catch (Throwable $e) {
        return 0;
    }
};

$adminStats = [
    ['label' => 'Services', 'icon' => 'bi-grid-1x2', 'value' => $countRow('services'), 'href' => 'services.php'],
    ['label' => 'Projects', 'icon' => 'bi-folder2-open', 'value' => $countRow('projects'), 'href' => 'projects.php'],
    ['label' => 'Jobs', 'icon' => 'bi-briefcase', 'value' => $countRow('job_openings'), 'href' => 'jobs.php'],
    ['label' => 'Team', 'icon' => 'bi-people', 'value' => $countRow('team_members'), 'href' => 'team.php'],
    ['label' => 'Clients', 'icon' => 'bi-building', 'value' => $countRow('clients'), 'href' => 'clients.php'],
    ['label' => 'Testimonials', 'icon' => 'bi-chat-quote', 'value' => $countRow('testimonials'), 'href' => 'testimonials.php'],
    ['label' => 'FAQ', 'icon' => 'bi-question-circle', 'value' => $countRow('faqs'), 'href' => 'faqs.php'],
    ['label' => 'Enquiries', 'icon' => 'bi-inbox', 'value' => $countRow('enquiries'), 'href' => 'enquiries.php'],
    ['label' => 'Pricing', 'icon' => 'bi-ui-checks', 'value' => $countRow('pricing_plans'), 'href' => 'pricing.php'],
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= htmlspecialchars($pageTitleSafe, ENT_QUOTES, 'UTF-8') ?> — Bhatt Enterprises</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
  <link href="assets/admin.css?v=<?= htmlspecialchars($adminCssVersion, ENT_QUOTES, 'UTF-8') ?>" rel="stylesheet">
</head>
<body class="admin-body">
<div id="sidebarBackdrop" class="admin-sidebar-backdrop d-lg-none" onclick="document.getElementById('adminSidebar').classList.remove('show');this.classList.remove('show');"></div>
<div class="admin-app d-flex">
  <aside id="adminSidebar" class="admin-sidebar flex-shrink-0">
    <div class="admin-sidebar-brand">
      <div class="d-flex align-items-center gap-2 mb-1">
        <img src="../assets/img/logo/logo-3.png" alt="Bhatt Enterprises" style="width:28px;height:28px;object-fit:contain;">
        <h1 class="mb-0">Bhatt Enterprises</h1>
      </div>
      <small>Content management</small>
    </div>
    <nav class="nav flex-column py-2 pb-4">
      <div class="admin-nav-section">Overview</div>
      <a class="nav-link <?= $pg === 'dash' ? 'active' : '' ?>" href="index.php"><i class="bi bi-speedometer2"></i> Dashboard</a>

      <div class="admin-nav-section mt-2">Homepage</div>
      <!-- <a class="nav-link <?= $pg === 'home' ? 'active' : '' ?>" href="home.php"><i class="bi bi-house"></i> Homepage text</a> -->
      <a class="nav-link <?= $pg === 'banners' ? 'active' : '' ?>" href="banners.php"><i class="bi bi-images"></i> Hero banners</a>
      <a class="nav-link <?= $pg === 'pricing' ? 'active' : '' ?>" href="pricing.php"><i class="bi bi-currency-rupee"></i> Home pricing</a>

      <div class="admin-nav-section mt-2">Catalog</div>
      <a class="nav-link <?= $pg === 'services' ? 'active' : '' ?>" href="services.php"><i class="bi bi-grid-1x2"></i> Services</a>
      <a class="nav-link <?= $pg === 'projects' ? 'active' : '' ?>" href="projects.php"><i class="bi bi-folder2-open"></i> Projects</a>
      <a class="nav-link <?= $pg === 'jobs' ? 'active' : '' ?>" href="jobs.php"><i class="bi bi-briefcase"></i> Job openings</a>

      <div class="admin-nav-section mt-2">People & trust</div>
      <a class="nav-link <?= $pg === 'team' ? 'active' : '' ?>" href="team.php"><i class="bi bi-people"></i> Team</a>
      <a class="nav-link <?= $pg === 'clients' ? 'active' : '' ?>" href="clients.php"><i class="bi bi-building"></i> Clients</a>
      <a class="nav-link <?= $pg === 'testimonials' ? 'active' : '' ?>" href="testimonials.php"><i class="bi bi-chat-quote"></i> Testimonials</a>

      <div class="admin-nav-section mt-2">Content</div>
      <a class="nav-link <?= $pg === 'faqs' ? 'active' : '' ?>" href="faqs.php"><i class="bi bi-question-circle"></i> FAQ</a>
      <a class="nav-link <?= $pg === 'pages' ? 'active' : '' ?>" href="static-pages.php"><i class="bi bi-file-text"></i> Legal pages</a>

      <div class="admin-nav-section mt-2">Site & leads</div>
      <a class="nav-link <?= $pg === 'contact' ? 'active' : '' ?>" href="contact-settings.php"><i class="bi bi-telephone"></i> Contact &amp; map</a>
      <a class="nav-link <?= $pg === 'enquiries' ? 'active' : '' ?>" href="enquiries.php"><i class="bi bi-inbox"></i> Enquiries</a>
    </nav>
  </aside>

  <div class="flex-grow-1 d-flex flex-column min-vh-100">
    <header class="admin-topbar">
      <div class="d-flex align-items-center gap-3 flex-wrap">
        <button type="button" class="btn btn-outline-secondary btn-sm d-lg-none" id="btnSidebar" aria-label="Toggle menu">
          <i class="bi bi-list"></i>
        </button>
        <div class="text-secondary small"><?= admin_breadcrumbs($breadcrumbs) ?></div>
      </div>
      <div class="d-flex align-items-center gap-2 flex-wrap">
        <span class="text-muted small d-none d-sm-inline"><i class="bi bi-person-circle me-1"></i><?= htmlspecialchars($adminName, ENT_QUOTES, 'UTF-8') ?></span>
        <a href="../index.php" class="btn btn-outline-primary btn-sm" target="_blank" rel="noopener"><i class="bi bi-box-arrow-up-right me-1"></i>View site</a>
        <a href="logout.php" class="btn btn-outline-danger btn-sm"><i class="bi bi-box-arrow-right me-1"></i>Logout</a>
      </div>
    </header>

    <main class="flex-grow-1 p-3 p-lg-4">
      <div class="admin-main-inner">
        <?php if ($flash): ?>
          <div class="alert alert-<?= $flash['type'] === 'success' ? 'success' : ($flash['type'] === 'danger' ? 'danger' : 'info') ?> alert-dismissible fade show shadow-sm" role="alert">
            <?= htmlspecialchars($flash['message'], ENT_QUOTES, 'UTF-8') ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
          </div>
        <?php endif; ?>

        <div class="admin-page-header d-flex flex-column flex-md-row justify-content-between align-items-start align-items-md-center gap-3 mb-4">
          <div class="admin-page-title">
            <h1><?= htmlspecialchars($pageTitleSafe, ENT_QUOTES, 'UTF-8') ?></h1>
            <?php if ($pageSubtitle !== ''): ?><p class="admin-page-subtitle"><?= htmlspecialchars($pageSubtitle, ENT_QUOTES, 'UTF-8') ?></p><?php endif; ?>
          </div>
          <?php if ($headerActions !== ''): ?>
            <div class="d-flex flex-wrap gap-2 admin-header-actions"><?= $headerActions ?></div>
          <?php endif; ?>
        </div>

        <?php if ($pg === 'dash'): ?>
        <div class="admin-kpi-section mb-4">
          <p class="admin-kpi-section-label">Shortcuts</p>
          <div class="admin-kpi-row">
            <?php foreach ($adminStats as $stat): ?>
              <a class="admin-kpi-card" href="<?= htmlspecialchars($stat['href'], ENT_QUOTES, 'UTF-8') ?>">
                <span class="admin-kpi-icon" aria-hidden="true"><i class="bi <?= htmlspecialchars($stat['icon'], ENT_QUOTES, 'UTF-8') ?>"></i></span>
                <span class="admin-kpi-meta">
                  <span class="admin-kpi-value"><?= (int)$stat['value'] ?></span>
                  <span class="admin-kpi-label"><?= htmlspecialchars($stat['label'], ENT_QUOTES, 'UTF-8') ?></span>
                </span>
              </a>
            <?php endforeach; ?>
          </div>
        </div>
        <?php endif; ?>
