{% extends "ui/templates/base/layout.html" %}
{% block content %}
<section class="space-y-6">
  <div 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">Firm Setup Control Centre</p>
        <h1 class="mt-2 text-2xl font-bold text-slate-900">Firm Administration Dashboard</h1>
        <p class="mt-1 text-sm text-slate-500">Configure firm details, branches, users, roles, services, task templates, financial years, branding and billing readiness.</p>
      </div>
      <div class="flex flex-wrap gap-2">
        <a href="/system-settings/branches" class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white shadow-soft hover:bg-brand-700">+ Add Branch</a>
        <a href="/system-settings/users" class="rounded-xl border border-slate-200 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Users & Roles</a>
        {% if 'Partner' in current_user_roles %}<a href="/partner/dashboard" class="rounded-xl border border-brand-200 bg-brand-50 px-4 py-2 text-sm font-semibold text-brand-700 hover:bg-brand-100">Partner Operations</a>{% endif %}
      </div>
    </div>
  </div>

  {% set tabs = [
    ('overview','Overview'),
    ('branches','Branches'),
    ('users','Users & Roles'),
    ('firm-settings','Firm Settings'),
    ('services','Services Setup'),
    ('financial-years','Financial Years'),
    ('reports','Reports'),
    ('wizards','Wizards'),
    ('audit-logs','Audit Logs')
  ] %}
  <div class="rounded-3xl border border-slate-200 bg-white p-2 shadow-soft">
    <div class="flex gap-2 overflow-x-auto" id="firm-admin-tabs">
      {% for code, label in tabs %}
      <button type="button" data-tab="{{ code }}" class="firm-admin-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>
  </div>

  <div id="firm-admin-panel" class="min-h-[24rem]">
    {% include "modules/firm_admin_dashboard/templates/firm_admin_dashboard/partials/" ~ active_tab ~ ".html" ignore missing %}
  </div>
</section>

<script>
(function(){
  const panel = document.getElementById('firm-admin-panel');
  const buttons = Array.from(document.querySelectorAll('.firm-admin-tab'));
  if (!panel || !buttons.length) return;
  function activate(tab){
    buttons.forEach(btn => {
      const on = btn.dataset.tab === tab;
      btn.classList.toggle('bg-brand-600', on);
      btn.classList.toggle('text-white', on);
      btn.classList.toggle('text-slate-600', !on);
      btn.classList.toggle('hover:bg-slate-100', !on);
    });
  }
  async function loadTab(tab){
    activate(tab);
    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('/firm-admin/dashboard/tab/' + encodeURIComponent(tab), {headers: {'X-Requested-With':'fetch'}});
      if (!res.ok) throw new Error('HTTP ' + res.status);
      panel.innerHTML = await res.text();
      const url = new URL(window.location.href);
      url.searchParams.set('tab', tab);
      window.history.replaceState({}, '', url.toString());
    } catch (err) {
      panel.innerHTML = '<div class="rounded-3xl border border-red-200 bg-red-50 p-6 text-sm font-semibold text-red-800 shadow-soft">Unable to load tab. Please refresh the page.</div>';
    }
  }
  buttons.forEach(btn => btn.addEventListener('click', () => loadTab(btn.dataset.tab)));
})();
</script>
{% endblock %}

