{% extends 'admin_frontend/base.html' %}
{% block title %}Leads{% endblock %}
{% block content %}
<div class="flex items-center justify-between mb-4">
  <h1 class="text-2xl font-semibold">Leads</h1>
</div>

<div class="bg-white rounded shadow p-4 mb-4">
  {% load crispy_forms_tags %}
  <form method="get" class="space-y-3 flex gap-2 items-end flex-wrap">
    {{ filter.form|crispy }}
    <div class="flex items-center gap-2">
      <button class="px-3 py-2 bg-blue-600 text-white rounded">Filter</button>
      <a href="{% url 'public_frontend_admin:leads_list' %}" class="btn-outline">Reset</a>
    </div>
  </form>
  {% if filter.qs.count != page_obj.paginator.count %}
    <p class="text-sm text-gray-500 mt-2">Filtered results: {{ page_obj.paginator.count }} items</p>
  {% endif %}
  </div>

<div class="bg-white rounded shadow overflow-hidden">
  <table class="min-w-full divide-y divide-gray-200">
    <thead class="bg-gray-50">
      <tr>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Created</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Organization</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Name</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Contact</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Source</th>
        <th class="px-4 py-2"></th>
      </tr>
    </thead>
    <tbody class="divide-y divide-gray-200">
      {% for l in leads %}
      <tr>
        <td class="px-4 py-2 text-gray-500 whitespace-nowrap">{{ l.created_at }}</td>
        <td class="px-4 py-2">{{ l.organization.name }} <span class="text-gray-400">({{ l.organization.code }})</span></td>
        <td class="px-4 py-2 font-medium">{{ l.name }}</td>
        <td class="px-4 py-2">
          {% if l.email %}<div class="text-sm">{{ l.email }}</div>{% endif %}
          {% if l.phone %}<div class="text-sm">{{ l.phone }}</div>{% endif %}
        </td>
        <td class="px-4 py-2">{{ l.get_source_display|default:'-' }}</td>
        <td class="px-4 py-2 text-right">
          <a class="btn-outline" href="{% url 'public_frontend_admin:lead_detail' l.pk %}">View</a>
        </td>
      </tr>
      {% empty %}
      <tr><td class="px-4 py-4 text-center text-gray-500" colspan="6">No leads found.</td></tr>
      {% endfor %}
    </tbody>
  </table>
  {% include 'admin_frontend/_pagination.html' %}
  </div>
{% endblock %}
