{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6" id="employee-portal-root">
  <section class="overflow-hidden rounded-3xl border border-slate-200 bg-white shadow-soft">
    <div class="bg-gradient-to-r from-brand-700 via-brand-600 to-slate-900 px-6 py-6 text-white">
      <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">My Workspace</p>
          <h2 class="mt-2 text-2xl font-semibold">Employee Portal</h2>
          <p class="mt-2 max-w-3xl text-sm text-brand-100">Punch attendance, view priority work, track HR details, documents, payslips and alerts from one staff workspace.</p>
        </div>
        <div class="flex flex-wrap gap-2">
          {% if employee %}
          <a href="/employee/attendance" class="rounded-xl bg-white px-4 py-2 text-sm font-semibold text-brand-700 hover:bg-brand-50">Punch / Attendance</a>
          <a href="/employee/work" class="rounded-xl border border-white/40 px-4 py-2 text-sm font-semibold text-white hover:bg-white/10">Open Work Board</a>
          {% else %}
          <a href="/employee/register" class="rounded-xl bg-white px-4 py-2 text-sm font-semibold text-brand-700 hover:bg-brand-50">Request Employee Linkage</a>
          {% endif %}
        </div>
      </div>
    </div>
  </section>

  {% set main_tabs = [
    ('overview','Overview'),
    ('work','Work'),
    ('attendance','Attendance'),
    ('hr','HR & Profile'),
    ('reports','Reports')
  ] %}

  <section class="rounded-2xl border border-slate-200 bg-white p-3 shadow-soft">
    <div class="mb-2 px-1 text-xs font-semibold uppercase tracking-wide text-slate-500">Employee Workspace</div>
    <div class="overflow-x-auto pb-1">
      <div class="flex min-w-max flex-nowrap gap-2" id="employee-portal-tabs">
        {% for code, label in main_tabs %}
        <button type="button" data-tab="{{ code }}" class="employee-portal-tab employee-portal-main-tab inline-flex shrink-0 items-center rounded-xl px-4 py-2 text-sm font-semibold whitespace-nowrap transition {% if active_tab == code %}bg-brand-600 text-white shadow-soft{% else %}border border-slate-300 bg-white text-slate-700 hover:bg-slate-50{% endif %}">
          {{ label }}{% if code == 'hr' and unread_alert_count is defined and unread_alert_count > 0 %}<span class="ml-2 rounded-full bg-white/20 px-2 py-0.5 text-[10px]">{{ unread_alert_count }}</span>{% endif %}
        </button>
        {% endfor %}
      </div>
    </div>
  </section>

  <section id="employee-portal-panel" class="space-y-6">
    {% include "modules/employees/templates/employees/portal_partials/overview.html" %}
  </section>
</div>

<script>
(function () {
  const panel = document.getElementById('employee-portal-panel');
  const mainTabs = Array.from(document.querySelectorAll('.employee-portal-main-tab'));
  if (!panel || !mainTabs.length) return;

  const groupMap = {
    'overview': 'overview',
    'work': 'work',
    'work-board': 'work',
    'due-today': 'work',
    'overdue': 'work',
    'client-pending': 'work',
    'returned': 'work',
    'documents': 'work',
    'attendance': 'attendance',
    'attendance-details': 'attendance',
    'attendance-punch': 'attendance',
    'attendance-history': 'attendance',
    'hr': 'hr',
    'profile': 'hr',
    'leave': 'hr',
    'payslips': 'hr',
    'offboarding': 'hr',
    'alerts': 'hr',
    'reports': 'reports'
  };

  function setActive(tabName) {
    const activeGroup = groupMap[tabName] || tabName || 'overview';
    mainTabs.forEach(function (button) {
      const active = button.dataset.tab === activeGroup;
      button.classList.toggle('bg-brand-600', active);
      button.classList.toggle('text-white', active);
      button.classList.toggle('shadow-soft', active);
      button.classList.toggle('border', !active);
      button.classList.toggle('border-slate-300', !active);
      button.classList.toggle('bg-white', !active);
      button.classList.toggle('text-slate-700', !active);
      button.classList.toggle('hover:bg-slate-50', !active);
    });
  }

  async function loadTab(tabName, pushState) {
    const safeTab = tabName || 'overview';
    setActive(safeTab);
    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('/employee/dashboard/tab/' + encodeURIComponent(safeTab), {
        headers: {'X-Requested-With': 'fetch'}
      });
      if (!response.ok) throw new Error('HTTP ' + response.status);
      panel.innerHTML = await response.text();
      if (pushState) {
        const url = new URL(window.location.href);
        url.searchParams.set('tab', safeTab);
        window.history.pushState({tab: safeTab}, '', url.toString());
      }
      if (window.EmployeePortalGeo && typeof window.EmployeePortalGeo.bind === 'function') {
        window.EmployeePortalGeo.bind();
      }
    } catch (error) {
      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>';
    }
  }

  document.addEventListener('click', function (event) {
    const button = event.target.closest('.employee-portal-tab');
    if (!button || !button.dataset.tab) return;
    event.preventDefault();
    loadTab(button.dataset.tab || 'overview', true);
  });

  window.addEventListener('popstate', function (event) {
    const params = new URLSearchParams(window.location.search);
    loadTab((event.state && event.state.tab) || params.get('tab') || 'overview', false);
  });
})();
</script>
{% endblock %}
