{% if navigation_render_location_v2|default('page') == 'global-host' or not global_role_navigation_host_v2|default(false) %}
{#
  UI/UX V2 Phase 1D.6 — Unified Manager navigation.
  Presentation only. Existing routes and route-level permissions remain authoritative.
#}
{% set _manager_path = request.url.path %}
{% set _manager_tab = request.query_params.get('tab', 'overview') %}
{% set navigation_aria_label_v2 = 'Manager workspace navigation' %}
{% set navigation_items_v2 = [
  {
    'label': 'Dashboard',
    'visible': true,
    'active': _manager_path.startswith('/manager/dashboard'),
    'children': [
      {'label': 'Overview', 'url': '/manager/dashboard?tab=overview', 'active': _manager_path.startswith('/manager/dashboard') and _manager_tab == 'overview'},
      {'label': 'Team Work', 'url': '/manager/dashboard?tab=team-work', 'active': _manager_path.startswith('/manager/dashboard') and _manager_tab == 'team-work'},
      {'label': 'Due Calendar', 'url': '/manager/dashboard?tab=due-calendar', 'active': _manager_path.startswith('/manager/dashboard') and _manager_tab == 'due-calendar'},
      {'label': 'Review Queue', 'url': '/manager/dashboard?tab=review-queue', 'active': _manager_path.startswith('/manager/dashboard') and _manager_tab == 'review-queue'},
      {'label': 'Client Pending', 'url': '/manager/dashboard?tab=client-pending', 'active': _manager_path.startswith('/manager/dashboard') and _manager_tab == 'client-pending'},
      {'label': 'Documents', 'url': '/manager/dashboard?tab=documents', 'active': _manager_path.startswith('/manager/dashboard') and _manager_tab == 'documents'},
      {'label': 'Escalations', 'url': '/manager/dashboard?tab=escalations', 'active': _manager_path.startswith('/manager/dashboard') and _manager_tab == 'escalations'},
      {'label': 'Reports', 'url': '/manager/dashboard?tab=reports', 'active': _manager_path.startswith('/manager/dashboard') and _manager_tab == 'reports'},
      {'label': 'Wizards', 'url': '/manager/dashboard?tab=wizards', 'active': _manager_path.startswith('/manager/dashboard') and _manager_tab == 'wizards'}
    ]
  },
  {
    'label': 'Team Work Board',
    'url': '/manager/work',
    'visible': true,
    'active': _manager_path.startswith('/manager/work')
  },
  {
    'label': 'Detailed Allocation',
    'url': '/employees/work',
    'visible': true,
    'active': _manager_path.startswith('/employees/work')
  },
  {
    'label': 'Engagement Progress',
    'url': '/employees/progress',
    'visible': true,
    'active': _manager_path.startswith('/employees/progress')
  },
  {
    'label': 'Team Attendance',
    'url': '/employees/attendance',
    'visible': true,
    'active': _manager_path.startswith('/employees/attendance')
  },
  {
    'label': 'Team Leave',
    'url': '/employees/leave',
    'visible': true,
    'active': _manager_path.startswith('/employees/leave')
  },
  {
    'label': 'Bank Analyzer',
    'url': '/tools/bank-statement-analyzer',
    'visible': true,
    'active': _manager_path.startswith('/tools/bank-statement-analyzer')
  },
  {
    'label': 'Alerts',
    'url': '/alerts',
    'visible': true,
    'active': _manager_path.startswith('/alerts'),
    'badge': unread_alert_count if unread_alert_count is defined and unread_alert_count > 0 else none
  }
] %}

<div class="border-b border-slate-200 bg-white shadow-sm" data-uiux-v2-pilot="manager">
  {% include "ui/templates/components/navigation_v2.html" %}
  {% include "ui/templates/components/mobile_navigation_v2.html" %}
</div>

<script>
(function () {
  "use strict";

  function initialiseManagerDropdowns() {
    var root = document.querySelector('[data-uiux-v2-pilot="manager"]');
    if (!root || root.dataset.dropdownAutoCloseInitialised === 'true') return;

    var desktopNav = root.querySelector('nav[data-uiux-v2="desktop-navigation"]');
    if (!desktopNav) return;

    root.dataset.dropdownAutoCloseInitialised = 'true';
    var dropdowns = Array.from(desktopNav.querySelectorAll('details'));

    function closeAllExcept(exception) {
      dropdowns.forEach(function (details) {
        if (details !== exception) details.removeAttribute('open');
      });
    }

    dropdowns.forEach(function (details) {
      details.addEventListener('toggle', function () {
        if (details.open) closeAllExcept(details);
      });

      details.querySelectorAll('a[href]').forEach(function (link) {
        link.addEventListener('click', function () {
          details.removeAttribute('open');
        });
      });
    });

    document.addEventListener('click', function (event) {
      if (!root.contains(event.target)) closeAllExcept(null);
    });

    document.addEventListener('keydown', function (event) {
      if (event.key === 'Escape') closeAllExcept(null);
    });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initialiseManagerDropdowns, { once: true });
  } else {
    initialiseManagerDropdowns();
  }
})();
</script>
{% endif %}
