{% extends "base.html" %}
{% block title %}Facturas rectificativas{% endblock %}
{% block page_title %}Facturas rectificativas{% endblock %}

{% block topbar_actions %}
<a href="{{ url_for('rectificativas.nueva') }}" class="btn btn-primary btn-sm">
  <i class="bi bi-plus-lg me-1"></i>Nueva rectificativa
</a>
{% endblock %}

{% block content %}
<div class="card">
  <div class="card-body p-0">
    {% if rectificativas %}
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead>
          <tr>
            <th>Número</th>
            <th>Factura rectificada</th>
            <th>Cliente</th>
            <th>Fecha</th>
            <th>Estado</th>
            <th class="text-end">Total</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          {% for r in rectificativas %}
          <tr onclick="window.location='{{ url_for('rectificativas.detalle', id=r.id) }}'" style="cursor:pointer">
            <td class="fw-semibold text-danger">{{ r.numero }}</td>
            <td>
              {% if r.origen_numero %}
                <a href="{{ url_for('facturas.detalle_emitida', id=r.factura_origen_id) }}" class="text-muted small" onclick="event.stopPropagation()">
                  {{ r.origen_numero }}
                </a>
              {% else %}—{% endif %}
            </td>
            <td>{{ r.cliente_nombre or '—' }}</td>
            <td>{{ r.fecha }}</td>
            <td>
              <span class="badge {% if r.estado == 'emitida' %}bg-success{% else %}bg-secondary{% endif %}">
                {{ r.estado|capitalize }}
              </span>
            </td>
            <td class="text-end fw-semibold text-danger">{{ "%.2f"|format(r.total) }} €</td>
            <td class="text-end" onclick="event.stopPropagation()">
              <a href="{{ url_for('rectificativas.detalle', id=r.id) }}" class="btn btn-sm btn-outline-secondary btn-action">
                <i class="bi bi-eye"></i>
              </a>
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    {% else %}
    <div class="text-center py-5 text-muted">
      <i class="bi bi-file-earmark-minus fs-1 d-block mb-2"></i>
      <p>No hay facturas rectificativas.</p>
      <a href="{{ url_for('rectificativas.nueva') }}" class="btn btn-primary btn-sm">
        Crear la primera
      </a>
    </div>
    {% endif %}
  </div>
</div>
{% endblock %}
