{% extends "base.html" %}

{% block title %}Payments · Delphi Database{% endblock %}

{% block content %}
<div class="row g-3 align-items-center mb-3">
  <div class="col-auto">
    <h2 class="mb-0">Payments</h2>
  </div>
  <div class="col ms-auto">
    <form class="row g-2" method="get">
      <div class="col-md-2">
        <input type="date" class="form-control" name="from_date" value="{{ from_date or '' }}" placeholder="From">
      </div>
      <div class="col-md-2">
        <input type="date" class="form-control" name="to_date" value="{{ to_date or '' }}" placeholder="To">
      </div>
      <div class="col-md-2">
        <input type="text" class="form-control" name="file_no" value="{{ file_no or '' }}" placeholder="File #">
      </div>
      <div class="col-md-2">
        <input type="text" class="form-control" name="rolodex_id" value="{{ rolodex_id or '' }}" placeholder="Rolodex Id">
      </div>
      <div class="col-md-3">
        <input type="text" class="form-control" name="q" value="{{ q or '' }}" placeholder="Description contains">
      </div>
      <div class="col-auto">
        <input type="hidden" name="page_size" value="{{ page_size }}">
        <button class="btn btn-outline-primary" type="submit"><i class="bi bi-search me-1"></i>Search</button>
      </div>
      <div class="col-auto">
        <a class="btn btn-outline-secondary" href="/payments"><i class="bi bi-x-circle me-1"></i>Clear</a>
      </div>
    </form>
  </div>
  <div class="col-12 text-muted small">
    {% if total and total > 0 %}
      Showing {{ start_index }}–{{ end_index }} of {{ total }} | Page total: ${{ '%.2f'|format(page_total_amount) }}
    {% else %}
      No results
    {% endif %}
  </div>

  <div class="col-12">
    <div class="table-responsive">
      <table class="table table-hover align-middle">
        <thead class="table-light">
          <tr>
            <th style="width: 120px;">Date</th>
            <th style="width: 140px;">File #</th>
            <th>Client</th>
            <th>Type</th>
            <th>Description</th>
            <th class="text-end" style="width: 140px;">Amount</th>
          </tr>
        </thead>
        <tbody>
          {% if payments and payments|length > 0 %}
            {% for p in payments %}
            <tr>
              <td>{{ p.payment_date.strftime('%Y-%m-%d') if p.payment_date else '' }}</td>
              <td>{{ p.case.file_no if p.case else '' }}</td>
              <td>
                {% set client = p.case.client if p.case else None %}
                {% if client %}{{ client.last_name }}, {{ client.first_name }}{% else %}<span class="text-muted">—</span>{% endif %}
              </td>
              <td>{{ p.payment_type or '' }}</td>
              <td>{{ p.description or '' }}</td>
              <td class="text-end">{{ '%.2f'|format(p.amount) if p.amount is not none else '' }}</td>
            </tr>
            {% endfor %}
          {% else %}
            <tr><td colspan="6" class="text-center text-muted py-4">No payments found.</td></tr>
          {% endif %}
        </tbody>
      </table>
    </div>
  </div>

  <div class="col-12">
    {% if total_pages and total_pages > 1 %}
    <nav aria-label="Payments pagination">
      <ul class="pagination mb-0">
        <li class="page-item {% if page <= 1 %}disabled{% endif %}">
          <a class="page-link" href="/payments?page={{ page - 1 if page > 1 else 1 }}&page_size={{ page_size }}{% if from_date %}&from_date={{ from_date }}{% endif %}{% if to_date %}&to_date={{ to_date }}{% endif %}{% if file_no %}&file_no={{ file_no | urlencode }}{% endif %}{% if rolodex_id %}&rolodex_id={{ rolodex_id | urlencode }}{% endif %}{% if q %}&q={{ q | urlencode }}{% endif %}">
            &laquo;
          </a>
        </li>
        {% for p in range(1, total_pages + 1) %}
          <li class="page-item {% if p == page %}active{% endif %}">
            <a class="page-link" href="/payments?page={{ p }}&page_size={{ page_size }}{% if from_date %}&from_date={{ from_date }}{% endif %}{% if to_date %}&to_date={{ to_date }}{% endif %}{% if file_no %}&file_no={{ file_no | urlencode }}{% endif %}{% if rolodex_id %}&rolodex_id={{ rolodex_id | urlencode }}{% endif %}{% if q %}&q={{ q | urlencode }}{% endif %}">{{ p }}</a>
          </li>
        {% endfor %}
        <li class="page-item {% if page >= total_pages %}disabled{% endif %}">
          <a class="page-link" href="/payments?page={{ page + 1 if page < total_pages else total_pages }}&page_size={{ page_size }}{% if from_date %}&from_date={{ from_date }}{% endif %}{% if to_date %}&to_date={{ to_date }}{% endif %}{% if file_no %}&file_no={{ file_no | urlencode }}{% endif %}{% if rolodex_id %}&rolodex_id={{ rolodex_id | urlencode }}{% endif %}{% if q %}&q={{ q | urlencode }}{% endif %}">
            &raquo;
          </a>
        </li>
      </ul>
    </nav>
    {% endif %}
  </div>
</div>
{% endblock %}


