<h1>Fatturazione — <%= @club.name %></h1>
<p>
  <%= link_to "← Dashboard", admin_root_path %>
  · <%= link_to "Nuova fattura (senza pagamento)", new_admin_club_billing_invoice_path(@club), class: "btn btn-secondary" %>
</p>

<h2 style="margin-top:24px;font-size:1.1rem">Pagamenti</h2>
<% if @payments.any? %>
  <table class="data">
    <thead>
      <tr>
        <th>Data</th>
        <th>Descrizione</th>
        <th>Importo</th>
        <th>Fattura</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <% @payments.each do |payment| %>
        <% inv = payment.invoice %>
        <tr>
          <td><%= payment.paid_at&.to_date || payment.created_at.to_date %></td>
          <td><%= payment.display_description %></td>
          <td><%= payment.formatted_amount %></td>
          <td>
            <% if inv %>
              <%= inv.number %> — <%= inv.status %>
              <% if inv.pdf.attached? %> (PDF)<% end %>
            <% else %>
              —
            <% end %>
          </td>
          <td>
            <% if inv %>
              <%= link_to "Modifica / invia PDF", edit_admin_club_billing_invoice_path(@club, inv) %>
            <% else %>
              <%= link_to "Crea fattura", new_admin_club_billing_invoice_path(@club, billing_payment_id: payment.id), class: "btn btn-primary", style: "padding:6px 10px;font-size:0.85rem" %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
<% else %>
  <p>Nessun pagamento registrato per questa società.</p>
<% end %>

<h2 style="margin-top:32px;font-size:1.1rem">Tutte le fatture</h2>
<% if @invoices.any? %>
  <table class="data">
    <thead>
      <tr><th>Numero</th><th>Data</th><th>Importo</th><th>Stato</th><th>Pagamento</th><th></th></tr>
    </thead>
    <tbody>
      <% @invoices.each do |inv| %>
        <tr>
          <td><%= inv.number %></td>
          <td><%= inv.issued_on %></td>
          <td><%= inv.formatted_amount %></td>
          <td><%= inv.status %></td>
          <td><%= inv.billing_payment_id.present? ? "Sì" : "No" %></td>
          <td><%= link_to "Modifica", edit_admin_club_billing_invoice_path(@club, inv) %></td>
        </tr>
      <% end %>
    </tbody>
  </table>
<% else %>
  <p>Nessuna fattura.</p>
<% end %>
