<h2>Pagamenti da fatturare</h2>
<p style="color:#666;margin-bottom:16px">
  Pagamenti Stripe <strong>pagati</strong> senza PDF fattura. Genera il PDF nei tuoi sistemi, poi caricalo qui: viene associato al pagamento e inviato via email al cliente.
</p>

<% if @clubs.many? %>
  <p style="margin-bottom:16px">
    Filtra società:
    <%= link_to "Tutte", admin_billing_path, class: (@filter_club ? nil : "admin-nav-active") %>
    <% @clubs.each do |club| %>
      · <%= link_to club.name, admin_billing_path(club_id: club.id), class: (@filter_club&.id == club.id ? "admin-nav-active" : nil) %>
    <% end %>
  </p>
<% end %>

<% if @pending_payments.any? %>
  <div class="billing-pending-list">
    <% @pending_payments.each do |payment| %>
      <% club = payment.club %>
      <article id="payment-<%= payment.id %>" class="billing-pending-card">
        <header class="billing-pending-card__head">
          <div>
            <strong><%= club.name %></strong>
            · <%= (payment.paid_at || payment.created_at).to_date.strftime("%d/%m/%Y") %>
            · <%= payment.display_description %>
            · <strong><%= payment.formatted_amount %></strong>
          </div>
          <% unless club.billing_profile_complete? %>
            <span class="billing-pending-card__warn">Dati fatturazione incompleti</span>
          <% end %>
        </header>

        <div class="billing-pending-card__body">
          <% if club.billing_profile_invoice_lines.any? %>
            <dl class="billing-profile-dl">
              <% club.billing_profile_invoice_lines.each do |label, value| %>
                <dt><%= label %></dt>
                <dd><%= value %></dd>
              <% end %>
            </dl>
          <% else %>
            <p class="billing-pending-card__warn">Nessun dato di fatturazione — il cliente deve completare il profilo.</p>
          <% end %>

          <%= form_with url: admin_billing_payment_attach_pdf_path(payment),
                method: :post, multipart: true, local: true, class: "billing-upload-form" do %>
            <label class="billing-upload-form__label">
              PDF fattura
              <%= file_field_tag :pdf, accept: "application/pdf", required: true %>
            </label>
            <%= submit_tag "Carica PDF e invia al cliente", class: "admin-btn admin-btn--primary" %>
          <% end %>
        </div>
      </article>
    <% end %>
  </div>
<% else %>
  <p class="admin-flash" style="background:#1b3d1b;border-color:#2e7d32">
    Nessun pagamento in attesa di fattura<%= @filter_club ? " per #{@filter_club.name}" : "" %>.
  </p>
<% end %>

<% if @completed_payments.any? %>
  <h2 style="margin-top:40px;font-size:1.15rem">Fatture già caricate</h2>
  <table class="admin-table">
    <thead>
      <tr>
        <th>Data</th>
        <th>Società</th>
        <th>Descrizione</th>
        <th>Importo</th>
        <th>Fattura</th>
        <th>Stato</th>
      </tr>
    </thead>
    <tbody>
      <% @completed_payments.each do |payment| %>
        <% inv = payment.invoice %>
        <tr>
          <td><%= payment.paid_at&.to_date || payment.created_at.to_date %></td>
          <td><%= payment.club.name %></td>
          <td><%= payment.display_description %></td>
          <td><%= payment.formatted_amount %></td>
          <td><%= inv&.display_number %></td>
          <td><%= inv&.display_status %></td>
        </tr>
      <% end %>
    </tbody>
  </table>
<% end %>

<p style="margin-top:24px"><%= link_to "← Dashboard", admin_root_path %></p>
