<h1>Fattura <%= @invoice.number %> — <%= @club.name %></h1>
<p style="color:#666">
  Stato: <strong><%= @invoice.status %></strong>
  <% if @invoice.emailed_at.present? %>
    · Inviata il <%= @invoice.emailed_at.in_time_zone.strftime("%d/%m/%Y %H:%M") %>
  <% end %>
</p>

<div class="card">
  <%= form_with model: @invoice, url: admin_club_billing_invoice_path(@club, @invoice), multipart: true, method: :patch do |f| %>
    <%= f.label :number, "Numero fattura" %>
    <%= f.text_field :number, required: true, disabled: @invoice.status.in?(%w[sent]) %>

    <%= f.label :issued_on, "Data emissione" %>
    <%= f.date_field :issued_on, required: true, disabled: @invoice.status.in?(%w[sent]) %>

    <%= label_tag :amount_euros, "Importo (€)" %>
    <%= number_field_tag "billing_invoice[amount_euros]",
          @invoice.amount_cents / 100.0,
          step: 0.01, min: 0.01, required: true, disabled: @invoice.status.in?(%w[sent]) %>

    <% if @payment %>
      <p style="color:#888;font-size:0.9rem">Pagamento: <%= @payment.display_description %> — <%= @payment.formatted_amount %></p>
    <% end %>

    <%= f.label :pdf, "PDF fattura" %>
    <% if @invoice.pdf.attached? %>
      <p style="color:#888">PDF già caricato: <%= @invoice.pdf.filename %></p>
    <% end %>
    <%= f.file_field :pdf, accept: "application/pdf" %>

    <%= f.label :notes, "Note interne" %>
    <%= f.text_area :notes, rows: 2 %>

    <div style="margin-top:16px;display:flex;gap:10px;flex-wrap:wrap">
      <% unless @invoice.status.in?(%w[sent]) %>
        <%= f.submit "Salva bozza", class: "btn btn-secondary" %>
        <%= f.submit "Emetti e invia via email", class: "btn btn-primary",
              data: { turbo_confirm: "Inviare la fattura a #{@club.billing_email}?" } %>
      <% end %>
    </div>
  <% end %>
</div>

<p><%= link_to "← Pagamenti e fatture", admin_club_billing_invoices_path(@club) %></p>
