<?php
// includes/header.php
if (session_status() !== PHP_SESSION_ACTIVE) session_start();
require_once __DIR__ . '/auth.php';
$U = ec_user();
$ACTIVE = $ACTIVE ?? '';

// Avatar fallback (treat empty/"0" as missing)
$__avatar = (string)($U['avatar_url'] ?? '');
if ($__avatar === '' || $__avatar === '0') {
  $__avatar = '/edenrms/assets/img/avatar-default.jpg';
}
$__name = htmlspecialchars($U['name'] ?? 'User', ENT_QUOTES, 'UTF-8');
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Eden RMS</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
  <script defer src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

  <style>
    :root{
      /* Gradient colors for unified design */
      --gradient-start: #0b1e3a;
      --gradient-end: #0b1e3a;
      --ec-body-bg:#f4f6f9;
      --ec-topbar-h: 40px;
      --ec-sidebar-w: 260px;
    }

    html, body {
      height: 100%;
      margin: 0;
      padding: 0;
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    }

    body {
      background: var(--ec-body-bg);
      padding-top: var(--ec-topbar-h);
      overflow-x: hidden;
    }

    /* ===== TOPBAR ===== */
    .ec-topbar {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      z-index: 1060;
      background: linear-gradient(135deg, var(--gradient-start) 0%, var(--gradient-end) 100%);
      border-bottom: 1px solid rgba(0,0,0,0.15);
    }
    
    .ec-topbar.navbar {
      padding: 0 !important;
      min-height: var(--ec-topbar-h) !important;
      transition: all 0.3s ease;
    }
    
    .ec-topbar .container-fluid {
      min-height: var(--ec-topbar-h) !important;
      padding: 0 0.5rem !important;
    }
    
    .ec-topbar .navbar-brand {
      font-size: 0.92rem !important;
      line-height: 1 !important;
      padding: 0 0.25rem !important;
      margin: 0 !important;
      color: #fff !important;
      font-weight: 600 !important;
      text-shadow: 0 1px 2px rgba(0,0,0,0.2);
    }
    
    .ec-topbar .ms-auto.d-flex {
      gap: 0.5rem !important;
    }
    
    .ec-topbar .btn {
      padding: 0.07rem 0.32rem !important;
      font-size: 0.74rem !important;
      border-radius: 0.30rem !important;
      line-height: 1 !important;
      border: none !important;
      background: rgba(255,255,255,0.1) !important;
      color: #fff !important;
      transition: all 0.2s ease;
    }
    
    .ec-topbar .btn:hover {
      background: rgba(255,255,255,0.2) !important;
      transform: translateY(-2px);
    }
    
    .ec-topbar .btn:focus {
      box-shadow: 0 0 0 0.2rem rgba(255,255,255,0.2) !important;
    }
    
    .ec-avatar {
      width: 20px !important;
      height: 20px !important;
      border-radius: 50%;
      object-fit: cover;
      box-shadow: 0 0 0 1px rgba(255,255,255,0.3);
    }

    /* ===== SIDEBAR ===== */
    .ec-sidebar {
      position: fixed;
      left: 0;
      top: var(--ec-topbar-h);
      height: calc(100vh - var(--ec-topbar-h));
      width: var(--ec-sidebar-w);
      background: linear-gradient(135deg, var(--gradient-start) 0%, var(--gradient-end) 100%);
      color: #e7efff;
      box-shadow: 0 0 12px rgba(0,0,0,0.15);
      overflow-y: auto;
      -webkit-overflow-scrolling: touch;
      z-index: 1040;
      transform: translateX(-100%);
      transition: transform 0.25s ease;
    }
    
    .ec-sidebar .menu-title {
      font-size: 0.75rem;
      letter-spacing: 0.06em;
      text-transform: uppercase;
      color: #c6d4ff;
      margin: 0.35rem 0.5rem 0.35rem;
      font-weight: 600;
    }
    
    .ec-sidebar .nav-link {
      font-size: 0.88rem;
      padding: 0.55rem 0.70rem;
      color: #e7efff;
      border-radius: 12px;
      margin: 2px 6px;
      display: flex;
      align-items: center;
      gap: 0.55rem;
      transition: all 0.2s ease;
      text-shadow: 0 1px 1px rgba(0,0,0,0.2);
    }
    
    .ec-sidebar .nav-link:hover, 
    .ec-sidebar .nav-link.active {
      background: linear-gradient(135deg, #0b1e3a 10%, #0b1e3a 100%) !important;
      color: #ffffff !important;
      font-weight: 600 !important;
      border-left: 3px solid #0d6efd !important;
      box-shadow: inset 4px 0 0 #0d6efd !important;
      transform: translateX(3px);
    }
    
    .ec-sidebar .nav-link i {
      font-size: 1.05rem;
    }
    
    /* ===== LAYOUT ===== */
    .ec-layout {
      display: flex;
      min-height: 100vh;
    }
    
    main.ec-main {
      flex: 1 1 auto;
      min-width: 0;
      background: var(--ec-body-bg);
      transition: margin-left 0.25s ease;
    }
    
    /* ===== MOBILE SIDEBAR ===== */
    @media (max-width: 991.98px) {
      .ec-sidebar {
        transform: translateX(-100%);
        transition: transform 0.2s ease;
      }
      
      .ec-sidebar.show {
        transform: translateX(0);
      }
      
      .ec-main {
        margin-left: 0;
      }
      
      body.ec-sidebar-open .ec-main {
        position: fixed;
        width: 100%;
        left: 0;
      }
    }
    
    /* ===== DESKTOP SIDEBAR ===== */
    @media (min-width: 992px) {
      .ec-main {
        margin-left: var(--ec-sidebar-w);
      }
    }
    
    /* ===== NOTIFICATIONS ===== */
    .ec-notify-panel {
      position: fixed;
      right: 0;
      width: 360px;
      max-width: 95vw;
      top: var(--ec-topbar-h);
      height: calc(100vh - var(--ec-topbar-h));
      background: #fff;
      border-left: 1px solid #e5e7eb;
      box-shadow: -8px 0 20px rgba(0,0,0,0.08);
      transform: translateX(100%);
      transition: transform 0.2s ease;
      z-index: 1055;
      display: flex;
      flex-direction: column;
      pointer-events: none;
    }
    
    .ec-notify-panel.show {
      transform: translateX(0);
      pointer-events: auto;
    }
    
    .ec-notify-head {
      padding: 10px 12px;
      border-bottom: 1px solid #eaecef;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    
    .ec-notify-list {
      overflow: auto;
      padding: 8px 8px 16px;
    }
    
    /* ===== OVERLAY FOR MOBILE ===== */
    #ecOverlay {
      position: fixed;
      inset: 0;
      background: rgba(2,6,23,0.45);
      display: none;
      z-index: 1035;
    }
    
    /* ===== USER AVATAR ===== */
    .ec-avatar {
      width: 20px !important;
      height: 20px !important;
    }
    
    /* ===== USER PROFILE ===== */
    .sidebar-profile {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 10px;
      border-radius: 8px;
      margin-top: 10px;
    }
    
    .sidebar-profile img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      object-fit: cover;
    }
    
    .sidebar-profile .name {
      font-weight: 600;
      color: #fff;
      text-shadow: 0 1px 1px rgba(0,0,0,0.3);
    }
    
    .sidebar-profile .role {
      font-size: 0.75rem;
      color: #c6d4ff;
      text-shadow: 0 1px 1px rgba(0,0,0,0.3);
    }
  </style>
