{% extends "base.html" %}
{% block title %}Rectificativa {{ rect.numero }}{% endblock %}
{% block page_title %}Rectificativa {{ rect.numero }}{% endblock %}

{% block topbar_actions %}
<button type="button" class="btn btn-outline-danger btn-sm no-print"
  data-url="{{ url_for('rectificativas.pdf', id=rect.id) }}"
  data-nombre="Rectificativa_{{ rect.numero|replace('/', '-') }}.pdf"
  onclick="verArchivo(this.dataset.url, this.dataset.nombre)">
  <i class="bi bi-file-earmark-pdf me-1"></i>Ver PDF
</button>
<a href="{{ url_for('rectificativas.pdf', id=rect.id) }}" download
   class="btn btn-danger btn-sm no-print">
  <i class="bi bi-download me-1"></i>Descargar PDF
</a>
{% if rect.origen_numero %}
<a href="{{ url_for('facturas.emitidas') }}" class="btn btn-outline-secondary btn-sm no-print">
  <i class="bi bi-link-45deg me-1"></i>Ver factura original
</a>
{% endif %}
<form method="post" action="{{ url_for('rectificativas.eliminar', id=rect.id) }}" class="d-inline no-print"
      onsubmit="return confirm('¿Eliminar esta rectificativa?')">
  <button class="btn btn-outline-danger btn-sm"><i class="bi bi-trash"></i></button>
</form>
{% endblock %}

{% block content %}
<div class="row justify-content-center">
  <div class="col-lg-9">
    <div class="card border-danger-subtle">
      <div class="card-body p-5">

        <!-- Cabecera -->
        <div class="d-flex justify-content-between align-items-start mb-4">
          <div>
            <h2 class="fw-bold mb-1">{{ empresa.nombre }}</h2>
            {% if empresa.nif %}<div class="text-muted small">NIF: {{ empresa.nif }}</div>{% endif %}
            {% if empresa.direccion %}<div class="text-muted small">{{ empresa.direccion }}</div>{% endif %}
            {% if empresa.cp or empresa.ciudad %}<div class="text-muted small">{{ empresa.cp }} {{ empresa.ciudad }}</div>{% endif %}
            {% if empresa.email %}<div class="text-muted small">{{ empresa.email }}</div>{% endif %}
          </div>
          <div class="text-end">
            <h1 class="fw-bold text-danger mb-1" style="font-size:1.25rem">FACTURA RECTIFICATIVA</h1>
            <div class="fs-5 fw-bold">{{ rect.numero }}</div>
            {% if rect.origen_numero %}
            <div class="text-muted small">Rectifica: <strong>{{ rect.origen_numero }}</strong></div>
            {% endif %}
            <div class="text-muted small">Fecha: {{ rect.fecha }}</div>
            <span class="badge mt-1 {% if rect.estado == 'emitida' %}bg-success{% else %}bg-secondary{% endif %}">
              {{ rect.estado|capitalize }}
            </span>
          </div>
        </div>

        <!-- Motivo -->
        {% if rect.motivo %}
        <div class="alert alert-warning py-2 mb-4">
          <i class="bi bi-exclamation-circle me-1"></i>
          <strong>Motivo:</strong> {{ rect.motivo }}
        </div>
        {% endif %}

        <!-- Datos cliente -->
        {% if rect.cliente_nombre %}
        <div class="row mb-4">
          <div class="col-md-6">
            <div class="bg-light rounded p-3">
              <div class="text-muted small fw-semibold mb-2">CLIENTE</div>
              <div class="fw-semibold">{{ rect.cliente_nombre }}</div>
              {% if rect.cliente_nif %}<div class="small text-muted">NIF: {{ rect.cliente_nif }}</div>{% endif %}
              {% if rect.cliente_direccion %}<div class="small text-muted">{{ rect.cliente_direccion }}</div>{% endif %}
              {% if rect.cliente_email %}<div class="small text-muted">{{ rect.cliente_email }}</div>{% endif %}
            </div>
          </div>
        </div>
        {% endif %}

        <!-- Líneas -->
        <div class="table-responsive mb-4">
          <table class="table">
            <thead style="background:#b41e2d;color:#fff">
              <tr>
                <th>Descripción</th>
                <th class="text-center">Cant.</th>
                <th class="text-end">Precio unit.</th>
                <th class="text-center">Dto.</th>
                <th class="text-end">Subtotal</th>
              </tr>
            </thead>
            <tbody>
              {% for l in lineas %}
              <tr>
                <td>{{ l.descripcion }}</td>
                <td class="text-center">{{ l.cantidad }}</td>
                <td class="text-end">{{ "%.2f"|format(l.precio_unitario) }} €</td>
                <td class="text-center">{{ l.descuento_pct }}%</td>
                <td class="text-end fw-semibold">{{ "%.2f"|format(l.subtotal) }} €</td>
              </tr>
              {% endfor %}
            </tbody>
          </table>
        </div>

        <!-- Totales -->
        <div class="row justify-content-end">
          <div class="col-md-4">
            <table class="table table-sm">
              <tr><td class="text-muted">Base imponible</td><td class="text-end">{{ "%.2f"|format(rect.subtotal) }} €</td></tr>
              <tr><td class="text-muted">IVA ({{ rect.iva_pct }}%)</td><td class="text-end">{{ "%.2f"|format(rect.iva_importe) }} €</td></tr>
              {% if rect.irpf_pct > 0 %}
              <tr><td class="text-muted text-danger">IRPF ({{ rect.irpf_pct }}%)</td><td class="text-end text-danger">−{{ "%.2f"|format(rect.irpf_importe) }} €</td></tr>
              {% endif %}
              <tr class="fw-bold fs-5">
                <td class="text-danger">TOTAL</td>
                <td class="text-end text-danger">{{ "%.2f"|format(rect.total) }} €</td>
              </tr>
            </table>
          </div>
        </div>

        {% if rect.notas %}
        <div class="border-top pt-3 mt-3">
          <div class="text-muted small fw-semibold">NOTAS</div>
          <div class="small">{{ rect.notas }}</div>
        </div>
        {% endif %}

      </div>
    </div>

    <!-- Cambiar estado -->
    <div class="card mt-3 no-print">
      <div class="card-header py-3"><i class="bi bi-arrow-repeat me-2"></i>Estado</div>
      <div class="card-body d-flex gap-2">
        {% for estado, color in [('emitida','success'),('cancelada','secondary')] %}
        <form method="post" action="{{ url_for('rectificativas.cambiar_estado', id=rect.id) }}" class="d-inline">
          <input type="hidden" name="estado" value="{{ estado }}">
          <button class="btn btn-outline-{{ color }} btn-sm {% if rect.estado == estado %}active{% endif %}">
            {{ estado|capitalize }}
          </button>
        </form>
        {% endfor %}
      </div>
    </div>

  </div>
</div>
{% endblock %}
