{% if navigation_render_location_v2|default('page') == 'global-host' or not global_role_navigation_host_v2|default(false) %}
{#
  Partner operations navigation.
  Presentation only: all links use existing production routes and route-level permissions remain authoritative.
  Existing Partner dashboard tabs are retained and organised under their operational modules.
#}
{% 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') and _partner_tab in ['overview', 'branch-work'],
    'children': [
      {
        'label': 'Overview Matrix',
        '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': 'Review Board',
    'visible': true,
    'active': _partner_path.startswith('/partner/reviews') or (_partner_path.startswith('/partner/dashboard') and _partner_tab in ['review', 'escalations']),
    'children': [
      {
        'label': 'Review Overview',
        'url': '/partner/dashboard?tab=review',
        'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'review'
      },
      {
        'label': 'Reviews',
        'url': '/partner/reviews',
        'active': _partner_path.startswith('/partner/reviews')
      },
      {
        'label': 'Escalations & Ageing',
        'url': '/partner/dashboard?tab=escalations',
        'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'escalations'
      }
    ]
  },
  {
    'label': 'Clients',
    'visible': true,
    'active': (_partner_path.startswith('/partner/dashboard') and _partner_tab == 'clients') or _partner_path.startswith('/partner/clients') or _partner_path.startswith('/clients') or _partner_path.startswith('/registrations') or _partner_path.startswith('/documents/permanent'),
    'children': [
      {
        'label': 'Client Overview',
        'url': '/partner/dashboard?tab=clients',
        'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'clients'
      },
      {
        'label': 'Clients List',
        'url': '/partner/clients',
        'active': _partner_path.startswith('/partner/clients')
      },
      {
        'label': 'Add Client',
        'url': '/clients/new',
        'active': _partner_path == '/clients/new'
      },
      {
        'label': 'Registrations & Renewals',
        'url': '/registrations',
        'active': _partner_path == '/registrations' or (_partner_path.startswith('/registrations/') and not _partner_path.startswith('/registrations/masters') and not _partner_path.startswith('/registrations/bulk'))
      },
      {
        'label': 'Bulk Registration Addition',
        'url': '/registrations/bulk',
        'active': _partner_path.startswith('/registrations/bulk')
      },
      {
        'label': 'Registration Masters',
        'url': '/registrations/masters',
        'active': _partner_path.startswith('/registrations/masters')
      },
      {
        'label': 'Permanent Docs',
        'url': '/documents/permanent',
        'active': _partner_path.startswith('/documents/permanent')
      }
    ]
  },
  {
    'label': 'Engagements',
    'visible': true,
    'active': _partner_path.startswith('/services/subscriptions') or _partner_path.startswith('/services/engagements') or _partner_path.startswith('/services/bulk-imports'),
    'children': [
      {
        'label': 'Client Subscriptions',
        'url': '/services/subscriptions',
        'active': _partner_path == '/services/subscriptions' or (_partner_path.startswith('/services/subscriptions/') and not _partner_path.startswith('/services/subscriptions/bulk'))
      },
      {
        'label': 'Bulk Subscriptions',
        'url': '/services/subscriptions/bulk',
        'active': _partner_path.startswith('/services/subscriptions/bulk')
      },
      {
        'label': 'All Engagements',
        'url': '/services/engagements',
        'active': _partner_path == '/services/engagements' or (_partner_path.startswith('/services/engagements/') and _partner_path != '/services/engagements/new' and _partner_path != '/services/engagements/bulk-new')
      },
      {
        'label': 'Assign Services',
        'url': '/services/engagements/new',
        'active': _partner_path == '/services/engagements/new'
      },
      {
        'label': 'Bulk Setup',
        'url': '/services/engagements/bulk-new',
        'active': _partner_path == '/services/engagements/bulk-new'
      },
      {
        'label': 'Bulk Excel',
        'url': '/services/bulk-imports',
        'active': _partner_path.startswith('/services/bulk-imports')
      }
    ]
  },
  {
    'label': 'Staff',
    'visible': true,
    'active': (_partner_path.startswith('/partner/dashboard') and _partner_tab == 'staff') or _partner_path.startswith('/employees'),
    'children': [
      {
        'label': 'Workload',
        'url': '/partner/dashboard?tab=staff',
        'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'staff'
      },
      {
        'label': 'Staff List',
        'url': '/employees',
        'active': _partner_path == '/employees' or (_partner_path.startswith('/employees/') and _partner_path != '/employees/new')
      },
      {
        'label': 'Add Staff',
        'url': '/employees/new',
        'active': _partner_path == '/employees/new'
      }
    ]
  },
  {
    'label': 'Billing',
    'visible': true,
    'active': (_partner_path.startswith('/partner/dashboard') and _partner_tab == 'billing') or _partner_path.startswith('/billing'),
    'children': [
      {
        'label': 'Billing Overview',
        'url': '/partner/dashboard?tab=billing',
        'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'billing'
      },
      {
        'label': 'New Invoice',
        'url': '/billing/new',
        'active': _partner_path == '/billing/new' or _partner_path == '/billing/invoices/new'
      },
      {
        'label': 'Generate Bills',
        'url': '/billing/generate',
        'active': _partner_path == '/billing/generate'
      },
      {
        'label': 'Fee Structure',
        'url': '/billing/fee-structures/list',
        'active': _partner_path == '/billing/fee-structures/list'
      },
      {
        'label': 'Fee Templates',
        'url': '/billing/fee-structures/template',
        'active': false
      },
      {
        'label': 'Bulk Fee Import',
        'url': '/billing/fee-structures/import',
        'active': _partner_path.startswith('/billing/fee-structures/import')
      },
      {
        'label': 'Payments',
        'url': '/billing/payments',
        'active': _partner_path.startswith('/billing/payments') or (_partner_path.startswith('/billing/') and _partner_path.endswith('/payments/new'))
      },
      {
        'label': 'Bill Settings',
        'url': '/billing/settings',
        'active': _partner_path.startswith('/billing/settings')
      }
    ]
  },
  {
    'label': 'Reports',
    'visible': true,
    'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'reports',
    'children': [
      {
        'label': 'Reports Centre',
        'url': '/partner/dashboard?tab=reports',
        'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'reports'
      }
    ]
  },
  {
    'label': 'Tools',
    'visible': true,
    'active': (_partner_path.startswith('/partner/dashboard') and _partner_tab == 'wizards') or _partner_path.startswith('/tools/bank-statement-analyzer') or _partner_path.startswith('/documents/storage-nodes') or _partner_path.startswith('/documents/storage-jobs'),
    'children': [
      {
        'label': 'Bank Analyzer',
        'url': '/tools/bank-statement-analyzer',
        'active': _partner_path.startswith('/tools/bank-statement-analyzer')
      },
      {
        'label': 'Storage Nodes',
        'url': '/documents/storage-nodes',
        'active': _partner_path.startswith('/documents/storage-nodes')
      },
      {
        'label': 'Storage Jobs',
        'url': '/documents/storage-jobs',
        'active': _partner_path.startswith('/documents/storage-jobs')
      },
      {
        'label': 'Setup Wizards',
        'url': '/partner/dashboard?tab=wizards',
        'active': _partner_path.startswith('/partner/dashboard') and _partner_tab == 'wizards'
      }
    ]
  },
  {
    '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 %}
