<?php
// Base paths updated for the new subdomain document root
$domain = ' ';
$iconPath = $domain . '/rimax/application/dripend/assets/icons/dashboard';

// Determine current page for active state
$current_page = basename($_SERVER['PHP_SELF']); 

// Simulate fetching user data from session
$userName = isset($_SESSION['user_full_name']) ? $_SESSION['user_full_name'] : 'Dr. Admin';
$userRole = isset($_SESSION['user_role']) ? $_SESSION['user_role'] : 'Administrator';
$userInitial = strtoupper(substr($userName, 0, 1));
?>

<!-- Floating Toggle Button (Visible when sidebar vanishes) -->
<button id="floatingToggle" class="floating-toggle-btn" title="Open Menu">
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg>
</button>

<!-- Mobile Backdrop Overlay -->
<div class="sidebar-overlay" id="sidebarOverlay"></div>

<aside class="sidebar" id="sidebar">
  <div class="sidebar-header">
    <div class="brand-info">
      <div class="brand-logo">e</div>
      <h3>eRHAMS <span>Menu</span></h3>
    </div>
    <button id="sidebarToggle" class="close-btn" title="Hide sidebar">
      <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
    </button>
  </div>
  
  <!-- Logged in User Profile -->
  <div class="user-profile-section">
    <div class="user-card">
      <div class="user-avatar"><?php echo $userInitial; ?></div>
      <div class="user-details">
        <span class="user-name"><?php echo htmlspecialchars($userName); ?></span>
        <span class="user-role"><?php echo htmlspecialchars($userRole); ?></span>
      </div>
    </div>
  </div>
  
  <div class="sidebar-scroll-area">
    <nav class="sidebar-nav">
      <p class="nav-section-title">MAIN MENU</p>
      
      <a href="<?php echo $domain; ?>/rimax/application/dashboard.php" class="nav-link <?php echo ($current_page == 'dashboard.php') ? 'active' : ''; ?>">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/dashboard.png" alt="Dashboard"></div>
        <span>Dashboard</span>
      </a>

      <a href="<?php echo $domain; ?>/rimax/application/views/appointment/appointment_calendar.php" class="nav-link <?php echo ($current_page == 'appointment_calendar.php') ? 'active' : ''; ?>">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/calendar.png" alt="Calendar"></div>
        <span>Appointment Calendar</span>
      </a>
      
      <a href="<?php echo $domain; ?>/rimax/application/views/patient/patient_management_dashboard.php" class="nav-link <?php echo ($current_page == 'patient_management_dashboard.php') ? 'active' : ''; ?>">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/records.png" alt="Records"></div>
        <span>Patient Records</span>
      </a>

      <a href="<?php echo $domain; ?>/rimax/application/views/accounts/accounts_dashboard.php" class="nav-link <?php echo ($current_page == 'accounts_dashboard.php') ? 'active' : ''; ?>">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/accounts.png" alt="Accounts"></div>
        <span>Accounts Management</span>
      </a>
      
      <p class="nav-section-title">DEPARTMENTS</p>

      <a href="<?php echo $domain; ?>/rimax/application/views/laboratory/lab_dashboard.php" class="nav-link <?php echo ($current_page == 'lab_dashboard.php') ? 'active' : ''; ?>">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/investigation.png" alt="Lab"></div>
        <span>Laboratory</span>
      </a>

      <a href="<?php echo $domain; ?>/rimax/application/views/pharmacy/pharmacy_dashboard.php" class="nav-link <?php echo ($current_page == 'pharmacy_dashboard.php') ? 'active' : ''; ?>">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/pharmacy.png" alt="Pharmacy"></div>
        <span>Pharmacy</span>
      </a>

      <a href="#" class="nav-link">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/ipd.png" alt="IPD"></div>
        <span>IPD Management</span>
      </a>

      <a href="#" class="nav-link">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/emergency.png" alt="Emergency"></div>
        <span>Accident & Emergency</span>
      </a>

      <a href="#" class="nav-link">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/dental.png" alt="Dental"></div>
        <span>Dental Clinic</span>
      </a>

      <p class="nav-section-title">ADMINISTRATION</p>

      <a href="<?php echo $domain; ?>/rimax/application/views/insurance/insurance_claims_dashboard.php" class="nav-link <?php echo ($current_page == 'insurance_claims_dashboard.php') ? 'active' : ''; ?>">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/insurance-claims.png" alt="Insurance"></div>
        <span>Insurance Claims</span>
      </a>

      <a href="/rimax/application/views/procurement/listPurchaseRequisition.php" class="nav-link">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/administrator.png" alt="Admin"></div>
        <span>Procurement</span>
      </a>

      <a href="#" class="nav-link">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/report.png" alt="Reports"></div>
        <span>Reports & Analytics</span>
      </a>

      <a href="#" class="nav-link">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/support.png" alt="Support"></div>
        <span>Alerts & Comm.</span>
      </a>

      <a href="/rimax/application/views/system_setup/systemSetup.php" class="nav-link">
        <div class="icon-wrapper"><img src="<?php echo $iconPath; ?>/administrator.png" alt="Admin"></div>
        <span>System Admin</span>
      </a>
    </nav>
  </div>

  <div class="sidebar-footer">
    <button class="btn-primary">
      <span>Get Help</span>
      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle><path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"></path><line x1="12" y1="17" x2="12.01" y2="17"></line></svg>
    </button>
  </div>
