{% extends "base.html" %}
{% block title %}Libros de registros{% endblock %}
{% block page_title %}Libros de registros IVA{% endblock %}

{% block topbar_actions %}
<a href="{{ url_for('fiscal.libros_exportar', ejercicio=año, trimestre=trim) }}"
   class="btn btn-success btn-sm">
  <i class="bi bi-file-earmark-excel me-1"></i>Exportar Excel
</a>
<a href="{{ url_for('fiscal.index') }}" class="btn btn-outline-secondary btn-sm">
  <i class="bi bi-arrow-left me-1"></i>Volver al calendario
</a>
{% endblock %}

{% block content %}

<!-- Filtros -->
<div class="card mb-4 border-0 shadow-sm">
  <div class="card-body py-3">
    <form method="get" class="d-flex align-items-center gap-3 flex-wrap">
      <div class="d-flex align-items-center gap-2">
        <label class="fw-semibold text-muted">Ejercicio</label>
        <select name="ejercicio" class="form-select form-select-sm" style="width:110px">
          {% for a in años %}<option {{ 'selected' if a==año }}>{{ a }}</option>{% endfor %}
        </select>
      </div>
      <div class="d-flex align-items-center gap-2">
        <label class="fw-semibold text-muted">Período</label>
        <select name="trimestre" class="form-select form-select-sm" style="width:110px">
          <option value="anual" {{ 'selected' if trim=='anual' }}>Anual</option>
          {% for t in trimestres %}
          <option value="{{ t }}" {{ 'selected' if t==trim }}>{{ t }} — {{ trim_labels[t] }}</option>
          {% endfor %}
        </select>
      </div>
      <div class="d-flex align-items-center gap-2">
        <label class="fw-semibold text-muted">Libro</label>
        <select name="tipo" class="form-select form-select-sm" style="width:150px">
          <option value="emitidas" {{ 'selected' if tipo=='emitidas' }}>Solo emitidas</option>
          <option value="recibidas" {{ 'selected' if tipo=='recibidas' }}>Solo recibidas</option>
          <option value="ambas" {{ 'selected' if tipo=='ambas' }}>Ambos libros</option>
        </select>
      </div>
      <button type="submit" class="btn btn-primary btn-sm">
        <i class="bi bi-funnel me-1"></i>Filtrar
      </button>
    </form>
  </div>
</div>

<!-- Indicador de período -->
<div class="alert alert-light border d-flex gap-2 align-items-center mb-4 py-2" style="font-size:.85rem">
  <i class="bi bi-calendar-range text-primary"></i>
  Mostrando: <b>{{ fi }}</b> → <b>{{ ff }}</b>
  &nbsp;&mdash;&nbsp;
  {% if tipo=='emitidas' %}Libro de facturas emitidas
  {% elif tipo=='recibidas' %}Libro de facturas recibidas
  {% else %}Ambos libros{% endif %}
</div>

{% if tipo in ('emitidas','ambas') %}
<!-- Libro Facturas Emitidas -->
<div class="card border-0 shadow-sm mb-4">
  <div class="card-header border-0" style="background:linear-gradient(135deg,#eef3ff,#e0e7ff)">
    <div class="d-flex align-items-center justify-content-between">
      <div>
        <i class="bi bi-arrow-up-circle me-2 text-primary"></i>
        <span class="fw-semibold text-primary">Libro registro — Facturas emitidas</span>
      </div>
      <span class="badge bg-primary bg-opacity-10 text-primary">{{ emitidas|length }} facturas</span>
    </div>
  </div>
  <div class="card-body p-0">
    {% if emitidas %}
    <div class="table-responsive">
      <table class="table table-hover mb-0" style="font-size:.82rem">
        <thead>
          <tr>
            <th>Nº Factura</th>
            <th>Fecha</th>
            <th>Cliente / Destinatario</th>
            <th class="text-end">Base imp.</th>
            <th class="text-center">IVA %</th>
            <th class="text-end">Cuota IVA</th>
            <th class="text-center">IRPF %</th>
            <th class="text-end">Ret. IRPF</th>
            <th class="text-end fw-bold">Total</th>
            <th class="text-center">Estado</th>
          </tr>
        </thead>
        <tbody>
          {% for f in emitidas %}
          <tr>
            <td class="font-monospace">{{ f.numero }}</td>
            <td>{{ f.fecha }}</td>
            <td>{{ f.cliente or '—' }}</td>
            <td class="text-end">{{ "%.2f"|format(f.subtotal) }} €</td>
            <td class="text-center"><span class="badge bg-primary bg-opacity-10 text-primary">{{ f.iva_pct }}%</span></td>
            <td class="text-end text-primary">{{ "%.2f"|format(f.iva_importe) }} €</td>
            <td class="text-center">
              {% if f.irpf_pct %}<span class="badge bg-warning bg-opacity-20 text-warning-emphasis">{{ f.irpf_pct }}%</span>{% else %}—{% endif %}
            </td>
            <td class="text-end text-danger">
              {% if f.irpf_importe %}{{ "%.2f"|format(f.irpf_importe) }} €{% else %}—{% endif %}
            </td>
            <td class="text-end fw-semibold">{{ "%.2f"|format(f.total) }} €</td>
            <td class="text-center">
              <span class="badge-estado-{{ f.estado }}">{{ f.estado|capitalize }}</span>
            </td>
          </tr>
          {% endfor %}
        </tbody>
        <tfoot class="fw-bold" style="background:#eef3ff">
          <tr>
            <td colspan="3">TOTALES ({{ emitidas|length }} facturas)</td>
            <td class="text-end">{{ "%.2f"|format(tot_emit.base) }} €</td>
            <td></td>
            <td class="text-end text-primary">{{ "%.2f"|format(tot_emit.iva) }} €</td>
            <td></td>
            <td class="text-end text-danger">{{ "%.2f"|format(tot_emit.irpf) }} €</td>
            <td class="text-end">{{ "%.2f"|format(tot_emit.total) }} €</td>
            <td></td>
          </tr>
        </tfoot>
      </table>
    </div>
    {% else %}
    <div class="text-center py-5 text-muted">
      <i class="bi bi-file-earmark-x fs-1 d-block mb-2 opacity-30"></i>
      Sin facturas emitidas en este período
    </div>
    {% endif %}
  </div>