</head>
<body>

<nav class="navbar ec-topbar">
  <div class="container-fluid">
    <button class="btn btn-outline-light btn-sm d-lg-none" id="ecSidebarToggle"><i class="bi bi-list"></i></button>
    <a class="navbar-brand fw-semibold ms-1" href="/edenrms/pages/dashboard.php">Eden RMS</a>

    <div class="ms-auto d-flex align-items-center gap-2">
      <!-- Notifications -->
      <button class="btn btn-outline-light btn-sm position-relative" id="ecNotifyBtn" title="Notifications" type="button">
        <i class="bi bi-bell"></i>
        <span id="ecNotifyDot" class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger d-none">!</span>
      </button>

      <!-- User dropdown -->
      <div class="dropdown">
        <button class="btn btn-outline-light btn-sm d-flex align-items-center gap-2" data-bs-toggle="dropdown" aria-expanded="false" type="button">
          <img src="<?= htmlspecialchars($__avatar, ENT_QUOTES, 'UTF-8') ?>"
               class="ec-avatar"
               alt=""
               onerror="this.onerror=null;this.src='/edenrms/assets/img/avatar-default.jpg'">
          <span class="d-none d-md-inline"><?= $__name ?></span>
        </button>
        <ul class="dropdown-menu dropdown-menu-end">
          <li><a class="dropdown-item" href="/edenrms/pages/profile.php"><i class="bi bi-person me-2"></i>Profile</a></li>
          <?php if (ec_can('settings.manage') || ec_role()==='admin' || ec_role()==='super_admin'): ?>
            <li><a class="dropdown-item" href="/edenrms/pages/settings.php"><i class="bi bi-gear me-2"></i>Settings</a></li>
          <?php endif; ?>
          <li><a class="dropdown-item" href="/edenrms/pages/change_password.php"><i class="bi bi-key me-2"></i>Change Password</a></li>
          <li><hr class="dropdown-divider"></li>
          <li><a class="dropdown-item" href="/edenrms/auth/logout.php"><i class="bi bi-box-arrow-right me-2"></i>Logout</a></li>
        </ul>
      </div>
    </div>
  </div>
</nav>

<!-- Notifications panel -->
<aside id="ecNotifyPanel" class="ec-notify-panel" aria-hidden="true">
  <div class="ec-notify-head">
    <strong>Notifications</strong>
    <div class="d-flex align-items-center gap-2">
      <button class="btn btn-sm btn-outline-secondary" id="ecMarkAllRead" title="Mark all read" type="button">
        <i class="bi bi-check2-all"></i>
      </button>
      <button class="btn btn-sm btn-outline-secondary" id="ecNotifyClose" title="Close" type="button">
        <i class="bi bi-x-lg"></i>
      </button>
    </div>
  </div>
  <div id="ecNotifyList" class="ec-notify-list">
    <div class="text-center text-muted small p-3">No notifications</div>
  </div>
</aside>

<div class="ec-layout">
<!-- pages include sidebar.php next, then open: <main class="ec-main"> -->

<!-- JS that sets the exact topbar height (run once on load & on resize) -->
<script>
(function(){
  var tb = document.querySelector('.ec-topbar');
  if (!tb) return;
  function setH(){
    var h = tb.getBoundingClientRect().height || 40;
    document.documentElement.style.setProperty('--ec-topbar-h', h + 'px');
  }
  setH();
  addEventListener('resize', setH);
  if (document.fonts && document.fonts.ready) document.fonts.ready.then(setH);
})();
</script>
