{% extends 'base.html' %}

{% block css %}
<style>
    #unidadesTable th, #unidadesTable td {
        vertical-align: middle;
        max-width: 150px;
        word-wrap: break-word;
        white-space: normal;
    }
    .card { border-radius: 15px; overflow: hidden; }
    .table-hover tbody tr:hover { background-color: rgba(13, 110, 253, 0.03) !important; }
    .dataTables_wrapper .dataTables_filter input {
        border: 1px solid #dee2e6; border-radius: 20px; padding: 0.375rem 1.75rem;
    }
    .dataTables_wrapper .dataTables_paginate .paginate_button {
        border-radius: 50% !important; margin: 0 3px; border: none !important;
    }
</style>
{% endblock %}

{% block titulo %}Unidades{% endblock %}

{% block vertical %} {% include 'partes/vertical.html' %} {% endblock %}
{% block horizontal %} {% include 'partes/horizontal.html' %} {% endblock %}
{% block pie %} {% include 'partes/pie.html' %} {% endblock %}

{% block seccion %}
<div class="d-flex align-items-left align-items-md-center flex-column flex-md-row pt-2 pb-5">
    <div>
        <h3 class="fw-bold mb-1">Unidades</h3>
        <h6 class="op-7 mb-0">Servicio de Transporte del Ejército Bolivariano</h6>
    </div>
    <div class="ms-md-auto py-1">
        <a href="{% url 'unidad_create' %}" class="btn btn-primary fw-bold rounded-3 ">Agregar Unidad</a>
    </div>
</div>
{% endblock %}

{% block contenido %}
<div class="card shadow-sm rounded-3 border-0">
    <div class="card-header bg-primary py-1 rounded-top">
        <h5 class="card-title mb-0 text-white fw-bold "><i class="fas fa-users-cog me-3"></i>Inventario de Unidades</h5>
    </div>
    <div class="card-body p-1">
        <div class="table-responsive">
            <table id="unidadesTable" class="table table-sm table-striped fs-6" style="width:100%">
                <thead style="background-color: #f8f9fa;">
                    <tr>
                        <th class="text-center py-0 px-1">N°</th>
                        <th class="py-0 px-1">Nombre de la Unidad</th>
                        <th class="text-center py-0 px-1">Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    {% for unidad in unidades %}
                    <tr>
                        <td class="text-center py-0 px-1 fw-bold">{{ forloop.counter }}</td>
                        <td class="py-0 px-1">{{ unidad.nombre|default:"-" }}</td>
                        <td class="text-center py-0 px-1">
                            <div class="d-flex justify-content-center gap-1">
                                <a href="{% url 'detalle' unidad.pk %}" 
                                   class="btn btn-info btn-xs"
                                   data-bs-toggle="tooltip"
                                   title="Ver detalle">
                                    <i class="far fa-eye fa-xs"></i>
                                </a>
                                <a href="{% url 'unidad_edit' unidad.pk %}" 
                                   class="btn btn-primary btn-xs"
                                   data-bs-toggle="tooltip"
                                   title="Editar">
                                    <i class="fas fa-pen fa-xs"></i>
                                </a>
                                <form action="{% url 'unidad_delete' unidad.pk %}" method="post" class="d-inline">
                                    {% csrf_token %}
                                    <button type="submit" 
                                            class="btn btn-danger btn-xs"
                                            data-bs-toggle="tooltip"
                                            title="Eliminar">
                                        <i class="fas fa-trash fa-xs"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="3" class="text-center py-0 px-1">No hay unidades registradas</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<!-- DataTables mínimo -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>

<script>
$(document).ready(function() {
    $('#unidadesTable').DataTable({
        "language": {
            "decimal": "",
            "emptyTable": "No hay datos disponibles",
            "info": "Mostrando _START_ a _END_ de _TOTAL_ registros",
            "infoEmpty": "Mostrando 0 a 0 de 0 registros",
            "infoFiltered": "(filtrado de _MAX_ registros totales)",
            "lengthMenu": "Mostrar _MENU_ registros",
            "loadingRecords": "Cargando...",
            "processing": "Procesando...",
            "search": "Buscar:",
            "zeroRecords": "No se encontraron registros",
            "paginate": {
                "first": "Primero",
                "last": "Último",
                "next": "Siguiente",
                "previous": "Anterior"
            }
        },
        "pageLength": 10,
        "searching": true,
        "paging": true,
        "ordering": false,
        "info": false,
        "lengthChange": false,
        "autoWidth": false
    });

    const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
    const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));
});
</script>
{% endblock %}