<?php $u = current_user(); $role = $u['role']; $cur = basename($_SERVER['PHP_SELF']); $curDir = basename(dirname($_SERVER['PHP_SELF']));
function navlink($href, $label, $icon, $active) {
    $base = "flex items-center gap-3 px-4 py-2.5 rounded-lg text-sm font-medium transition";
    $activeCls = $active ? "bg-brand-600 text-white" : "text-slate-200 hover:bg-slate-700/60";
    echo "<a href=\"$href\" class=\"$base $activeCls\" onclick=\"closeSidebar()\"><span class=\"w-2\">$icon</span><span>$label</span></a>";
}
$unread = unread_notification_count($pdo, $u['id']);
?>
<?php include __DIR__ . '/notifications_ui.php'; ?>
<!-- Mobile top bar (hidden on md+ where the sidebar is always visible) -->
<div class="md:hidden flex items-center justify-between bg-slate-900 text-white px-4 py-3 sticky top-0 z-30">
  <button onclick="toggleSidebar()" aria-label="Open menu" class="text-2xl leading-none px-1">☰</button>
  <div class="text-center">
    <p class="font-bold text-sm leading-tight">Best Western Plus</p>
    <p class="text-[10px] text-slate-400 leading-tight">Inventory Management</p>
  </div>
  <a href="<?= BASE_URL ?>notifications.php" class="notif-bell relative text-xl leading-none px-1">
    🔔
    <span class="notif-badge absolute -top-1 -right-2 bg-red-500 text-white text-[10px] rounded-full px-1.5 leading-tight <?= $unread > 0 ? '' : 'hidden' ?>"><?= $unread ?></span>
  </a>
</div>

<!-- Backdrop shown behind the sidebar drawer on mobile -->
<div id="sidebarBackdrop" onclick="closeSidebar()" class="hidden md:hidden fixed inset-0 bg-black/40 z-30"></div>

