{% extends "ui/templates/base/layout.html" %}
{% block content %}
{% include "modules/consultants/templates/consultants/_consultant_tabs.html" %}

<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 lg:flex-row lg:items-center lg:justify-between">
      <div>
        <p class="text-xs font-semibold uppercase tracking-[0.22em] text-brand-100">Consultant Portal</p>
        <h2 class="mt-2 text-2xl font-semibold">Consultant Workspace</h2>
        <p class="mt-2 max-w-3xl text-sm text-brand-100">A grouped workspace for assignments, client requests, documents, clarifications and consultant reports.</p>
      </div>
      <div class="flex flex-wrap gap-2">
        <a href="/consultant/work" class="rounded-xl bg-white px-4 py-2 text-sm font-semibold text-brand-700 hover:bg-brand-50">Open Work Board</a>
        <a href="/consultant/service-requests" class="rounded-xl border border-white/40 px-4 py-2 text-sm font-semibold text-white hover:bg-white/10">Service Requests</a>
        <a href="/consultant/communications" class="rounded-xl border border-white/40 px-4 py-2 text-sm font-semibold text-white hover:bg-white/10">Clarifications</a>
      </div>
    </div>
  </section>

  {# Dashboard sections are now available from the global Consultant Dashboard menu. #}
  <section id="consultant-portal-panel">
    {% if active_tab == 'assigned-work' %}
      {% include "modules/consultants/templates/consultants/portal_partials/assigned_work.html" %}
    {% elif active_tab == 'clients' %}
      {% include "modules/consultants/templates/consultants/portal_partials/clients.html" %}
    {% elif active_tab == 'documents' %}
      {% include "modules/consultants/templates/consultants/portal_partials/documents.html" %}
    {% elif active_tab == 'clarifications' %}
      {% include "modules/consultants/templates/consultants/portal_partials/clarifications.html" %}
    {% elif active_tab == 'requests' %}
      {% include "modules/consultants/templates/consultants/portal_partials/service_requests.html" %}
    {% elif active_tab == 'reports' %}
      {% include "modules/consultants/templates/consultants/portal_partials/reports.html" %}
    {% elif active_tab == 'work-centre' %}
      {% include "modules/consultants/templates/consultants/portal_partials/work_centre.html" %}
    {% elif active_tab == 'clients-requests' %}
      {% include "modules/consultants/templates/consultants/portal_partials/clients_requests.html" %}
    {% elif active_tab == 'documents-clarifications' %}
      {% include "modules/consultants/templates/consultants/portal_partials/documents_clarifications.html" %}
    {% elif active_tab == 'reports' %}
      {% include "modules/consultants/templates/consultants/portal_partials/reports.html" %}
    {% else %}
      {% include "modules/consultants/templates/consultants/portal_partials/overview.html" %}
    {% endif %}
  </section>
</div>

<script>
(function(){
  const panel = document.getElementById('consultant-portal-panel');
  if (!panel) return;

  async function loadTab(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('/consultant/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, '', '/consultant/dashboard?tab=' + encodeURIComponent(tab));
    } catch (err) {
      panel.innerHTML = '<div class="rounded-3xl border border-red-200 bg-red-50 p-8 text-center text-sm text-red-700 shadow-soft">Unable to load tab. Please refresh the page.</div>';
    }
  }

  window.loadConsultantDashboardTab = loadTab;
})();
</script>
{% endblock %}
