{% 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-center justify-between gap-3">
    <div>
      <h2 class="text-xl font-semibold text-slate-900">My Managed Clients</h2>
      <p class="text-sm text-slate-500">Clients maintained by you in the consultant portal. These records do not modify the audit firm client master.</p>
    </div>
    <div class="flex gap-2">
      <a href="/consultant/dashboard" class="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-50">Dashboard</a>
      {% if can_add_managed_client %}
      <a href="/consultant/managed-clients/new" class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-700">Add Managed Client</a>
      {% else %}
      <span class="rounded-xl bg-slate-200 px-4 py-2 text-sm font-semibold text-slate-500">Limit Reached</span>
      {% endif %}
    </div>
  </div>

  {% if workspace_summary %}
  <div class="rounded-2xl border border-slate-200 bg-white p-4 text-sm text-slate-700 shadow-soft">
    Managed client usage: <strong>{{ workspace_summary.managed_clients_used }}</strong> / <strong>{{ workspace_summary.managed_clients_limit or 'No limit' }}</strong>
    {% if workspace_summary.managed_clients_remaining is not none %} • Remaining: <strong>{{ workspace_summary.managed_clients_remaining }}</strong>{% endif %}
  </div>
  {% endif %}

  <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_auto]">
      <input name="q" value="{{ q or '' }}" placeholder="Search name, PAN, GSTIN, email, mobile" 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 statuses</option>
        {% for code, label in managed_client_statuses %}<option value="{{ code }}" {% if status == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
      </select>
      <label class="inline-flex items-center gap-2 rounded-xl border border-slate-300 px-3 py-2 text-sm text-slate-700">
        <input type="checkbox" name="include_inactive" value="1" {% if include_inactive %}checked{% endif %}> Include inactive
      </label>
      <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-hidden rounded-2xl border border-slate-200 bg-white shadow-soft">
    <table class="min-w-full divide-y divide-slate-200 text-sm">
      <thead class="bg-slate-50 text-left text-xs font-semibold uppercase tracking-wide text-slate-500">
        <tr>
          <th class="px-4 py-3">Client</th>
          <th class="px-4 py-3">Contact</th>
          <th class="px-4 py-3">PAN / GSTIN</th>
          <th class="px-4 py-3">Stage</th>
          <th class="px-4 py-3">Status</th>
          <th class="px-4 py-3">Conversion</th>
          <th class="px-4 py-3 text-right">Action</th>
        </tr>
      </thead>
      <tbody class="divide-y divide-slate-100">
        {% for row in rows %}
        <tr class="hover:bg-slate-50">
          <td class="px-4 py-3">
            <div class="font-semibold text-slate-900">{{ row.client_name }}</div>
            <div class="text-xs text-slate-500">{{ row.client_code or '-' }}{% if row.trade_name %} • {{ row.trade_name }}{% endif %}</div>
          </td>
          <td class="px-4 py-3 text-slate-600">
            <div>{{ row.contact_person_name or '-' }}</div>
            <div class="text-xs">{{ row.mobile or row.email or '-' }}</div>
          </td>
          <td class="px-4 py-3 text-slate-600">
            <div>{{ row.pan or '-' }}</div>
            <div class="text-xs">{{ row.gstin or '-' }}</div>
          </td>
          <td class="px-4 py-3 text-slate-600">{{ row.relationship_stage.replace('_', ' ').title() }}</td>
          <td class="px-4 py-3">
            <span class="rounded-full px-2 py-1 text-xs font-semibold {% if row.is_active and row.status == 'active' %}bg-emerald-50 text-emerald-700{% elif row.status == 'prospect' %}bg-blue-50 text-blue-700{% else %}bg-slate-100 text-slate-600{% endif %}">{{ row.status.replace('_',' ').title() }}</span>
          </td>
          <td class="px-4 py-3 text-slate-600">
            {{ row.conversion_status.replace('_',' ').title() if row.conversion_status else 'Not Requested' }}
            {% if row.linked_firm_client_id %}<div class="text-xs text-emerald-700">Linked to firm client</div>{% endif %}
          </td>
          <td class="px-4 py-3 text-right">
            <a href="/consultant/managed-clients/{{ row.id }}" class="rounded-lg border border-slate-300 px-3 py-1.5 text-xs font-semibold text-slate-700 hover:bg-slate-50">Open</a>
          </td>
        </tr>
        {% else %}
        <tr><td colspan="7" class="px-4 py-8 text-center text-slate-500">No managed clients found.</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% endblock %}
