{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <section class="rounded-3xl border border-slate-200 bg-white p-6 shadow-soft">
    <div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
      <div>
        <p class="text-xs font-semibold uppercase tracking-[0.24em] text-brand-600">Branch Operations Control Centre</p>
        <h1 class="mt-2 text-2xl font-bold text-slate-900">Partner Operations Dashboard</h1>
        <p class="mt-1 text-sm text-slate-500">
          Monitor branch clients, due work, overdue tasks, review queue, team workload, billing and operational reports.
        </p>
        <p class="mt-2 text-xs text-slate-500">
          Scope: {{ overview.tenant.name if overview.tenant else 'Tenant' }}{% if overview.branch %} · {{ overview.branch.name }}{% else %} · All permitted branches{% endif %}{% if overview.financial_year %} · FY {{ overview.financial_year }}{% endif %}
        </p>
      </div>
      <div class="flex flex-wrap gap-2">
        <a href="/partner/reviews" class="rounded-2xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white shadow-soft hover:bg-brand-700">Review Board</a>
        <a href="/clients" class="rounded-2xl border border-slate-200 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Clients</a>
        <a href="/billing" class="rounded-2xl border border-slate-200 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Billing</a>
      </div>
    </div>
  </section>

  {% set tabs = [
    ('overview','Overview'),
    ('branch-work','Branch Work'),
    ('clients','Clients'),
    ('staff','Staff'),
    ('review','Review'),
    ('billing','Billing'),
    ('reports','Reports'),
    ('wizards','Wizards')
  ] %}
  <section class="rounded-3xl border border-slate-200 bg-white p-2 shadow-soft">
    <div class="flex gap-2 overflow-x-auto" id="partner-dashboard-tabs">
      {% for code, label in tabs %}
      <button type="button" data-tab="{{ code }}" class="partner-dashboard-tab whitespace-nowrap rounded-2xl px-4 py-2 text-sm font-semibold transition {% if active_tab == code %}bg-brand-600 text-white{% else %}text-slate-600 hover:bg-slate-100{% endif %}">{{ label }}</button>
      {% endfor %}
    </div>
  </section>

  <section id="partner-dashboard-panel">
    {% include 'modules/partner_dashboard/templates/partner_dashboard/partials/' ~ (active_tab|replace('-', '_')) ~ '.html' ignore missing %}
  </section>
</div>

<script>
(function(){
  const panel = document.getElementById('partner-dashboard-panel');
  const buttons = Array.from(document.querySelectorAll('.partner-dashboard-tab'));
  async function loadTab(tab) {
    buttons.forEach(btn => {
      const active = btn.dataset.tab === tab;
      btn.classList.toggle('bg-brand-600', active);
      btn.classList.toggle('text-white', active);
      btn.classList.toggle('text-slate-600', !active);
      btn.classList.toggle('hover:bg-slate-100', !active);
    });
    panel.innerHTML = '<div class="rounded-3xl border border-slate-200 bg-white p-8 text-center text-sm text-slate-500 shadow-soft">Loading...</div>';
    try {
      const res = await fetch('/partner/dashboard/tab/' + encodeURIComponent(tab), {headers: {'X-Requested-With':'fetch'}});
      if (!res.ok) throw new Error('HTTP ' + res.status);
      panel.innerHTML = await res.text();
      history.replaceState(null, '', '/partner/dashboard?tab=' + encodeURIComponent(tab));
    } catch (err) {
      panel.innerHTML = '<div class="rounded-3xl border border-red-200 bg-red-50 p-6 text-sm font-semibold text-red-700">Unable to load tab. Please refresh the page.</div>';
    }
  }
  buttons.forEach(btn => btn.addEventListener('click', () => loadTab(btn.dataset.tab)));
})();
</script>
{% endblock %}
