<?php
/**
 * app/views/partials/topbar.php
 *
 * Expects the currently authenticated user to be available via
 * App\Core\Auth::user() — no data needs to be passed in explicitly.
 */
$currentUser = \App\Core\Auth::user();
$unreadCount = \App\Services\NotificationService::unreadCount((int) $currentUser['id']);
$initials = mb_strtoupper(mb_substr($currentUser['first_name'], 0, 1) . mb_substr($currentUser['last_name'] ?? '', 0, 1));
?>
<header class="ab-topbar">
    
    <!-- Google tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-MFHPEGL9GK"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'G-MFHPEGL9GK');
    </script>

  <button class="ab-burger d-none d-lg-flex" id="abCollapseBtn" aria-label="Collapse sidebar"><i class="bi bi-layout-sidebar"></i></button>
  <button class="ab-burger d-lg-none" id="abMobileMenuBtn" aria-label="Open menu"><i class="bi bi-list"></i></button>

  <div class="ab-search position-relative">
    <i class="bi bi-search"></i>
    <input type="search" id="abGlobalSearchInput" placeholder="Search leads, customers, companies..." aria-label="Global search" autocomplete="off">
    <div id="abGlobalSearchResults" class="dropdown-menu shadow-sm" style="display:none; width:340px; max-height:420px; overflow-y:auto;"></div>
  </div>

  <div class="ab-topbar-actions">
    <div class="dropdown">
      <button class="ab-icon-btn" data-bs-toggle="dropdown" aria-label="Notifications" id="abNotifBellBtn">
        <i class="bi bi-bell"></i>
        <?php if ($unreadCount > 0): ?><span class="ab-badge-count" id="abNotifBadge"><?= (int) $unreadCount ?></span><?php endif; ?>
      </button>
      <div class="dropdown-menu dropdown-menu-end ab-dropdown-panel p-0" id="abNotifPanel">
        <div class="ab-dp-head"><h6>Notifications</h6><a href="#" id="abMarkAllReadLink" class="small fw-semibold">Mark all read</a></div>
        <div id="abNotifList"><div class="p-3 text-center small ab-text-muted">Loading…</div></div>
        <div class="ab-dp-foot"><a href="<?= base_url('notifications') ?>">View all notifications</a></div>
      </div>
    </div>

    <div class="dropdown">
      <button class="ab-profile-btn" data-bs-toggle="dropdown" aria-label="Account menu">
        <div class="ab-avatar">
          <?php if (!empty($currentUser['avatar_path'])): ?>
            <img src="<?= e(base_url('uploads-view/' . $currentUser['avatar_path'])) ?>" alt="" style="width:100%;height:100%;object-fit:cover;">
          <?php else: ?>
            <?= e($initials) ?>
          <?php endif; ?>
        </div>
        <div class="ab-profile-meta d-none d-md-block">
          <strong><?= e($currentUser['first_name'] . ' ' . ($currentUser['last_name'] ?? '')) ?></strong>
          <span><?= e($currentUser['job_title'] ?? $currentUser['role_name'] ?? '') ?></span>
        </div>
        <i class="bi bi-chevron-down small d-none d-md-inline ab-text-faint"></i>
      </button>
      <ul class="dropdown-menu dropdown-menu-end shadow-sm" style="border-radius:12px; border:1px solid var(--ab-border);">
        <li><a class="dropdown-item" href="<?= base_url('profile') ?>"><i class="bi bi-person me-2"></i>My Profile</a></li>
        <li><hr class="dropdown-divider"></li>
        <li>
          <form action="<?= base_url('logout') ?>" method="POST" id="abLogoutForm">
            <?= csrf_field() ?>
            <button type="submit" class="dropdown-item text-danger border-0 bg-transparent w-100 text-start"><i class="bi bi-box-arrow-right me-2"></i>Sign Out</button>
          </form>
        </li>
      </ul>
    </div>
  </div>
</header>
