{% extends "ui/templates/base/layout.html" %}
{% block content %}
{% include "modules/consultants/templates/consultants/_consultant_tabs.html" %}
<div class="space-y-6">
  <div class="flex flex-wrap items-start justify-between gap-4">
    <div>
      <h2 class="text-xl font-semibold text-slate-900">My Consultant Work Board</h2>
      <p class="text-sm text-slate-500">Consultant-visible work shared by the firm. Internal firm tasks and private notes are not shown here.</p>
    </div>
    <a href="/consultant/service-requests/new" class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Raise Service Request</a>
  </div>

  <form method="get" class="rounded-2xl border border-slate-200 bg-white p-4 shadow-soft">
    <div class="grid gap-3 md:grid-cols-[1fr_auto_auto]">
      <input name="q" value="{{ q or '' }}" placeholder="Search client, service, task, message" class="rounded-xl border border-slate-300 px-3 py-2 text-sm">
      <select name="status" class="rounded-xl border border-slate-300 px-3 py-2 text-sm">
        <option value="">All columns</option>
        {% for code, label in board.column_options %}<option value="{{ code }}" {% if status == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
      </select>
      <button class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Filter</button>
    </div>
  </form>

  <div class="overflow-x-auto pb-2">
    <div class="grid min-w-[1180px] gap-4 lg:grid-cols-6">
      {% for key, column in board.columns.items() %}
      <section class="rounded-2xl border border-slate-200 bg-slate-50 p-3">
        <div class="mb-3 flex items-center justify-between">
          <h3 class="text-sm font-semibold text-slate-800">{{ column["label"] }}</h3>
          <span class="rounded-full bg-white px-2 py-0.5 text-xs font-semibold text-slate-600">{{ column["items"]|length }}</span>
        </div>
        <div class="space-y-3">
          {% for item in column["items"] %}
          {% set task = item.task %}
          {% set client = item.client %}
          {% set catalogue = item.catalogue %}
          <a href="/work/engagements/{{ task.subscription_id }}" class="block rounded-2xl border border-slate-200 bg-white p-4 shadow-sm hover:border-brand-300 hover:shadow-soft">
            <div class="flex items-start justify-between gap-2">
              <div>
                <div class="text-sm font-semibold text-slate-900">{{ client.client_name }}</div>
                <div class="text-xs text-slate-500">{{ catalogue.service_name }}</div>
              </div>
              {% if item.is_overdue %}<span class="rounded-full bg-red-50 px-2 py-1 text-[11px] font-semibold text-red-700">Overdue</span>{% endif %}
            </div>
            <div class="mt-3 text-sm font-medium text-slate-800">{{ task.task_name }}</div>
            <div class="mt-2 flex flex-wrap gap-1 text-[11px] font-semibold">
              <span class="rounded-full bg-slate-100 px-2 py-1 text-slate-600">{{ task.status.replace('_',' ').title() }}</span>
              <span class="rounded-full bg-slate-100 px-2 py-1 text-slate-600">{{ task.priority.replace('_',' ').title() }}</span>
              {% if task.internal_target_date %}<span class="rounded-full bg-slate-100 px-2 py-1 text-slate-600">Due {{ task.internal_target_date.strftime('%d-%m-%Y') }}</span>{% endif %}
            </div>
            <div class="mt-3 line-clamp-3 rounded-xl bg-slate-50 p-2 text-xs text-slate-600">{{ item.comment.message }}</div>
          </a>
          {% else %}
          <div class="rounded-xl border border-dashed border-slate-300 bg-white p-4 text-center text-xs text-slate-500">No items</div>
          {% endfor %}
        </div>
      </section>
      {% endfor %}
    </div>
  </div>

  <div class="rounded-2xl border border-slate-200 bg-white shadow-soft">
    <div class="flex items-center justify-between border-b border-slate-200 px-4 py-3">
      <div>
        <h3 class="font-semibold text-slate-900">My Service Requests</h3>
        <p class="text-xs text-slate-500">Requests raised by you to the firm.</p>
      </div>
      <a href="/consultant/service-requests" class="text-xs font-semibold text-brand-700 hover:underline">View all</a>
    </div>
    <div class="divide-y divide-slate-100">
      {% for row in board.service_requests[:8] %}
      <a href="/consultant/service-requests/{{ row.id }}" class="flex flex-wrap items-center justify-between gap-3 p-4 hover:bg-slate-50">
        <div>
          <div class="font-semibold text-slate-900">{{ row.subject }}</div>
          <div class="text-xs text-slate-500">{{ row.request_no }} • {{ row.requested_service_name }}{% if row.requested_due_date %} • Due {{ row.requested_due_date.strftime('%d-%m-%Y') }}{% endif %}</div>
        </div>
        <span class="rounded-full bg-slate-100 px-2 py-1 text-xs font-semibold text-slate-700">{{ row.status.replace('_',' ').title() }}</span>
      </a>
      {% else %}
      <div class="p-6 text-sm text-slate-500">No service requests yet.</div>
      {% endfor %}
    </div>
  </div>
</div>
{% endblock %}
