{% extends "admin_frontend/base.html" %}
{% load breadcrumbs %}
{% block title %}Pickup #{{ pickup.id }}{% endblock %}
{% block content %}
{% render_breadcrumbs breadcrumbs %}

<div class="flex items-center justify-between mb-4">
  <h1 class="text-2xl font-semibold">Pickup #{{ pickup.id }}</h1>
  <div class="flex items-center gap-2">
    {% if pickup.weigh_ticket %}
      <a class="btn-outline" href="{% url 'recycle_core:weigh_ticket_detail' pickup.weigh_ticket.id %}">View Weigh Ticket</a>
      {% if perms.recycle_core.generate_invoice %}
      <form method="post" action="{% url 'recycle_core:pickup_generate_invoice' pickup.id %}">
        {% csrf_token %}
        <button class="px-3 py-2 rounded bg-blue-600 text-white">Generate Invoice</button>
      </form>
      {% endif %}
    {% else %}
      {% if perms.recycle_core.create_weigh_ticket %}
      <a class="px-3 py-2 rounded bg-blue-600 text-white" href="{% url 'recycle_core:pickup_create_weigh_ticket' pickup.id %}">Create Weigh Ticket</a>
      {% endif %}
    {% endif %}
  </div>
  
</div>

<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
  <div class="bg-white rounded shadow p-4">
    <h2 class="font-medium mb-2">Details</h2>
    <dl class="grid grid-cols-3 gap-2 text-sm">
      <dt class="text-gray-500">Customer</dt><dd class="col-span-2">{{ pickup.customer.name }}</dd>
      <dt class="text-gray-500">Site</dt><dd class="col-span-2">{{ pickup.site.name }}</dd>
      <dt class="text-gray-500">Status</dt><dd class="col-span-2">{{ pickup.status }}</dd>
      <dt class="text-gray-500">Scheduled</dt><dd class="col-span-2">{{ pickup.scheduled_at|default:"-" }}</dd>
      <dt class="text-gray-500">Driver</dt><dd class="col-span-2">{{ pickup.assigned_driver.username|default:"-" }}</dd>
      <dt class="text-gray-500">Notes</dt><dd class="col-span-2 whitespace-pre-wrap">{{ pickup.notes|default:"-" }}</dd>
    </dl>
  </div>
  <div class="bg-white rounded shadow p-4">
    <h2 class="font-medium mb-2">Items</h2>
    <table class="min-w-full">
      <thead>
        <tr class="text-left text-sm text-gray-600">
          <th class="px-2 py-1">Material</th>
          <th class="px-2 py-1">Est. Qty</th>
          <th class="px-2 py-1">Unit</th>
        </tr>
      </thead>
      <tbody class="divide-y">
        {% for it in items %}
          <tr>
            <td class="px-2 py-1">{{ it.material.name }}</td>
            <td class="px-2 py-1">{{ it.estimated_qty|default:"-" }}</td>
            <td class="px-2 py-1">{{ it.unit }}</td>
          </tr>
        {% empty %}
          <tr>
            <td colspan="3" class="px-2 py-2 text-gray-500">No items.</td>
          </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>

<div class="mt-6 bg-white rounded shadow p-4">
  <h2 class="font-medium mb-2">Weigh Tickets</h2>
  <table class="min-w-full">
    <thead>
      <tr class="text-left text-sm text-gray-600">
        <th class="px-2 py-1">Ticket</th>
        <th class="px-2 py-1">Recorded</th>
        <th class="px-2 py-1">Net</th>
        <th class="px-2 py-1">Actions</th>
      </tr>
    </thead>
    <tbody class="divide-y">
      {% if pickup.weigh_ticket %}
        <tr>
          <td class="px-2 py-1"><a class="text-blue-700 hover:underline" href="{% url 'recycle_core:weigh_ticket_detail' pickup.weigh_ticket.id %}">{{ pickup.weigh_ticket.ticket_number|default:pickup.weigh_ticket.id }}</a></td>
          <td class="px-2 py-1">{{ pickup.weigh_ticket.recorded_at }}</td>
          <td class="px-2 py-1">{{ pickup.weigh_ticket.net_weight }} {{ pickup.weigh_ticket.unit }}</td>
          <td class="px-2 py-1">
            {% if perms.recycle_core.generate_invoice %}
            <form method="post" action="{% url 'recycle_core:weigh_ticket_generate_invoice' pickup.weigh_ticket.id %}">
              {% csrf_token %}
              <button class="btn-outline btn-xs">Generate Invoice</button>
            </form>
            {% endif %}
          </td>
        </tr>
      {% else %}
        <tr>
          <td colspan="4" class="px-2 py-2 text-gray-500">No weigh tickets yet.</td>
        </tr>
      {% endif %}
    </tbody>
  </table>

  {% if not pickup.weigh_ticket %}
    <div class="mt-3">
      <a class="btn" href="{% url 'recycle_core:pickup_create_weigh_ticket' pickup.id %}">Create Weigh Ticket</a>
    </div>
  {% endif %}
</div>

{% endblock %}