<aside id="sidebarNav" class="fixed inset-y-0 top-0 left-0 w-64 shrink-0 bg-slate-900 text-white flex flex-col -translate-x-full md:translate-x-0 transition-transform duration-200 z-40 h-screen">
  <div class="px-5 py-5 border-b border-slate-700 flex items-center justify-between">
    <div>
      <p class="font-bold leading-tight">Best Western Plus</p>
      <p class="text-xs text-slate-400">Inventory Management</p>
    </div>
    <button onclick="closeSidebar()" class="md:hidden text-slate-400 hover:text-white text-xl leading-none px-1">✕</button>
  </div>
  <nav class="flex-1 overflow-y-auto px-3 py-4 space-y-1">
    <?php navlink(BASE_URL.'dashboard.php','Dashboard','📊',$cur=='dashboard.php'); ?>
    <a href="<?= BASE_URL ?>notifications.php" onclick="closeSidebar()" class="notif-bell flex items-center justify-between px-4 py-2.5 rounded-lg text-sm font-medium transition <?= $cur=='notifications.php' ? 'bg-brand-600 text-white' : 'text-slate-200 hover:bg-slate-700/60' ?>">
      <span class="flex items-center gap-3"><span class="w-2">🔔</span><span>Notifications</span></span>
      <span class="notif-badge bg-red-500 text-white text-xs rounded-full px-2 py-0.5 <?= $unread > 0 ? '' : 'hidden' ?>"><?= $unread ?></span>
    </a>

    <p class="px-4 pt-4 pb-1 text-xs uppercase tracking-wider text-slate-500">Inventory</p>
    <?php if (has_permission($pdo, $u, 'main_store.view')): ?><?php navlink(BASE_URL.'main_store.php','Main Store','🏬',$cur=='main_store.php'); ?><?php endif; ?>
    <?php if (has_permission($pdo, $u, 'outlets.manage')): ?><?php navlink(BASE_URL.'outlets.php','Outlets','🏪',$cur=='outlets.php'); ?><?php endif; ?>
    <?php if (has_permission($pdo, $u, 'items.manage')): ?><?php navlink(BASE_URL.'items.php','Items','📦',$cur=='items.php'); ?><?php endif; ?>
    <?php if (has_permission($pdo, $u, 'categories.manage')): ?><?php navlink(BASE_URL.'categories.php','Categories & Units','🏷️',$cur=='categories.php'); ?><?php endif; ?>
    <?php if (has_permission($pdo, $u, 'suppliers.manage')): ?><?php navlink(BASE_URL.'suppliers.php','Suppliers','🚚',$cur=='suppliers.php'); ?><?php endif; ?>

    <p class="px-4 pt-4 pb-1 text-xs uppercase tracking-wider text-slate-500">Requisitions</p>
    <?php navlink(BASE_URL.'purchase_requisitions/list.php','Purchase Requisitions','🧾',$curDir=='purchase_requisitions'); ?>
    <?php if (has_permission($pdo, $u, 'purchase_orders.manage')): ?><?php navlink(BASE_URL.'purchase_orders/list.php','Purchase Orders','📑',$curDir=='purchase_orders'); ?><?php endif; ?>
    <?php if (has_permission($pdo, $u, 'goods_received.view')): ?><?php navlink(BASE_URL.'goods_received/list.php','Goods Received','📥',$curDir=='goods_received'); ?><?php endif; ?>
    <?php if (has_permission($pdo, $u, 'return_to_supplier.manage')): ?><?php navlink(BASE_URL.'return_to_supplier/list.php','Return to Supplier','↩️',$curDir=='return_to_supplier'); ?><?php endif; ?>
    <?php navlink(BASE_URL.'store_requisitions/list.php','Store Requisitions','📋',$curDir=='store_requisitions'); ?>

    <?php if (has_permission($pdo, $u, 'waste.manage')): ?>
    <p class="px-4 pt-4 pb-1 text-xs uppercase tracking-wider text-slate-500">Waste & Spoilage</p>
    <?php navlink(BASE_URL.'waste/list.php','Waste Records','🗑️',$curDir=='waste'); ?>
    <?php endif; ?>

    <?php if (has_permission($pdo, $u, 'reports.view')): ?>
    <p class="px-4 pt-4 pb-1 text-xs uppercase tracking-wider text-slate-500">Reports</p>
    <?php navlink(BASE_URL.'reports/stock_report.php','Stock Report','📈',$curDir=='reports' && $cur=='stock_report.php'); ?>
    <?php navlink(BASE_URL.'reports/low_stock.php','Low Stock Alerts','⚠️',$curDir=='reports' && $cur=='low_stock.php'); ?>
    <?php navlink(BASE_URL.'reports/expiring_items.php','Expiring Items','⏳',$curDir=='reports' && $cur=='expiring_items.php'); ?>
    <?php navlink(BASE_URL.'reports/waste_report.php','Waste Report','🗑️',$curDir=='reports' && $cur=='waste_report.php'); ?>
    <?php endif; ?>

    <?php if (has_permission($pdo, $u, 'accounting.reports_view') || has_permission($pdo, $u, 'accounting.coa_manage') || has_permission($pdo, $u, 'accounting.ap_manage') || has_permission($pdo, $u, 'accounting.ar_manage')): ?>
    <p class="px-4 pt-4 pb-1 text-xs uppercase tracking-wider text-slate-500">Accounting</p>
    <?php if (has_permission($pdo, $u, 'accounting.coa_manage')): ?><?php navlink(BASE_URL.'chart_of_accounts.php','Chart of Accounts','📚',$cur=='chart_of_accounts.php'); ?><?php endif; ?>
    <?php if (has_permission($pdo, $u, 'accounting.reports_view')): ?>
      <?php navlink(BASE_URL.'journal_entries/list.php','Journal Entries','📓',$curDir=='journal_entries'); ?>
      <?php navlink(BASE_URL.'reports/general_ledger.php','General Ledger','📖',$curDir=='reports' && $cur=='general_ledger.php'); ?>
      <?php navlink(BASE_URL.'reports/trial_balance.php','Trial Balance','⚖️',$curDir=='reports' && $cur=='trial_balance.php'); ?>
      <?php navlink(BASE_URL.'reports/income_statement.php','Income Statement (P&L)','💹',$curDir=='reports' && $cur=='income_statement.php'); ?>
      <?php navlink(BASE_URL.'reports/balance_sheet.php','Balance Sheet','🧾',$curDir=='reports' && $cur=='balance_sheet.php'); ?>
    <?php endif; ?>
    <?php if (has_permission($pdo, $u, 'accounting.ap_manage')): ?><?php navlink(BASE_URL.'accounts_payable/list.php','Accounts Payable','💳',$curDir=='accounts_payable'); ?><?php endif; ?>
    <?php if (has_permission($pdo, $u, 'accounting.ar_manage')): ?><?php navlink(BASE_URL.'accounts_receivable/list.php','Accounts Receivable','💰',$curDir=='accounts_receivable'); ?><?php endif; ?>
    <?php endif; ?>

    <?php if (is_admin_role($role)): ?>
    <p class="px-4 pt-4 pb-1 text-xs uppercase tracking-wider text-slate-500">Administration</p>
    <?php navlink(BASE_URL.'users.php','Users','👤',$cur=='users.php'); ?>
    <?php navlink(BASE_URL.'permissions.php','Permissions','🔐',$cur=='permissions.php'); ?>
    <?php navlink(BASE_URL.'role_permissions.php','Role Permissions','🧩',$cur=='role_permissions.php'); ?>
    <?php navlink(BASE_URL.'departments.php','Departments','🏢',$cur=='departments.php'); ?>
    <?php navlink(BASE_URL.'waste_reasons.php','Waste Reasons','📝',$cur=='waste_reasons.php'); ?>
    <?php navlink(BASE_URL.'approval_workflow.php','Approval Workflow','✅',$cur=='approval_workflow.php'); ?>
    <?php navlink(BASE_URL.'mail_test.php','Test Email Setup','✉️',$cur=='mail_test.php'); ?>
    <?php if ($role === 'superadmin'): ?>
      <?php navlink(BASE_URL.'system_reset.php','Reset Test Data','🧨',$cur=='system_reset.php'); ?>
    <?php endif; ?>
    <?php endif; ?>
  </nav>
  <div class="px-4 py-4 border-t border-slate-700 text-sm">
    <p class="font-medium"><?= e($u['username']) ?></p>
    <p class="text-xs text-slate-400 capitalize mb-2"><?= e($role) ?></p>
    <a href="<?= BASE_URL ?>profile.php" class="text-brand-100 hover:underline text-xs">Profile</a>
    ·
    <a href="<?= BASE_URL ?>change_password.php" class="text-brand-100 hover:underline text-xs">Password</a>
    ·
    <a href="<?= BASE_URL ?>logout.php" class="text-red-300 hover:underline text-xs">Logout</a>
  </div>
</aside>
<script>
function toggleSidebar() {
  document.getElementById('sidebarNav').classList.toggle('-translate-x-full');
  document.getElementById('sidebarBackdrop').classList.toggle('hidden');
}
function closeSidebar() {
  if (window.innerWidth < 768) {
    document.getElementById('sidebarNav').classList.add('-translate-x-full');
    document.getElementById('sidebarBackdrop').classList.add('hidden');
  }
}
</script>
<main class="w-full md:pl-64 min-w-0">
  <div id="mainContent" class="max-w-7xl mx-auto px-4 md:px-6 py-6">
  <?php foreach (get_flashes() as $f): ?>
    <script>showToast(<?= json_encode($f['msg']) ?>, <?= json_encode($f['type']==='error' ? 'error' : 'success') ?>);</script>
  <?php endforeach; ?>
