{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  <section class="rounded-3xl bg-gradient-to-r from-slate-900 via-brand-700 to-brand-600 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.24em] text-brand-100">Staff Work Execution</p>
        <h1 class="mt-2 text-2xl font-bold">Staff Dashboard V1</h1>
        <p class="mt-2 max-w-3xl text-sm text-brand-100">Your assigned tasks, due work, client-pending items, documents, returned work and staff work wizards in one place.</p>
        <p class="mt-3 text-xs text-brand-100">FY: {{ overview.financial_year or 'All' }}{% if overview.employee %} · {{ overview.employee.full_name }}{% endif %}</p>
      </div>
      <div class="flex flex-wrap gap-2">
        <a href="/employees/work" class="rounded-xl bg-white px-4 py-2 text-sm font-semibold text-brand-700 hover:bg-brand-50">Open My Work</a>
        <a href="/documents" class="rounded-xl border border-white/40 px-4 py-2 text-sm font-semibold text-white hover:bg-white/10">Documents</a>
      </div>
    </div>
  </section>

  {% set tabs = [
    ('my-tasks','My Tasks'),
    ('due-today','Due Today'),
    ('overdue','Overdue'),
    ('client-pending','Client Pending'),
    ('documents','Documents'),
    ('returned-work','Returned Work'),
    ('reports','Reports'),
    ('wizards','Wizards')
  ] %}

  <section class="rounded-3xl border border-slate-200 bg-white p-3 shadow-soft">
    <div class="flex gap-2 overflow-x-auto" id="staff-tabs">
      {% for code, label in tabs %}
      <button type="button" data-tab="{{ code }}" class="staff-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="staff-dashboard-panel">
    {% include 'modules/staff_dashboard/templates/staff_dashboard/partials/' ~ (active_tab|replace('-', '_')) ~ '.html' ignore missing %}
  </section>
</div>

<script>
(function(){
  const tabs = document.querySelectorAll('.staff-tab');
  const panel = document.getElementById('staff-dashboard-panel');
  async function loadTab(tab) {
    tabs.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('/staff/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);
    } catch (err) {
      panel.innerHTML = '<div class="rounded-3xl border border-red-200 bg-red-50 p-8 text-center text-sm font-semibold text-red-700 shadow-soft">Unable to load tab. Please refresh the page.</div>';
    }
  }
  tabs.forEach(btn => btn.addEventListener('click', () => loadTab(btn.dataset.tab)));
})();
</script>
{% endblock %}
