{% extends "admin_frontend/base.html" %}
{% load crispy_forms_tags breadcrumbs %}
{% block title %}Invoices{% endblock %}
{% block content %}
{% render_breadcrumbs breadcrumbs %}
<div class="flex items-center justify-between mb-4">
  <h1 class="text-2xl font-semibold">Invoices</h1>
</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:invoices_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">
  <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">Customer</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Total</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Paid</th>
        <th class="px-4 py-2 text-left text-xs font-medium text-gray-500">Balance</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">Issued</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 inv in invoices %}
      <tr>
        <td class="px-4 py-2">{{ inv.customer.name }}</td>
        <td class="px-4 py-2">{{ inv.total_amount }} {{ inv.currency_code }}</td>
        <td class="px-4 py-2">{{ inv.payments_total|default:0 }} {{ inv.currency_code }}</td>
        <td class="px-4 py-2">{{ inv.balance }} {{ inv.currency_code }}</td>
        <td class="px-4 py-2"><span class="px-2 py-1 rounded bg-gray-100 text-gray-700">{{ inv.status }}</span></td>
        <td class="px-4 py-2">{{ inv.issued_at|default:"-" }}</td>
        <td class="px-4 py-2">
          <div class="flex items-center justify-end gap-2">
            <details class="relative">
              <summary class="cursor-pointer px-3 py-2 border rounded text-gray-700 hover:bg-gray-50">Add Payment</summary>
              <div class="absolute right-0 mt-2 bg-white shadow rounded p-4 w-80" style="z-index: 1000;">
                <form method="post" action="{% url 'recycle_core:invoice_add_payment' inv.id %}" class="space-y-3">
                  {% csrf_token %}
                  {{ pay_form|crispy }}
                  <div class="flex justify-end">
                    <button class="px-3 py-2 rounded bg-blue-600 text-white">Record</button>
                  </div>
                </form>
              </div>
            </details>
          </div>
        </td>
      </tr>
      {% empty %}
      <tr><td class="px-4 py-4 text-center text-gray-500" colspan="5">No invoices.</td></tr>
      {% endfor %}
    </tbody>
  </table>
  {% include 'admin_frontend/_pagination.html' %}
</div>
{% endblock %}
