{% 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">Platform Control Centre</p>
        <h1 class="mt-2 text-2xl font-bold text-slate-900">System Admin Dashboard V2</h1>
        <p class="mt-1 text-sm text-slate-500">Monitor firm readiness, catalogue health, SMTP, storage, billing readiness, reports, wizards and audit logs.</p>
      </div>
      <div class="flex flex-wrap gap-2">
        <a href="/wizards/system/firm/new" class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white shadow-soft hover:bg-brand-700">+ Create Firm</a>
        <a href="/email/platform-smtp" class="rounded-xl border border-slate-200 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Platform SMTP</a>
        <a href="/platform-billing" class="rounded-xl border border-slate-200 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Platform Billing</a>
      </div>
    </div>

    {% set tabs = [
      ('overview', 'Overview'),
      ('firms', 'Firms'),
      ('firm-setup-health', 'Setup Health'),
      ('catalogue', 'Service Catalogue'),
      ('smtp', 'Platform SMTP'),
      ('storage', 'Storage'),
      ('billing', 'Billing Readiness'),
      ('reports', 'Reports'),
      ('wizards', 'Wizards'),
      ('audit-logs', 'Audit Logs')
    ] %}
    <div class="mt-6 overflow-x-auto">
      <div class="flex min-w-max gap-2 rounded-2xl bg-slate-100 p-1">
        {% for tab_key, tab_label in tabs %}
        <a href="/system-admin/dashboard?tab={{ tab_key }}"
           data-dashboard-tab="{{ tab_key }}"
           class="system-admin-tab rounded-xl px-4 py-2 text-sm font-semibold transition {% if active_tab == tab_key %}bg-white text-brand-700 shadow-soft{% else %}text-slate-600 hover:bg-white hover:text-slate-900{% endif %}">
          {{ tab_label }}
        </a>
        {% endfor %}
      </div>
    </div>
  </div>

  <div id="system-admin-dashboard-panel"
       data-initial-tab="{{ active_tab }}"
       class="min-h-[360px]">
    <div class="rounded-3xl border border-slate-200 bg-white p-8 text-center text-sm text-slate-500 shadow-soft">Loading dashboard...</div>
  </div>
</section>

<script>
(function () {
  const panel = document.getElementById('system-admin-dashboard-panel');
  const tabs = Array.from(document.querySelectorAll('[data-dashboard-tab]'));
  if (!panel || !tabs.length) return;

  function setActive(tabName) {
    tabs.forEach((tab) => {
      const active = tab.dataset.dashboardTab === tabName;
      tab.classList.toggle('bg-white', active);
      tab.classList.toggle('text-brand-700', active);
      tab.classList.toggle('shadow-soft', active);
      tab.classList.toggle('text-slate-600', !active);
    });
  }

  async function loadTab(tabName, pushState) {
    setActive(tabName);
    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 response = await fetch('/system-admin/dashboard/tab/' + encodeURIComponent(tabName), {
        headers: { 'X-Requested-With': 'fetch' }
      });
      if (!response.ok) throw new Error('Tab load failed');
      panel.innerHTML = await response.text();
      if (pushState) {
        const url = new URL(window.location.href);
        url.searchParams.set('tab', tabName);
        history.pushState({ tab: tabName }, '', url.toString());
      }
    } catch (err) {
      panel.innerHTML = '<div class="rounded-3xl border border-red-200 bg-red-50 p-6 text-sm text-red-700 shadow-soft">Unable to load this tab. Please refresh the page.</div>';
    }
  }

  tabs.forEach((tab) => {
    tab.addEventListener('click', function (event) {
      event.preventDefault();
      loadTab(tab.dataset.dashboardTab, true);
    });
  });

  window.addEventListener('popstate', function () {
    const url = new URL(window.location.href);
    loadTab(url.searchParams.get('tab') || 'overview', false);
  });

  loadTab(panel.dataset.initialTab || 'overview', false);
})();
</script>
{% endblock %}
