@extends('layouts.app')

@section('content')
<!-- Hero -->
<div class="bg-body-light">
    <div class="content content-full">
        <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center py-2">
            <div class="flex-grow-1">
                <h1 class="h3 fw-bold mb-1">
                    Urbanizaciones
                </h1>
                <h2 class="fs-base fw-medium text-muted mb-0">
                    Gestión y administración de urbanizaciones registradas
                </h2>
            </div>
            @if(auth()->user()->tipo == 1)
                <nav class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3" aria-label="breadcrumb">
                    <a href="{{ route('urbanizaciones.create') }}" class="btn btn-primary">
                        <i class="fa fa-plus me-1"></i> Nueva urbanización
                    </a>
                </nav>
            @endif
        </div>
    </div>
</div>
<!-- END Hero -->

<!-- Page Content -->
<div class="content">
    {{-- Mensajes --}}
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="fa fa-check-circle me-2"></i> {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="fa fa-exclamation-triangle me-2"></i> {{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    <!-- Stats -->
    <div class="row">
        <div class="col-md-6 col-xl-3">
            <div class="block block-rounded">
                <div class="block-content block-content-full d-flex justify-content-between align-items-center">
                    <div>
                        <div class="fs-3 fw-bold">{{ $urbanizaciones->total() }}</div>
                        <div class="fs-sm fw-semibold text-uppercase text-muted">Total Urbanizaciones</div>
                    </div>
                    <div class="item item-rounded-lg bg-body-light">
                        <i class="fa fa-building fs-3 text-primary"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-xl-3">
            <div class="block block-rounded">
                <div class="block-content block-content-full d-flex justify-content-between align-items-center">
                    <div>
                        <div class="fs-3 fw-bold">{{ $urbanizaciones->count() }}</div>
                        <div class="fs-sm fw-semibold text-uppercase text-muted">En esta página</div>
                    </div>
                    <div class="item item-rounded-lg bg-body-light">
                        <i class="fa fa-table-list fs-3 text-primary"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-xl-3">
            <div class="block block-rounded">
                <div class="block-content block-content-full d-flex justify-content-between align-items-center">
                    <div>
                        <div class="fs-3 fw-bold">{{ $urbanizaciones->firstItem() ?? 0 }}</div>
                        <div class="fs-sm fw-semibold text-uppercase text-muted">Primer registro</div>
                    </div>
                    <div class="item item-rounded-lg bg-body-light">
                        <i class="fa fa-arrow-up fs-3 text-primary"></i>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-6 col-xl-3">
            <div class="block block-rounded">
                <div class="block-content block-content-full d-flex justify-content-between align-items-center">
                    <div>
                        <div class="fs-3 fw-bold">{{ $urbanizaciones->lastItem() ?? 0 }}</div>
                        <div class="fs-sm fw-semibold text-uppercase text-muted">Último registro</div>
                    </div>
                    <div class="item item-rounded-lg bg-body-light">
                        <i class="fa fa-arrow-down fs-3 text-primary"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- END Stats -->

    <!-- Search -->
    <div class="block block-rounded">
        <div class="block-header block-header-default">
            <h3 class="block-title">
                <i class="fa fa-search me-1 text-muted"></i> Buscar urbanizaciones
            </h3>
            <div class="block-options">
                <button type="button" class="btn-block-option" data-toggle="block-option" data-action="content_toggle"></button>
            </div>
        </div>
        <div class="block-content">
            <form method="GET" action="{{ route('urbanizaciones.index') }}" class="row g-3">
                <div class="col-md-8">
                    <div class="input-group">
                        <span class="input-group-text border-0 bg-body">
                            <i class="fa fa-search"></i>
                        </span>
                        <input type="text" 
                               class="form-control" 
                               name="urb" 
                               placeholder="Buscar por nombre o código..." 
                               value="{{ $busqueda }}">
                        @if($busqueda)
                            <a href="{{ route('urbanizaciones.index') }}" class="btn btn-alt-danger">
                                <i class="fa fa-times me-1"></i> Limpiar
                            </a>
                        @endif
                    </div>
                </div>
                <div class="col-md-4">
                    <button class="btn btn-primary w-100" type="submit">
                        <i class="fa fa-search me-1"></i> Buscar
                    </button>
                </div>
            </form>
        </div>
    </div>
    <!-- END Search -->

    <!-- Urbanizaciones Table -->
    <div class="block block-rounded">
        <div class="block-header block-header-default">
            <h3 class="block-title">
                <i class="fa fa-list me-1 text-muted"></i> Listado de urbanizaciones
            </h3>
            <div class="block-options">
                <span class="badge bg-primary">{{ $urbanizaciones->total() }} registros</span>
                <button type="button" class="btn-block-option" data-toggle="block-option" data-action="content_toggle"></button>
            </div>
        </div>
        <div class="block-content block-content-full overflow-hidden">
            <div class="table-responsive">
                <table class="table table-hover table-vcenter">
                    <thead>
                        <tr>
                            <th class="text-center" style="width: 80px;">Código</th>
                            <th>Nombre</th>
                            <th style="width: 120px;">Fecha</th>
                            <th>Observaciones</th>
                            <th class="text-center" style="width: 120px;">Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($urbanizaciones as $urb)
                            <tr>
                                <td class="text-center fs-sm fw-semibold text-primary">
                                    {{ $urb->id }}
                                </td>
                                <td>
                                    <a href="{{ route('urbanizaciones.show', $urb->id) }}" class="fw-semibold">
                                        <i class="fa fa-building me-1 text-muted"></i>
                                        {{ $urb->nombre }}
                                    </a>
                                </td>
                                <td>
                                    @if($urb->fechaurb)
                                        <span class="fs-sm">
                                            <i class="fa fa-calendar-alt me-1 text-muted"></i>
                                            {{ \Carbon\Carbon::parse($urb->fechaurb)->format('d/m/Y') }}
                                        </span>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                                <td>
                                    <span class="fs-sm text-muted">
                                        {{ \Illuminate\Support\Str::limit($urb->observacionesurb, 60) ?: '—' }}
                                    </span>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm" role="group">
                                        <a href="{{ route('urbanizaciones.show', $urb->id) }}" 
                                           class="btn btn-alt-info" 
                                           data-bs-toggle="tooltip" 
                                           title="Ver detalles">
                                            <i class="fa fa-eye"></i>
                                        </a>
                                        @if(auth()->user()->tipo == 1)
                                            <a href="{{ route('urbanizaciones.edit', $urb->id) }}" 
                                               class="btn btn-alt-warning"
                                               data-bs-toggle="tooltip" 
                                               title="Editar">
                                                <i class="fa fa-pencil-alt"></i>
                                            </a>
                                            <button type="button" 
                                                    class="btn btn-alt-danger" 
                                                    data-bs-toggle="modal" 
                                                    data-bs-target="#deleteModal{{ $urb->id }}"
                                                    data-bs-toggle="tooltip" 
                                                    title="Eliminar">
                                                <i class="fa fa-trash-alt"></i>
                                            </button>
                                        @endif
                                    </div>

                                    {{-- Modal de confirmación --}}
                                    @if(auth()->user()->tipo == 1)
                                    <div class="modal fade" id="deleteModal{{ $urb->id }}" tabindex="-1" aria-hidden="true">
                                        <div class="modal-dialog modal-dialog-centered">
                                            <div class="modal-content">
                                                <div class="modal-header bg-danger text-white">
                                                    <h5 class="modal-title">
                                                        <i class="fa fa-exclamation-triangle me-2"></i> Confirmar eliminación
                                                    </h5>
                                                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <p class="mb-2">¿Estás seguro de que deseas eliminar la urbanización?</p>
                                                    <p class="fw-bold text-danger mb-2">{{ $urb->nombre }}</p>
                                                    <p class="fs-sm text-muted mb-0">Esta acción no se puede deshacer.</p>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-alt-secondary" data-bs-dismiss="modal">
                                                        <i class="fa fa-times me-1"></i> Cancelar
                                                    </button>
                                                    <form action="{{ route('urbanizaciones.destroy', $urb->id) }}" method="POST" class="d-inline">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button type="submit" class="btn btn-danger">
                                                            <i class="fa fa-trash-alt me-1"></i> Sí, eliminar
                                                        </button>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5" class="text-center py-5">
                                    <i class="fa fa-building-circle-exclamation fa-3x text-muted mb-3"></i>
                                    <p class="text-muted mb-2">No hay urbanizaciones que coincidan con la búsqueda</p>
                                    @if($busqueda)
                                        <a href="{{ route('urbanizaciones.index') }}" class="btn btn-sm btn-alt-primary mt-2">
                                            <i class="fa fa-undo me-1"></i> Limpiar búsqueda
                                        </a>
                                    @endif
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        
        {{-- Paginación --}}
        @if($urbanizaciones->hasPages())
            <div class="block-content block-content-full bg-body-light">
                <div class="d-flex justify-content-between align-items-center flex-wrap">
                    <div class="fs-sm text-muted mb-2 mb-sm-0">
                        Mostrando {{ $urbanizaciones->firstItem() ?? 0 }} - {{ $urbanizaciones->lastItem() ?? 0 }} 
                        de {{ $urbanizaciones->total() }} registros
                    </div>
                    <div>
                        {{ $urbanizaciones->appends(request()->query())->links() }}
                    </div>
                </div>
            </div>
        @endif
    </div>
    <!-- END Urbanizaciones Table -->
</div>
<!-- END Page Content -->
@endsection

@push('scripts')
<script>
    // Inicializar tooltips
    document.addEventListener('DOMContentLoaded', function() {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
        var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl)
        });
    });
</script>
@endpush