{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
  {% include "modules/employees/templates/employees/_my_workspace_tabs.html" %}

  <div class="flex flex-wrap items-start justify-between gap-3">
    <div>
      <h2 class="text-xl font-semibold text-slate-900">My Work</h2>
      <p class="mt-1 text-sm text-slate-500">One row per assigned engagement. Start, continue, follow up or view the full workflow from here.</p>
    </div>
  </div>

  {% if not employee %}
  <div class="rounded-2xl border border-amber-200 bg-amber-50 p-6 text-sm text-amber-900 shadow-soft">
    <h3 class="font-semibold">Your login is not linked to an employee master</h3>
    <p class="mt-1">You may still see tasks assigned directly to your user ID, but attendance, profile, leave, documents and payslip self-service require an employee master link.</p>
    <div class="mt-4 flex flex-wrap gap-2">
      <a href="/employee/register" class="rounded-xl bg-amber-700 px-4 py-2 text-sm font-semibold text-white hover:bg-amber-800">Request Employee Link</a>
      <a href="/employee/dashboard" class="rounded-xl border border-amber-300 px-4 py-2 text-sm font-semibold text-amber-900 hover:bg-amber-100">Back to My Workspace</a>
    </div>
  </div>
  {% endif %}

  <div class="grid gap-4 md:grid-cols-6">
    <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-slate-500">Engagements</div><div class="mt-1 text-2xl font-semibold">{{ work_payload.summary.total }}</div></div>
    <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-slate-500">Open</div><div class="mt-1 text-2xl font-semibold">{{ work_payload.summary.open }}</div></div>
    <div class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-slate-500">In Progress</div><div class="mt-1 text-2xl font-semibold">{{ work_payload.summary.in_progress }}</div></div>
    <div class="rounded-2xl border border-rose-200 bg-rose-50 p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-rose-600">Blocked</div><div class="mt-1 text-2xl font-semibold text-rose-700">{{ work_payload.summary.blocked }}</div></div>
    <div class="rounded-2xl border border-red-200 bg-red-50 p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-red-600">Overdue</div><div class="mt-1 text-2xl font-semibold text-red-700">{{ work_payload.summary.overdue }}</div></div>
    <div class="rounded-2xl border border-emerald-200 bg-emerald-50 p-4 shadow-soft"><div class="text-xs font-semibold uppercase text-emerald-600">Completed</div><div class="mt-1 text-2xl font-semibold text-emerald-700">{{ work_payload.summary.completed }}</div></div>
  </div>

  <form method="get" action="/employee/work" class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft">
    <div class="grid gap-3 md:grid-cols-[1fr_220px_auto]">
      <input type="search" name="q" value="{{ q or '' }}" placeholder="Search engagement, task, client or service" class="rounded-xl border border-slate-300 px-3 py-2 text-sm focus:border-brand-500 focus:outline-none focus:ring-2 focus:ring-brand-100">
      <select name="status" class="rounded-xl border border-slate-300 px-3 py-2 text-sm focus:border-brand-500 focus:outline-none focus:ring-2 focus:ring-brand-100">
        <option value="open" {% if status == 'open' %}selected{% endif %}>Open engagements</option>
        <option value="pending" {% if status == 'pending' %}selected{% endif %}>Pending</option>
        <option value="in_progress" {% if status == 'in_progress' %}selected{% endif %}>In Progress</option>
        <option value="blocked" {% if status == 'blocked' %}selected{% endif %}>Blocked</option>
        <option value="completed" {% if status == 'completed' %}selected{% endif %}>Completed</option>
        <option value="closed" {% if status == 'closed' %}selected{% endif %}>Closed engagements</option>
      </select>
      <button type="submit" class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Apply</button>
    </div>
  </form>

  <div class="space-y-5">
    {% for column in work_payload.columns %}
    <section class="rounded-2xl border border-slate-200 bg-slate-50 p-4 shadow-soft">
      <div class="mb-4 flex items-center justify-between gap-3">
        <div>
          <h3 class="text-base font-semibold text-slate-900">{{ column.label }}</h3>
          {% if column.code == 'pending' %}<p class="mt-1 text-xs text-slate-500">Assigned engagements where work has not started.</p>{% endif %}
          {% if column.code == 'in_progress' %}<p class="mt-1 text-xs text-slate-500">Started engagements with remaining assigned work.</p>{% endif %}
          {% if column.code == 'blocked' %}<p class="mt-1 text-xs text-slate-500">Engagements waiting for documents, clarification, review or another dependency.</p>{% endif %}
          {% if column.code == 'completed' %}<p class="mt-1 text-xs text-slate-500">All tasks assigned to you in these engagements are complete.</p>{% endif %}
        </div>
        <span class="rounded-full bg-white px-3 py-1 text-xs font-semibold text-slate-600">{{ column.total_cards }}</span>
      </div>

      <div class="overflow-x-auto rounded-2xl border border-slate-200 bg-white">
        <table class="min-w-[1180px] w-full border-collapse text-sm">
          <thead class="bg-slate-50">
            <tr class="border-b border-slate-200 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">
              <th class="px-4 py-3">Client / Scope</th>
              <th class="px-4 py-3">Service</th>
              <th class="px-4 py-3">FY / Due</th>
              <th class="px-4 py-3">Progress</th>
              <th class="px-4 py-3">Next / Pending</th>
              <th class="px-4 py-3">Status</th>
              <th class="px-4 py-3 text-right">Action</th>
            </tr>
          </thead>
          <tbody class="divide-y divide-slate-100">
            {% for card in column.cards %}
            <tr class="align-top hover:bg-slate-50/70">
              <td class="px-4 py-4">
                <div class="text-[11px] font-semibold uppercase tracking-wide text-slate-500">{{ card.client_code or 'Client' }}</div>
                <div class="mt-1 font-semibold text-slate-900">{{ card.client_name }}</div>
                {% if card.scope_secondary %}
                <div class="mt-1 text-xs text-slate-500">{{ card.scope_secondary }}</div>
                {% endif %}
                {% if card.scope_context %}
                <div class="mt-0.5 text-xs text-slate-400">{{ card.scope_context }}</div>
                {% endif %}
              </td>

              <td class="px-4 py-4">
                <div class="font-semibold text-slate-900">{{ card.service_name }}</div>
                {% if card.period_label %}
                <div class="mt-1 text-xs text-slate-500">{{ card.period_label }}</div>
                {% endif %}
              </td>

              <td class="px-4 py-4 whitespace-nowrap">
                <div class="font-medium text-slate-700">FY {{ card.financial_year }}</div>
                <div class="mt-1 text-xs text-slate-500">Due {{ card.due_date or '-' }}</div>
              </td>

              <td class="px-4 py-4">
                <div class="min-w-[180px]">
                  <div class="flex items-center justify-between gap-3 text-xs">
                    <span class="font-semibold text-slate-700">{{ card.progress_percent }}%</span>
                    <span class="text-slate-500">{{ card.completed_count }}/{{ card.task_count }} tasks</span>
                  </div>
                  <div class="mt-2 h-2 overflow-hidden rounded-full bg-slate-200">
                    <div class="h-full rounded-full bg-brand-600" style="width: {{ card.progress_percent }}%"></div>
                  </div>
                  <div class="mt-1 text-[11px] text-slate-400">Weighted progress</div>
                </div>
              </td>

              <td class="px-4 py-4">
                {% if column.code == 'blocked' %}
                  <div class="max-w-[300px]">
                    <div class="font-medium text-rose-800">{{ card.blocked_reason }}</div>
                    {% if card.blocked_notes %}<div class="mt-1 text-xs text-rose-600">{{ card.blocked_notes }}</div>{% endif %}
                    {% if card.follow_up_date %}<div class="mt-1 text-xs font-semibold text-rose-700">Follow-up: {{ card.follow_up_date }}</div>{% endif %}
                    {% if card.blocked_task_name %}<div class="mt-1 text-xs text-rose-600">Task: {{ card.blocked_task_name }}</div>{% endif %}
                  </div>
                {% elif card.next_task_name and column.code != 'completed' %}
                  <div class="max-w-[300px] text-slate-700">{{ card.next_task_name }}</div>
                {% else %}
                  <span class="text-slate-400">—</span>
                {% endif %}
                {% if card.latest_comment %}
                <div class="mt-1 max-w-[300px] truncate text-xs text-slate-500">Latest: {{ card.latest_comment.message }}</div>
                {% endif %}
              </td>

              <td class="px-4 py-4 whitespace-nowrap">
                <div class="flex flex-col items-start gap-1.5">
                  {% if card.is_overdue %}
                  <span class="rounded-full bg-red-100 px-2 py-1 text-[11px] font-semibold text-red-700">Overdue</span>
                  {% elif card.is_due_today %}
                  <span class="rounded-full bg-amber-100 px-2 py-1 text-[11px] font-semibold text-amber-700">Due today</span>
                  {% endif %}

                  {% if column.code == 'pending' %}
                  <span class="rounded-full bg-slate-100 px-2 py-1 text-[11px] font-semibold text-slate-700">Pending</span>
                  {% elif column.code == 'in_progress' %}
                  <span class="rounded-full bg-blue-100 px-2 py-1 text-[11px] font-semibold text-blue-700">In Progress</span>
                  {% elif column.code == 'blocked' %}
                  <span class="rounded-full bg-rose-100 px-2 py-1 text-[11px] font-semibold text-rose-700">Blocked</span>
                  {% else %}
                  <span class="rounded-full bg-emerald-100 px-2 py-1 text-[11px] font-semibold text-emerald-700">Completed</span>
                  {% endif %}
                </div>
              </td>

              <td class="px-4 py-4 text-right">
                <div class="inline-flex min-w-[140px] flex-col gap-2">
                  {% if column.code == 'pending' %}
                  <form method="post" action="/employee/work/engagements/{{ card.engagement_id }}/start">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                    <button type="submit" class="inline-flex w-full justify-center rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Start</button>
                  </form>
                  {% elif column.code == 'in_progress' %}
                  <a href="/employee/work/engagements/{{ card.engagement_id }}" class="inline-flex w-full justify-center rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Continue</a>
                  {% elif column.code == 'blocked' %}
                  <a href="/employee/work/engagements/{{ card.engagement_id }}" class="inline-flex w-full justify-center rounded-xl bg-rose-600 px-4 py-2 text-sm font-semibold text-white hover:bg-rose-700">Open / Follow Up</a>
                  {% if card.manual_blocker %}
                  <form method="post" action="/employee/work/engagements/{{ card.engagement_id }}/resume">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                    <button type="submit" class="inline-flex w-full justify-center rounded-xl border border-emerald-300 bg-emerald-50 px-4 py-2 text-sm font-semibold text-emerald-700 hover:bg-emerald-100">Resume Workflow</button>
                  </form>
                  {% endif %}
                  {% else %}
                  <a href="/employee/work/engagements/{{ card.engagement_id }}" class="inline-flex w-full justify-center rounded-xl border border-slate-300 bg-white px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">View</a>
                  {% endif %}
                </div>
              </td>
            </tr>
            {% else %}
            <tr>
              <td colspan="7" class="px-6 py-8 text-center text-sm text-slate-500">
                No engagements in this section.
              </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>

      {% if column.code == 'completed' and work_payload.completed_pagination.pages > 1 %}
      <div class="mt-4 flex flex-wrap items-center justify-between gap-3 rounded-xl border border-slate-200 bg-white px-4 py-3">
        <div class="text-xs text-slate-500">
          Showing {{ work_payload.completed_pagination.start }}-{{ work_payload.completed_pagination.end }} of {{ work_payload.completed_pagination.total }} completed engagements
        </div>
        <div class="flex items-center gap-2">
          {% if work_payload.completed_pagination.has_previous %}
          <form method="get" action="/employee/work">
            <input type="hidden" name="q" value="{{ q or '' }}">
            <input type="hidden" name="status" value="{{ status }}">
            <input type="hidden" name="completed_page" value="{{ work_payload.completed_pagination.previous_page }}">
            <button type="submit" class="rounded-lg border border-slate-300 bg-white px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50">Previous</button>
          </form>
          {% endif %}
          <span class="rounded-lg bg-slate-100 px-3 py-1.5 text-xs font-semibold text-slate-700">Page {{ work_payload.completed_pagination.page }} of {{ work_payload.completed_pagination.pages }}</span>
          {% if work_payload.completed_pagination.has_next %}
          <form method="get" action="/employee/work">
            <input type="hidden" name="q" value="{{ q or '' }}">
            <input type="hidden" name="status" value="{{ status }}">
            <input type="hidden" name="completed_page" value="{{ work_payload.completed_pagination.next_page }}">
            <button type="submit" class="rounded-lg border border-slate-300 bg-white px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50">Next</button>
          </form>
          {% endif %}
        </div>
      </div>
      {% endif %}
    </section>
    {% endfor %}
  </div>
</div>
{% endblock %}