</aside>

<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
  --sidebar-bg: #ffffff;
  --sidebar-width: 300px;
  
  --brand-primary: #059669; /* Drip Dental Green */
  --brand-primary-light: #d1fae5;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --hover-bg: #f8fafc;
  --border-color: #e2e8f0;
  
  --transition-speed: 0.35s;
}

body {
  font-family: 'Inter', sans-serif;
  margin: 0;
  padding: 0;
  overflow-x: hidden;
}

/* --- FLOATING TOGGLE BUTTON --- */
.floating-toggle-btn {
  position: fixed;
  top: 20px;
  left: 20px;
  width: 48px;
  height: 48px;
  background: var(--sidebar-bg);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-main);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  z-index: 90;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  opacity: 0;
  visibility: hidden;
  transform: scale(0.8);
}

.floating-toggle-btn.visible {
  opacity: 1;
  visibility: visible;
  transform: scale(1);
}

.floating-toggle-btn:hover {
  background: var(--brand-primary-light);
  color: var(--brand-primary);
  border-color: var(--brand-primary-light);
}

/* --- OVERLAY FOR MOBILE --- */
.sidebar-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(6px);
  z-index: 99;
  opacity: 0;
  visibility: hidden;
  transition: all var(--transition-speed) ease;
}

.sidebar-overlay.active {
  opacity: 1;
  visibility: visible;
}

/* --- SIDEBAR BASE --- */
.sidebar {
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border-color);
  width: var(--sidebar-width);
  height: 100vh;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
  /* Animates sliding off the screen entirely */
  transform: translateX(0);
  transition: transform var(--transition-speed) cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 10px 0 30px rgba(0, 0, 0, 0.05);
}

/* The Vanish Class - Completely hides sidebar on both desktop and mobile */
.sidebar.vanished {
  transform: translateX(-100%);
  box-shadow: none;
}

/* --- HEADER --- */
.sidebar-header {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  flex-shrink: 0;
}

.brand-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, var(--brand-primary), #10b981);
  color: white;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 800;
}

.sidebar-header h3 {
  font-size: 18px;
  color: var(--text-main);
  margin: 0;
  font-weight: 800;
  letter-spacing: -0.5px;
}

.sidebar-header h3 span {
  font-weight: 400;
  color: var(--text-muted);
}

.close-btn {
  background: transparent;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.2s;
}

.close-btn:hover {
  background: #fee2e2;
  color: #ef4444;
}

/* --- USER PROFILE SECTION --- */
.user-profile-section {
  padding: 0 24px 20px 24px;
  border-bottom: 1px solid var(--border-color);
}

.user-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--hover-bg);
  padding: 12px;
  border-radius: 16px;
  border: 1px solid var(--border-color);
}

.user-avatar {
  width: 40px;
  height: 40px;
  background: #e2e8f0;
  color: var(--text-main);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 700;
  flex-shrink: 0;
}

