{% extends "ui/templates/base/layout.html" %}

{% block content %}
<div class="space-y-6">
  <section class="rounded-3xl bg-gradient-to-r from-brand-700 to-slate-900 p-6 text-white shadow-soft">
    <div class="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
      <div>
        <p class="text-xs font-semibold uppercase tracking-[0.22em] text-brand-100">Client Portal</p>
        <h2 class="mt-2 text-2xl font-semibold">My Compliance Workspace</h2>
        <p class="mt-2 max-w-3xl text-sm text-brand-100">View pending actions, service progress, documents, invoices, payments and firm messages from one clean dashboard.</p>
      </div>
      <div class="flex flex-wrap gap-2">
        {% if client_row %}
        <a href="/client/documents" class="rounded-xl bg-white px-4 py-2 text-sm font-semibold text-brand-700 hover:bg-brand-50">Upload Documents</a>
        <a href="/client/messages" class="rounded-xl border border-white/30 px-4 py-2 text-sm font-semibold text-white hover:bg-white/10">Messages</a>
        <a href="/client/profile" class="rounded-xl border border-white/30 px-4 py-2 text-sm font-semibold text-white hover:bg-white/10">Profile</a>
        {% endif %}
      </div>
    </div>
  </section>

  {% if not client_row %}
  <div class="rounded-2xl border border-amber-200 bg-amber-50 p-5 text-sm text-amber-900 shadow-soft">
    We could not find a client master linked to your login email in the current audit firm. Please contact your firm admin to map this login to the correct client record.
  </div>
  {% else %}
  {% set client_tabs = [
    ('overview','Overview'),
    ('action-centre','Action Centre'),
    ('services-documents','Services & Documents'),
    ('billing-messages','Billing & Messages'),
    ('reports','Reports')
  ] %}

  <section class="rounded-3xl border border-slate-200 bg-white p-2 shadow-soft">
    <div class="flex gap-2 overflow-x-auto" id="client-dashboard-tabs">
      {% for code, label in client_tabs %}
      <button type="button" data-tab="{{ code }}" class="client-dashboard-tab whitespace-nowrap rounded-2xl px-4 py-2 text-sm font-semibold transition {% if (active_tab|default('overview')) == code %}bg-brand-600 text-white{% else %}text-slate-600 hover:bg-slate-100{% endif %}">{{ label }}</button>
      {% endfor %}
    </div>
  </section>

  <section id="client-dashboard-panel" class="min-h-[240px]">
    {% include "modules/clients/templates/clients/portal_partials/overview.html" %}
  </section>

  <script>
  (function(){
    const panel = document.getElementById('client-dashboard-panel');
    const buttons = Array.from(document.querySelectorAll('.client-dashboard-tab'));
    if (!panel || !buttons.length) return;

    function setActive(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);
      });
    }

    async function loadTab(tab) {
      setActive(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('/client/dashboard/tab/' + encodeURIComponent(tab), {headers: {'X-Requested-With':'fetch'}});
        if (!res.ok) throw new Error('HTTP ' + res.status);
        panel.innerHTML = await res.text();
        if (history && history.replaceState) {
          history.replaceState(null, '', '/client/dashboard?tab=' + encodeURIComponent(tab));
        }
      } 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 or open the linked full page.</div>';
      }
    }

    window.loadClientDashboardTab = loadTab;
    buttons.forEach((btn) => btn.addEventListener('click', () => loadTab(btn.dataset.tab || 'overview')));
  })();
  </script>
  {% endif %}
</div>
{% endblock %}
