{% if current_user.is_authenticated and current_user.usergroup.id == 1 %}
    {% extends "backend/template.html" %}
{% elif current_user.is_authenticated %}
    {% extends "backend/custom/surround.html" %}
{%endif%}

{% block content %}
{% set total = ""|general_stat %}

<div class="container-fluid py-5">
    <h1 class="fw-light mb-4">Panel de Control de Colecciones</h1>

    <div class="row mb-5">
        <div class="col-12">
            <h4 class="mb-3 text-secondary">Acciones Clave</h4>
            <div class="row g-3">
                
                <div class="col-sm-6 col-md-3">
                    <a href="{{ url_for('routing.create_record', classid=1) }}" class="card card-action p-4 border-0 shadow-sm text-decoration-none h-100 bg-secondary text-white">
                        <div class="d-flex align-items-center">
                            <i class="material-icons fs-2 me-3">add_box</i>
                            <div>
                                <h5 class="mb-0 fw-bold">Añadir Nuevo Objeto</h5>
                                <small class="opacity-75">Comenzar un registro nuevo.</small>
                            </div>
                        </div>
                    </a>
                </div>

                <div class="col-sm-6 col-md-3">
                    <a href="{{ url_for('routing.list_record', classid=1, page=1) }}" class="card card-action p-4 border-1 shadow-sm text-decoration-none h-100 text-dark">
                        <div class="d-flex align-items-center">
                            <i class="material-icons fs-2 me-3 text-primary">search</i>
                            <div>
                                <h5 class="mb-0 fw-bold">Buscar Objeto</h5>
                                <small class="text-muted">Encontrar un registro existente.</small>
                            </div>
                        </div>
                    </a>
                </div>

                <div class="col-sm-6 col-md-3">
                    <a href="{{ url_for('routing.list_record', classid=14, page=1) }}" class="card card-action p-4 border-1 shadow-sm text-decoration-none h-100 text-dark">
                        <div class="d-flex align-items-center">
                            <i class="material-icons fs-2 me-3 text-success">people_alt</i>
                            <div>
                                <h5 class="mb-0 fw-bold">Consultar Autores</h5>
                                <small class="text-muted">Gestionar lista de creadores.</small>
                            </div>
                        </div>
                    </a>
                </div>

                <div class="col-sm-6 col-md-3">
                    <a href="{{ url_for('routing.list_record', classid=13, page=1) }}" class="card card-action p-4 border-1 shadow-sm text-decoration-none h-100 text-dark">
                        <div class="d-flex align-items-center">
                            <i class="material-icons fs-2 me-3 text-info">view_list</i>
                            <div>
                                <h5 class="mb-0 fw-bold">Ver Colecciones</h5>
                                <small class="text-muted">Explorar grupos y fondos.</small>
                            </div>
                        </div>
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        
        <div class="col-lg-6">
            <h4 class="mb-3 text-secondary">Métricas de la Colección</h4>
            <div class="row">
                
                <div class="col-sm-4 col-lg-4 mb-4">
                    <div class="card p-3 border-start border-1 shadow-sm">
                        <div class="card-body p-0 d-flex flex-column justify-content-end">
                            <p class="text-muted text-uppercase mb-1 text-center small">Total Objetos</p>
                            <h3 class="fw-bold text-secondary mb-0 text-center">{{total["objects"]}}</h3>
                        </div>
                    </div>
                </div>
                
                <div class="col-sm-4 col-lg-4 mb-4">
                    <div class="card p-3 border-start border-1 shadow-sm">
                        <div class="card-body p-0 d-flex flex-column justify-content-end">
                            <p class="text-muted text-uppercase mb-1 text-center small">Autores Únicos</p>
                            <h3 class="fw-bold text-secondary mb-0 text-center">{{total["creators"]}}</h3>
                        </div>
                    </div>
                </div>

                <div class="col-sm-4 col-lg-4 mb-4">
                    <div class="card p-3 border-start border-1 shadow-sm">
                        <div class="card-body p-0 d-flex flex-column justify-content-end">
                            <p class="text-muted text-uppercase mb-1 text-center small">Colecciones</p>
                            <h3 class="fw-bold text-secondary mb-0 text-center">{{total["collections"]}}</h3>
                        </div>
                    </div>
                </div>
                
            </div>
        </div>

        <div class="col-lg-6">
            <h4 class="mb-3 text-secondary">Actividad Reciente</h4>
            <div class="card shadow-sm">
                <div class="card-header bg-white fw-bold">
                    Últimos 5 Objetos Modificados
                </div>
                <ul class="list-group list-group-flush small">
                    {%if total.get("lastObjects")%}
                    {%for object in total.get("lastObjects")%}
                    <a href="{{ url_for('routing.view_record', classid=1, record_id=object.id) }}" style="text-decoration: none;">
                    <li class="list-group-item d-flex align-items-center">
                        <i class="material-icons me-3 text-muted">history</i>
                        <div>
                            <p class="mb-0 fw-medium">{{object.accessionNumber}} - {{object.__repr__()}}</p>
                            <span class="text-muted">Modificado: {{ object.updated_at.strftime("%d/%m/%Y - %H:%M") }}</span>
                        </div>
                    </li>
                    </a>
                    {%endfor%}
                    {%endif%}
                </ul>
            </div>
        </div>
    </div>
</div>
{%endblock%}