{% if navigation_render_location_v2|default('page') == 'global-host' or not global_role_navigation_host_v2|default(false) %}
{#
  UI/UX V2 Phase 1D.7 — Partner client and engagement creation entry points.
  Presentation only. Existing routes and route-level permissions remain authoritative.
#}
{% set _partner_path = request.url.path %}
{% set _partner_tab = request.query_params.get('tab', 'overview') %}
{% set navigation_aria_label_v2 = 'Partner workspace navigation' %}
{% set navigation_items_v2 = [
  {
    'label': 'Dashboard',
    'visible': true,
    'active': _partner_path.startswith('/partner/dashboard'),
    'children': [
      {'label': 'Overview', 'url': '/partner/dashboard?tab=overview', 'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'overview'},
      {'label': 'Branch Work', 'url': '/partner/dashboard?tab=branch-work', 'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'branch-work'},
      {'label': 'Clients', 'url': '/partner/dashboard?tab=clients', 'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'clients'},
      {'label': 'Staff', 'url': '/partner/dashboard?tab=staff', 'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'staff'},
      {'label': 'Review', 'url': '/partner/dashboard?tab=review', 'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'review'},
      {'label': 'Billing', 'url': '/partner/dashboard?tab=billing', 'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'billing'},
      {'label': 'Reports', 'url': '/partner/dashboard?tab=reports', 'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'reports'},
      {'label': 'Wizards', 'url': '/partner/dashboard?tab=wizards', 'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'wizards'}
    ]
  },
  {
    'label': 'Review Board',
    'url': '/partner/reviews',
    'visible': true,
    'active': _partner_path.startswith('/partner/reviews')
  },
  {
    'label': 'My Clients',
    'visible': true,
    'active': _partner_path.startswith('/partner/clients') or _partner_path.startswith('/clients'),
    'children': [
      {'label': 'Client Portfolio', 'url': '/partner/clients', 'active': _partner_path.startswith('/partner/clients')},
      {'label': 'Add Client', 'url': '/clients/new', 'active': _partner_path == '/clients/new'}
    ]
  },
  {
    'label': 'Engagements',
    'visible': true,
    'active': _partner_path.startswith('/services/engagements'),
    'children': [
      {'label': 'All Engagements', 'url': '/services/engagements', 'active': _partner_path == '/services/engagements'},
      {'label': 'Assign Service', 'url': '/services/engagements/new', 'active': _partner_path == '/services/engagements/new'}
    ]
  },
  {
    'label': 'Documents',
    'url': '/documents',
    'visible': true,
    'active': _partner_path.startswith('/documents')
  },
  {
    'label': 'Billing',
    'url': '/billing',
    'visible': true,
    'active': _partner_path.startswith('/billing')
  },
  {
    'label': 'Bank Analyzer',
    'url': '/tools/bank-statement-analyzer',
    'visible': true,
    'active': _partner_path.startswith('/tools/bank-statement-analyzer')
  },
  {
    'label': 'Alerts',
    'url': '/alerts',
    'visible': true,
    'active': _partner_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="partner">
  {% include "ui/templates/components/navigation_v2.html" %}
  {% include "ui/templates/components/mobile_navigation_v2.html" %}
</div>

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

  function initialisePartnerDropdowns() {
    var root = document.querySelector('[data-uiux-v2-pilot="partner"]');
    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', initialisePartnerDropdowns, { once: true });
  } else {
    initialisePartnerDropdowns();
  }
})();
</script>
{% endif %}
