<?php
$items = [
  ['key' => 'dashboard', 'label' => 'Dashboard', 'icon' => 'bi-house-door', 'url' => 'dashboard', 'module' => 'dashboard'],
  ['key' => 'leads', 'label' => noun('leads', 'Leads'), 'icon' => 'bi-person-plus', 'url' => 'leads', 'module' => 'leads'],
  ['key' => 'accounts', 'label' => account_label(), 'icon' => 'bi-building', 'url' => 'accounts', 'module' => 'accounts'],
  ['key' => 'contacts', 'label' => noun('contacts', 'Contacts'), 'icon' => 'bi-people', 'url' => 'contacts', 'module' => 'contacts'],
  ['key' => 'opportunities', 'label' => opportunity_label(), 'icon' => 'bi-currency-dollar', 'url' => 'opportunities', 'module' => 'opportunities'],
  ['key' => 'tasks', 'label' => 'Tasks', 'icon' => 'bi-check2-square', 'url' => 'tasks', 'module' => 'tasks'],
  ['key' => 'calendar', 'label' => 'Calendar', 'icon' => 'bi-calendar3', 'url' => 'calendar', 'module' => 'calendar'],
  ['key' => 'emails', 'label' => 'Email', 'icon' => 'bi-envelope', 'url' => 'emails', 'module' => 'emails'],
  ['key' => 'campaigns', 'label' => 'Campaigns', 'icon' => 'bi-megaphone', 'url' => 'campaigns', 'module' => 'campaigns', 'group' => 'Marketing'],
  ['key' => 'lists', 'label' => 'Lists / Groups', 'icon' => 'bi-collection', 'url' => 'lists', 'module' => 'lists', 'group' => 'Marketing'],
  ['key' => 'templates', 'label' => 'Templates', 'icon' => 'bi-file-earmark-text', 'url' => 'templates', 'module' => 'templates', 'group' => 'Marketing'],
  ['key' => 'unsubscribes', 'label' => 'Unsubscribes', 'icon' => 'bi-slash-circle', 'url' => 'unsubscribes', 'module' => 'unsubscribes', 'group' => 'Marketing'],
  ['key' => 'campaign_reports', 'label' => 'Campaign reports', 'icon' => 'bi-graph-up', 'url' => 'campaigns/reports', 'module' => 'campaign_reports', 'group' => 'Marketing'],
  ['key' => 'service', 'label' => 'Service visits', 'icon' => 'bi-tools', 'url' => 'service', 'module' => 'service_visits'],
  ['key' => 'jobs', 'label' => 'Service jobs', 'icon' => 'bi-clipboard2-check', 'url' => 'jobs', 'module' => 'service_jobs'],
  ['key' => 'equipment', 'label' => 'Equipment', 'icon' => 'bi-cpu', 'url' => 'equipment', 'module' => 'equipment'],
  ['key' => 'quotes', 'label' => 'Quotes', 'icon' => 'bi-file-earmark-richtext', 'url' => 'quotes', 'module' => 'quotes', 'group' => 'More'],
  ['key' => 'documents', 'label' => 'Documents', 'icon' => 'bi-file-earmark-pdf', 'url' => 'documents', 'module' => 'documents', 'group' => 'More'],
  ['key' => 'orders', 'label' => 'Orders', 'icon' => 'bi-bag-check', 'url' => 'orders', 'module' => 'orders', 'group' => 'More'],
  ['key' => 'contracts', 'label' => 'Contracts', 'icon' => 'bi-file-earmark-ruled', 'url' => 'contracts', 'module' => 'contracts', 'group' => 'More'],
  ['key' => 'invoices', 'label' => 'Invoices', 'icon' => 'bi-receipt', 'url' => 'invoices', 'module' => 'invoices', 'group' => 'More'],
  ['key' => 'renewals', 'label' => 'Renewals', 'icon' => 'bi-arrow-repeat', 'url' => 'renewals', 'module' => 'renewals', 'group' => 'More'],
  ['key' => 'approvals', 'label' => 'Approvals', 'icon' => 'bi-patch-check', 'url' => 'approvals', 'module' => 'approvals'],
  ['key' => 'reports', 'label' => 'Reports', 'icon' => 'bi-bar-chart', 'url' => 'reports', 'module' => 'reports'],
  ['key' => 'assignment', 'label' => 'Lead assignment', 'icon' => 'bi-diagram-3', 'url' => 'assignment', 'module' => 'assignment'],
  ['key' => 'users', 'label' => 'Users', 'icon' => 'bi-person-gear', 'url' => 'users', 'module' => 'users'],
  ['key' => 'roles', 'label' => 'Roles', 'icon' => 'bi-shield-check', 'url' => 'roles', 'module' => 'roles'],
  ['key' => 'setup', 'label' => 'Setup', 'icon' => 'bi-sliders', 'url' => 'setup', 'module' => 'setup'],
];
$path = trim(parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?: '', '/');
$shownGroup = '';
?>
<aside class="sidebar">
  <a class="brand" href="<?= e(url('dashboard')) ?>">
    <img class="brand-logo" src="<?= e(asset('img/aosware-crm-logo.jpg')) ?>" alt="AOSWARE CRM">
  </a>
  <nav>
    <?php foreach ($items as $item):
      if (!can_see($item['module'])) continue;
      $group = $item['group'] ?? '';
      if ($group !== '' && $group !== $shownGroup):
        $shownGroup = $group; ?>
        <div class="nav-group"><?= e($group) ?></div>
      <?php endif;
      $active = str_contains($path, $item['url']);
      if ($item['url'] === 'dashboard') {
        $active = str_ends_with($path, 'Staff-Portal') || str_ends_with($path, 'dashboard') || str_ends_with($path, 'Staff-Portal/');
      }
      if ($item['url'] === 'campaigns') {
        $active = str_contains($path, '/campaigns') && !str_contains($path, '/campaigns/reports');
      }
    ?>
      <a class="nav-link <?= $active ? 'active' : '' ?>" href="<?= e(url($item['url'])) ?>">
        <i class="bi <?= e($item['icon']) ?>"></i> <?= e($item['label']) ?>
      </a>
    <?php endforeach; ?>
  </nav>
  <div class="sidebar-foot">
    <a class="nav-link" href="<?= e(url('settings')) ?>"><i class="bi bi-gear"></i> Settings</a>
    <a class="nav-link" href="<?= e(url('logout')) ?>"><i class="bi bi-box-arrow-left"></i> Sign out</a>
  </div>
</aside>
