{% extends 'base.html' %}
{% block title %}Activos Fijos{% endblock %}

{% block content %}
<div class="d-flex align-items-center justify-content-between mb-4">
  <div>
    <h4 class="fw-bold mb-1" style="color:#0f2744"><i class="bi bi-buildings me-2" style="color:#a3e635"></i>Activos Fijos</h4>
    <p class="text-muted mb-0" style="font-size:.85rem">Registro de inmovilizado material e intangible con cálculo de amortización.</p>
  </div>
  <a href="{{ url_for('activos.nuevo') }}" class="btn btn-primary btn-sm">
    <i class="bi bi-plus-lg me-1"></i>Nuevo activo
  </a>
</div>

{% with msgs = get_flashed_messages(with_categories=true) %}
  {% for cat, msg in msgs %}
    <div class="alert alert-{{ cat }} alert-dismissible fade show">{{ msg }}<button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
  {% endfor %}
{% endwith %}

<!-- Stats -->
<div class="row g-3 mb-4">
  <div class="col-6 col-md-3">
    <div class="stat-card" style="background:linear-gradient(135deg,#0f2744,#1e4a8a)">
      <div style="font-size:.72rem;opacity:.7;text-transform:uppercase;letter-spacing:1px">Activos registrados</div>
      <div style="font-size:1.8rem;font-weight:800;margin-top:4px">{{ stats.activos }}</div>
    </div>
  </div>
  <div class="col-6 col-md-3">
    <div class="stat-card" style="background:linear-gradient(135deg,#065f46,#047857)">
      <div style="font-size:.72rem;opacity:.7;text-transform:uppercase;letter-spacing:1px">Coste total</div>
      <div style="font-size:1.4rem;font-weight:800;margin-top:4px">{{ "%.2f €"|format(stats.coste_total) }}</div>
    </div>
  </div>
  <div class="col-6 col-md-3">
    <div class="stat-card" style="background:linear-gradient(135deg,#7c2d12,#c2410c)">
      <div style="font-size:.72rem;opacity:.7;text-transform:uppercase;letter-spacing:1px">Amort. acumulada</div>
      <div style="font-size:1.4rem;font-weight:800;margin-top:4px">{{ "%.2f €"|format(stats.amort_total) }}</div>
    </div>
  </div>
  <div class="col-6 col-md-3">
    <div class="stat-card" style="background:linear-gradient(135deg,#1e3a8a,#2d6cc8)">
      <div style="font-size:.72rem;opacity:.7;text-transform:uppercase;letter-spacing:1px">Valor neto contable</div>
      <div style="font-size:1.4rem;font-weight:800;margin-top:4px">{{ "%.2f €"|format(stats.valor_neto) }}</div>
    </div>
  </div>
</div>

{% if activos %}
<div class="card">
  <div class="table-responsive">
    <table class="table table-hover mb-0">
      <thead>
        <tr>
          <th>Nombre</th>
          <th style="width:110px">Cuenta</th>
          <th style="width:100px">F. adquisición</th>
          <th class="text-end" style="width:120px">Coste</th>
          <th class="text-end" style="width:120px">Amort. acum.</th>
          <th class="text-end" style="width:120px">Valor neto</th>
          <th style="width:80px">Método</th>
          <th style="width:60px">Estado</th>
          <th style="width:80px"></th>
        </tr>
      </thead>
      <tbody>
        {% for a in activos %}
        {% set vn = a.coste_adquisicion - (a.amort_acumulada or 0) %}
        {% set pct_amort = ((a.amort_acumulada or 0) / a.coste_adquisicion * 100) if a.coste_adquisicion else 0 %}
        <tr>
          <td>
            <a href="{{ url_for('activos.detalle', id=a.id) }}"
               class="fw-bold text-decoration-none" style="color:#0f2744">
              {{ a.nombre }}
            </a>
            {% if a.descripcion %}
            <div class="text-muted" style="font-size:.78rem">{{ a.descripcion[:60] }}{% if a.descripcion|length > 60 %}…{% endif %}</div>
            {% endif %}
            <div class="mt-1" style="height:4px;background:#e2e8f0;border-radius:2px;width:120px">
              <div style="height:4px;background:{% if pct_amort > 80 %}#ef4444{% elif pct_amort > 50 %}#f59e0b{% else %}#10b981{% endif %};border-radius:2px;width:{{ [pct_amort,100]|min }}%"></div>
            </div>
          </td>
          <td class="font-monospace" style="font-size:.82rem;color:#fbbf24">
            {{ a.cuenta_codigo or '—' }}
          </td>
          <td class="text-muted" style="font-size:.82rem">{{ a.fecha_adquisicion }}</td>
          <td class="text-end font-monospace" style="font-size:.85rem">{{ "%.2f €"|format(a.coste_adquisicion) }}</td>
          <td class="text-end font-monospace text-danger" style="font-size:.85rem">{{ "%.2f €"|format(a.amort_acumulada or 0) }}</td>
          <td class="text-end font-monospace fw-bold" style="font-size:.85rem">
            <span class="{{ 'text-success' if vn > 0 else 'text-muted' }}">{{ "%.2f €"|format(vn) }}</span>
          </td>
          <td>
            <span class="badge" style="font-size:.65rem;background:#f1f5f9;color:#475569">
              {{ 'Lineal' if a.metodo_amortizacion == 'lineal' else 'Acelerado' }}
            </span>
          </td>
          <td>
            {% if a.activo %}
            <span class="badge-estado-pagada">Activo</span>
            {% else %}
            <span class="badge-estado-cancelada">Baja</span>
            {% endif %}
          </td>
          <td class="text-end">
            <a href="{{ url_for('activos.detalle', id=a.id) }}" class="btn btn-sm btn-outline-secondary btn-action me-1">
              <i class="bi bi-eye"></i>
            </a>
            <a href="{{ url_for('activos.editar', id=a.id) }}" class="btn btn-sm btn-outline-primary btn-action me-1">
              <i class="bi bi-pencil"></i>
            </a>
            <button class="btn btn-sm btn-outline-danger btn-action"
                    data-bs-toggle="modal" data-bs-target="#modalDelActivo{{ a.id }}"
                    title="Eliminar activo">
              <i class="bi bi-trash"></i>
            </button>
            <div class="modal fade" id="modalDelActivo{{ a.id }}" tabindex="-1">
              <div class="modal-dialog modal-dialog-centered modal-sm">
                <div class="modal-content">
                  <div class="modal-header">
                    <h6 class="modal-title">¿Eliminar activo?</h6>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                  </div>
                  <div class="modal-body text-center">
                    <div class="fw-bold">{{ a.nombre }}</div>
                    <div class="text-muted small">{{ "%.2f €"|format(a.coste_adquisicion) }}</div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Cancelar</button>
                    <form method="post" action="{{ url_for('activos.eliminar', id=a.id) }}" class="d-inline">
                      <button type="submit" class="btn btn-danger btn-sm">Eliminar</button>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
</div>
{% else %}
<div class="card">
  <div class="card-body text-center py-5 text-muted">
    <i class="bi bi-buildings" style="font-size:2.5rem;opacity:.25"></i>
    <h6 class="mt-3 mb-1">Sin activos registrados</h6>
    <p class="mb-3" style="font-size:.85rem">Registra tu inmovilizado material e intangible aquí.</p>
    <a href="{{ url_for('activos.nuevo') }}" class="btn btn-primary btn-sm">
      <i class="bi bi-plus-lg me-1"></i>Primer activo
    </a>
  </div>
</div>
{% endif %}
{% endblock %}
