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

<div class="bg-white rounded shadow p-4 mb-4">
  <form method="get" class="flex flex-wrap gap-3 items-end">
    {{ filter.form|crispy }}
    <div class="flex items-center gap-2 w-full">
      <button class="px-3 py-2 bg-blue-600 text-white rounded">Filter</button>
      <a href="{% url 'recycle_core:pickups_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="overflow-x-auto">
  <table class="min-w-[1100px] 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">Customer</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Site</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Status</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Scheduled</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500 w-56 min-w-[220px]">Driver</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Actions</th>
      </tr>
    </thead>
    <tbody class="divide-y divide-gray-200">
      {% for p in pickups %}
      <tr>
        <td class="px-4 py-2">{{ p.customer.name }}</td>
        <td class="px-4 py-2">{{ p.site.name }}</td>
        <td class="px-4 py-2"><span class="px-2 py-1 rounded bg-gray-100 text-gray-700">{{ p.status }}</span></td>
        <td class="px-4 py-2 text-gray-500">{{ p.scheduled_at|date:"SHORT_DATETIME_FORMAT"|default:"-" }}</td>
        <td class="px-4 py-2 text-gray-600 w-56 min-w-[220px] whitespace-nowrap">{{ p.assigned_driver.username|default:"-" }}</td>
        <td class="px-4 py-2">
          <div class="flex flex-wrap gap-2 justify-end">
            <a class="text-blue-600 hover:text-blue-700 hover:underline text-xs font-medium mr-3" href="{% url 'recycle_core:pickup_detail' p.id %}">View</a>
            {% if perms.recycle_core.assign_driver %}
            <form method="post" action="{% url 'recycle_core:pickup_assign' p.id %}" class="flex items-center gap-2">
              {% csrf_token %}
              {{ assign_form.driver }}
              <button class="btn btn-xs">Assign</button>
            </form>
            {% endif %}
            {% if perms.recycle_core.set_pickup_status %}
            <form method="post" action="{% url 'recycle_core:pickup_set_status' p.id %}" class="flex items-center gap-2">
              {% csrf_token %}
              {{ status_form.status }}
              <button class="btn btn-xs">Update</button>
            </form>
            {% endif %}
            {% if perms.recycle_core.generate_invoice %}
            <form method="post" action="{% url 'recycle_core:pickup_generate_invoice' p.id %}">
              {% csrf_token %}
              <button class="btn btn-xs">Generate Invoice</button>
            </form>
            {% endif %}
            {% if perms.recycle_core.create_weigh_ticket %}
            <a class="btn btn-xs" href="{% url 'recycle_core:pickup_create_weigh_ticket' p.id %}">Create Weigh Ticket</a>
            {% endif %}
            {% with count=p.weigh_tickets.count %}
              {% if count %}
                <span class="text-xs text-gray-600">{{ count }} ticket{{ count|pluralize }}</span>
                <a class="btn-outline btn-xs" href="{% url 'recycle_core:pickup_detail' p.id %}#tickets">View</a>
              {% endif %}
            {% endwith %}
          </div>
        </td>
      </tr>
      {% empty %}
      <tr><td class="px-4 py-4 text-center text-gray-500" colspan="6">No pickups.</td></tr>
      {% endfor %}
    </tbody>
  </table>
  </div>
  {% include 'admin_frontend/_pagination.html' %}
</div>
{% endblock %}
