{% extends "admin_frontend/base.html" %}
{% load crispy_forms_tags breadcrumbs %}
{% block title %}Customers{% endblock %}
{% block content %}
{% render_breadcrumbs breadcrumbs %}
<div class="flex items-center justify-between mb-4">
  <h1 class="text-2xl font-semibold">Customers</h1>
  <details class="relative">
    <summary class="cursor-pointer px-3 py-2 bg-blue-600 text-white rounded">New</summary>
    <div class="absolute right-0 mt-2 bg-white shadow rounded p-4 w-96" style="z-index: 1000;">
      {% include 'admin_frontend/_form.html' with form=form submit_label='Create' %}
    </div>
  </details>
</div>

<div class="bg-white rounded shadow p-4 mb-4">
  <form method="get" class="space-y-3 flex gap-2">
    {{ 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 'recycle_core:customers_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">
  <div class="p-4 border-b">
    <h2 class="font-medium">Customer List</h2>
  </div>
  <div class="overflow-x-auto">
    <table class="min-w-full">
      <thead>
        <tr class="text-left text-sm text-gray-600">
          <th class="px-4 py-2">Organization</th>
          <th class="px-4 py-2">Name</th>
          <th class="px-4 py-2">Email</th>
          <th class="px-4 py-2">Phone</th>
          <th class="px-4 py-2">Price List</th>
          <th class="px-4 py-2">Actions</th>
        </tr>
      </thead>
      <tbody class="divide-y">
        {% for c in customers %}
          <tr>
            <td class="px-4 py-2">{{ c.organization.name }}</td>
            <td class="px-4 py-2">{{ c.name }}</td>
            <td class="px-4 py-2">{{ c.email }}</td>
            <td class="px-4 py-2">{{ c.phone }}</td>
            <td class="px-4 py-2">{% if c.price_list %}{{ c.price_list.name }}{% else %}-{% endif %}</td>
            <td class="px-4 py-2 space-x-2">
              <a class="text-blue-600 hover:underline" href="{% url 'recycle_core:customer_edit' c.id %}">Edit</a>
              <form method="post" action="{% url 'recycle_core:customer_delete' c.id %}" class="inline">
                {% csrf_token %}
                <button class="text-red-600 hover:underline" onclick="return confirm('Delete customer?')">Delete</button>
              </form>
            </td>
          </tr>
        {% empty %}
          <tr>
            <td class="px-4 py-4 text-gray-500" colspan="6">No customers.</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% include "admin_frontend/_pagination.html" %}
</div>
{% endblock %}
