{% extends "ui/templates/base/layout.html" %}
{% block content %}
{% include "modules/managers/templates/managers/_manager_tabs.html" %}
<div class="space-y-6">
  <div class="flex flex-wrap items-start justify-between gap-3">
    <div>
      <h2 class="text-2xl font-semibold text-slate-900">Team Work Board</h2>
      <p class="mt-1 text-sm text-slate-500">Assign, monitor and follow up on engagement/service tasks by operational status.</p>
    </div>
    <a href="/employees/work" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Open Detailed Allocation</a>
  </div>

  <form method="get" action="/manager/work" class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft">
    <div class="grid gap-3 md:grid-cols-[1fr_260px_auto]">
      <input type="search" name="q" value="{{ q or '' }}" placeholder="Search task, client or engagement" 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="assigned_to_user_id" class="rounded-xl border border-slate-300 px-3 py-2 text-sm">
        <option value="0">All assignees</option>
        {% for user in assignable_users %}
        <option value="{{ user.id }}" {% if selected_assigned_to_user_id == user.id %}selected{% endif %}>{{ user.full_name or user.email }}</option>
        {% endfor %}
      </select>
      <button type="submit" class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Filter</button>
    </div>
  </form>

  <div class="grid gap-4 md:grid-cols-2 xl:grid-cols-5">
    {% for column in payload.columns %}
    <section class="min-h-[420px] rounded-2xl border border-slate-200 bg-slate-50 p-3 shadow-soft">
      <div class="mb-3 flex items-start justify-between gap-2">
        <div>
          <h3 class="font-semibold text-slate-900">{{ column.label }}</h3>
          <p class="text-xs text-slate-500">{{ column.hint }}</p>
        </div>
        <span class="rounded-full bg-white px-2.5 py-1 text-xs font-semibold text-slate-700">{{ column.count }}</span>
      </div>
      <div class="space-y-3">
        {% for task in column.tasks %}
        <article class="rounded-xl border border-slate-200 bg-white p-3 shadow-sm {% if task.is_overdue %}ring-1 ring-red-200{% elif task.is_due_today %}ring-1 ring-amber-200{% endif %}">
          <div class="flex items-start justify-between gap-2">
            <div>
              <div class="text-sm font-semibold text-slate-900">{{ task.task_name }}</div>
              <div class="mt-1 text-xs text-slate-500">{{ task.client_display }}</div>
              <div class="mt-1 text-xs text-slate-500">{{ task.engagement_label }}</div>
            </div>
            {% if task.is_overdue %}<span class="rounded-full bg-red-100 px-2 py-0.5 text-[10px] font-semibold text-red-700">Overdue</span>{% elif task.is_due_today %}<span class="rounded-full bg-amber-100 px-2 py-0.5 text-[10px] font-semibold text-amber-700">Today</span>{% endif %}
          </div>

          <div class="mt-3 grid grid-cols-2 gap-2 text-xs text-slate-600">
            <div><span class="font-semibold">Status:</span> {{ task.status_label }}</div>
            <div><span class="font-semibold">Priority:</span> {{ task.priority_label }}</div>
            <div class="col-span-2"><span class="font-semibold">Assigned:</span> {{ task.assignee_display }}</div>
            <div class="col-span-2"><span class="font-semibold">Target:</span> {{ task.internal_target_date or '-' }} · {{ task.date_bucket }}</div>
          </div>

          <form method="post" action="/employees/work/tasks/{{ task.id }}/assign" class="mt-3 space-y-2 rounded-xl border border-slate-100 bg-slate-50 p-2">
            <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
            <input type="hidden" name="return_url" value="/manager/work{% if q or selected_assigned_to_user_id %}?q={{ q|urlencode }}&assigned_to_user_id={{ selected_assigned_to_user_id or 0 }}{% endif %}">
            <select name="assigned_to_user_id" class="w-full rounded-lg border border-slate-300 px-2 py-1.5 text-xs">
              <option value="">Unassigned</option>
              {% for user in assignable_users %}
              <option value="{{ user.id }}" {% if task.assigned_to_user_id == user.id %}selected{% endif %}>{{ user.full_name or user.email }}</option>
              {% endfor %}
            </select>
            <div class="grid grid-cols-2 gap-2">
              <select name="status" class="rounded-lg border border-slate-300 px-2 py-1.5 text-xs">
                {% for code, label in task_statuses %}<option value="{{ code }}" {% if task.status == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
              </select>
              <select name="priority" class="rounded-lg border border-slate-300 px-2 py-1.5 text-xs">
                {% for code, label in task_priorities %}<option value="{{ code }}" {% if task.priority == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
              </select>
            </div>
            <input type="date" name="internal_target_date" value="{{ task.internal_target_date or '' }}" class="w-full rounded-lg border border-slate-300 px-2 py-1.5 text-xs">
            <textarea name="remarks" rows="2" placeholder="Manager note / allocation remark" class="w-full rounded-lg border border-slate-300 px-2 py-1.5 text-xs">{{ task.remarks or '' }}</textarea>
            <button type="submit" class="w-full rounded-lg bg-brand-600 px-3 py-1.5 text-xs font-semibold text-white hover:bg-brand-700">Save Allocation</button>
          </form>

          <div class="mt-3 flex items-center justify-between gap-2">
            <a href="/employees/work/tasks/{{ task.id }}/communication" class="rounded-lg border border-slate-300 px-2.5 py-1 text-xs font-semibold text-slate-700 hover:bg-slate-50">Timeline{% if task.comment_count %} · {{ task.comment_count }}{% endif %}</a>
            {% if task.subscription_id %}<a href="/work/engagements/{{ task.subscription_id }}" class="rounded-lg border border-slate-300 px-2.5 py-1 text-xs font-semibold text-slate-700 hover:bg-slate-50">Work Details</a>{% endif %}
          </div>
        </article>
        {% else %}
        <div class="rounded-xl border border-dashed border-slate-300 bg-white p-4 text-center text-sm text-slate-500">No tasks</div>
        {% endfor %}
      </div>
    </section>
    {% endfor %}
  </div>
</div>
{% endblock %}