</div>
{% endif %}

{% if tipo in ('recibidas','ambas') %}
<!-- Libro Facturas Recibidas -->
<div class="card border-0 shadow-sm mb-4">
  <div class="card-header border-0" style="background:linear-gradient(135deg,#fff5f5,#fee2e2)">
    <div class="d-flex align-items-center justify-content-between">
      <div>
        <i class="bi bi-arrow-down-circle me-2 text-danger"></i>
        <span class="fw-semibold text-danger">Libro registro — Facturas recibidas</span>
      </div>
      <span class="badge bg-danger bg-opacity-10 text-danger">{{ recibidas|length }} facturas</span>
    </div>
  </div>
  <div class="card-body p-0">
    {% if recibidas %}
    <div class="table-responsive">
      <table class="table table-hover mb-0" style="font-size:.82rem">
        <thead>
          <tr>
            <th>Nº Factura</th>
            <th>Fecha</th>
            <th>Proveedor / Expedidor</th>
            <th class="text-end">Base imp.</th>
            <th class="text-center">IVA %</th>
            <th class="text-end">Cuota IVA</th>
            <th class="text-center">IRPF %</th>
            <th class="text-end">Ret. IRPF</th>
            <th class="text-end fw-bold">Total</th>
            <th class="text-center">Estado</th>
          </tr>
        </thead>
        <tbody>
          {% for f in recibidas %}
          <tr>
            <td class="font-monospace">{{ f.numero }}</td>
            <td>{{ f.fecha }}</td>
            <td>{{ f.proveedor or '—' }}</td>
            <td class="text-end">{{ "%.2f"|format(f.subtotal) }} €</td>
            <td class="text-center"><span class="badge bg-danger bg-opacity-10 text-danger">{{ f.iva_pct }}%</span></td>
            <td class="text-end text-danger">{{ "%.2f"|format(f.iva_importe) }} €</td>
            <td class="text-center">
              {% if f.irpf_pct %}<span class="badge bg-warning bg-opacity-20 text-warning-emphasis">{{ f.irpf_pct }}%</span>{% else %}—{% endif %}
            </td>
            <td class="text-end text-danger">
              {% if f.irpf_importe %}{{ "%.2f"|format(f.irpf_importe) }} €{% else %}—{% endif %}
            </td>
            <td class="text-end fw-semibold">{{ "%.2f"|format(f.total) }} €</td>
            <td class="text-center">
              <span class="badge-estado-{{ f.estado }}">{{ f.estado|capitalize }}</span>
            </td>
          </tr>
          {% endfor %}
        </tbody>
        <tfoot class="fw-bold" style="background:#fff5f5">
          <tr>
            <td colspan="3">TOTALES ({{ recibidas|length }} facturas)</td>
            <td class="text-end">{{ "%.2f"|format(tot_recib.base) }} €</td>
            <td></td>
            <td class="text-end text-danger">{{ "%.2f"|format(tot_recib.iva) }} €</td>
            <td></td>
            <td class="text-end text-danger">{{ "%.2f"|format(tot_recib.irpf) }} €</td>
            <td class="text-end">{{ "%.2f"|format(tot_recib.total) }} €</td>
            <td></td>
          </tr>
        </tfoot>
      </table>
    </div>
    {% else %}
    <div class="text-center py-5 text-muted">
      <i class="bi bi-file-earmark-x fs-1 d-block mb-2 opacity-30"></i>
      Sin facturas recibidas en este período
    </div>
    {% endif %}
  </div>
</div>
{% endif %}

{% endblock %}