.user-details {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.user-name {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role {
  font-size: 12px;
  color: var(--brand-primary);
  font-weight: 600;
}

/* --- SCROLL AREA --- */
.sidebar-scroll-area {
  flex-grow: 1;
  overflow-y: auto;
  padding: 24px;
}

.sidebar-scroll-area::-webkit-scrollbar { width: 4px; }
.sidebar-scroll-area::-webkit-scrollbar-track { background: transparent; }
.sidebar-scroll-area::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 10px; }

/* --- NAVIGATION --- */
.nav-section-title {
  font-size: 11px;
  font-weight: 700;
  color: #94a3b8;
  letter-spacing: 1.2px;
  margin: 24px 0 12px 0;
}

.nav-section-title:first-child {
  margin-top: 0;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 14px;
  border-radius: 12px;
  text-decoration: none;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
  margin-bottom: 6px;
  position: relative;
}

.icon-wrapper {
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: transform 0.2s;
}

.icon-wrapper img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  opacity: 0.6;
  transition: opacity 0.2s;
}

.nav-link:hover {
  background: var(--hover-bg);
  color: var(--text-main);
}

.nav-link:hover .icon-wrapper {
  transform: scale(1.1);
}

.nav-link:hover .icon-wrapper img {
  opacity: 1;
}

/* Active State */
.nav-link.active {
  background: var(--brand-primary-light);
  color: var(--brand-primary);
  font-weight: 600;
}

.nav-link.active .icon-wrapper img {
  opacity: 1;
}

/* --- FOOTER --- */
.sidebar-footer {
  padding: 24px;
  border-top: 1px solid var(--border-color);
  background: #ffffff;
}

.btn-primary {
  width: 100%;
  background: var(--text-main);
  color: #fff;
  padding: 14px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background 0.2s, transform 0.1s;
}

.btn-primary:hover {
  background: var(--brand-primary);
}

.btn-primary:active {
  transform: scale(0.98);
}

/* --- RESPONSIVE MEDIA QUERIES --- */
@media (max-width: 1024px) {
  .sidebar {
    transform: translateX(-100%); /* Hidden by default on mobile */
  }
  
  .sidebar.mobile-expanded {
    transform: translateX(0); /* Shown when toggled */
  }

  .floating-toggle-btn {
    opacity: 1;
    visibility: visible;
    transform: scale(1);
  }
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function () {
  const sidebar = document.getElementById('sidebar');
  const closeBtn = document.getElementById('sidebarToggle');
  const floatingBtn = document.getElementById('floatingToggle');
  const overlay = document.getElementById('sidebarOverlay');

  // Function to entirely hide the sidebar and show floating button
  function hideSidebar() {
    if (window.innerWidth <= 1024) {
      sidebar.classList.remove('mobile-expanded');
      overlay.classList.remove('active');
      document.body.style.overflow = '';
    } else {
      sidebar.classList.add('vanished');
    }
    floatingBtn.classList.add('visible');
  }

  // Function to show the sidebar and hide floating button
  function showSidebar() {
    if (window.innerWidth <= 1024) {
      sidebar.classList.add('mobile-expanded');
      overlay.classList.add('active');
      document.body.style.overflow = 'hidden'; // prevent background scrolling
    } else {
      sidebar.classList.remove('vanished');
    }
    floatingBtn.classList.remove('visible');
  }

  // Bind Events
  if (closeBtn) closeBtn.addEventListener('click', hideSidebar);
  if (floatingBtn) floatingBtn.addEventListener('click', showSidebar);
  if (overlay) overlay.addEventListener('click', hideSidebar);

  // Initialize visibility logic on load
  if (window.innerWidth <= 1024) {
    floatingBtn.classList.add('visible'); // Mobile defaults to hidden sidebar
  }

  // Handle window resizing safely
  window.addEventListener('resize', () => {
    if (window.innerWidth > 1024) {
      // Desktop state: if it wasn't explicitly vanished, show it
      if (!sidebar.classList.contains('vanished')) {
        floatingBtn.classList.remove('visible');
      }
      sidebar.classList.remove('mobile-expanded');
      overlay.classList.remove('active');
      document.body.style.overflow = '';
    } else {
      // Mobile state
      sidebar.classList.remove('vanished');
      if (!sidebar.classList.contains('mobile-expanded')) {
        floatingBtn.classList.add('visible');
      }
    }
  });
});
</script>