<?php $__env->startSection('content'); ?>
<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-2">
                    <i class="fa fa-map-marker-alt text-primary me-2"></i>Gestión de Distritos
                </h1>
                <h2 class="fs-base lh-base fw-medium text-muted mb-0">
                    Administre los distritos y sus urbanizaciones asignadas.
                </h2>
            </div>
            <?php if(auth()->user()->tipo == 1): ?>
            <div class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3">
                <a href="<?php echo e(route('distritos.create')); ?>" class="btn btn-alt-primary">
                    <i class="fa fa-plus-circle me-1"></i> Nuevo Distrito
                </a>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<div class="content">
    
    <?php if(session('success')): ?>
        <div class="alert alert-success d-flex align-items-center justify-content-between" role="alert">
            <p class="mb-0">
                <i class="fa fa-check-circle me-1"></i> <?php echo e(session('success')); ?>

            </p>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    
    <div class="row">
        <div class="col-6 col-md-4">
            <a class="block block-rounded block-link-pop text-center" href="javascript:void(0)">
                <div class="block-content block-content-full">
                    <div class="item item-circle bg-primary-light mx-auto mb-3">
                        <i class="fa fa-database text-primary"></i>
                    </div>
                    <div class="fs-2 fw-bold"><?php echo e($distritos->total()); ?></div>
                    <div class="fs-sm fw-semibold text-uppercase text-muted">Total Registros</div>
                </div>
            </a>
        </div>
        <div class="col-6 col-md-4">
            <a class="block block-rounded block-link-pop text-center" href="javascript:void(0)">
                <div class="block-content block-content-full">
                    <div class="item item-circle bg-info-light mx-auto mb-3">
                        <i class="fa fa-layer-group text-info"></i>
                    </div>
                    <div class="fs-2 fw-bold text-dark"><?php echo e($distritos->count()); ?></div>
                    <div class="fs-sm fw-semibold text-uppercase text-muted">En esta página</div>
                </div>
            </a>
        </div>
        <div class="col-md-4 d-none d-md-block">
            <a class="block block-rounded block-link-pop text-center" href="javascript:void(0)">
                <div class="block-content block-content-full">
                    <div class="item item-circle bg-warning-light mx-auto mb-3">
                        <i class="fa fa-file-alt text-warning"></i>
                    </div>
                    <div class="fs-2 fw-bold text-dark"><?php echo e($distritos->currentPage()); ?></div>
                    <div class="fs-sm fw-semibold text-uppercase text-muted">Página Actual</div>
                </div>
            </a>
        </div>
    </div>

    <div class="block block-rounded">
        <div class="block-content block-content-full">
            <form action="<?php echo e(route('distritos.index')); ?>" method="GET" id="searchForm">
                <div class="input-group">
                    <span class="input-group-text bg-transparent border-0">
                        <i class="fa fa-search text-muted"></i>
                    </span>
                    <input type="text" class="form-control form-control-alt" 
                           name="buscar" id="searchInput" 
                           placeholder="Escriba nombre o código del distrito..." 
                           value="<?php echo e($buscar); ?>" autocomplete="off">
                    <?php if($buscar): ?>
                        <a href="<?php echo e(route('distritos.index')); ?>" class="btn btn-alt-danger">
                            <i class="fa fa-times me-1"></i> Limpiar
                        </a>
                    <?php endif; ?>
                    <button type="submit" class="btn btn-alt-primary">Buscar</button>
                </div>
                <div id="searchHint" class="form-text fs-xs text-muted mt-2">
                    <i class="fa fa-info-circle me-1"></i> Ingrese al menos 2 caracteres para filtrar.
                </div>
            </form>
        </div>
    </div>

    <div class="block block-rounded">
        <div class="block-header block-header-default">
            <h3 class="block-title">Resultados</h3>
            <div class="block-options">
                <button type="button" class="btn-block-option" data-toggle="block-option" data-action="state_toggle" data-action-mode="demo">
                    <i class="si si-refresh"></i>
                </button>
            </div>
        </div>
        <div class="block-content">
            <div class="table-responsive">
                <table class="table table-striped table-hover table-vcenter fs-sm">
                    <thead>
                        <tr>
                            <th class="text-center" style="width: 50px;">#</th>
                            <th style="width: 15%;">Código</th>
                            <th>Nombre del Distrito</th>
                            <th class="d-none d-sm-table-cell" style="width: 30%;">Urbanización</th>
                            <th class="text-center" style="width: 100px;">Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $distritos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr>
                            <td class="text-center"><?php echo e($distritos->firstItem() + $i); ?></td>
                            <td>
                                <span class="badge bg-secondary-light text-secondary px-2 py-2">
                                    <i class="fa fa-hashtag me-1 small"></i><?php echo e($d->id); ?>

                                </span>
                            </td>
                            <td class="fw-semibold">
                                <a href="<?php echo e(route('distritos.show', $d->id )); ?>" class="text-dark hover-primary">
                                    <?php echo e($d->nombre); ?>

                                </a>
                            </td>
                            <td class="d-none d-sm-table-cell">
                                <?php if($d->urbanizacion): ?>
                                    <span class="fs-xs fw-bold d-inline-block py-1 px-3 rounded-pill bg-info-light text-info text-uppercase">
                                        <i class="fa fa-building me-1"></i> <?php echo e($d->urbanizacion->nombre); ?>

                                    </span>
                                <?php else: ?>
                                    <span class="text-muted italic"><i class="fa fa-minus me-1"></i> Sin asignar</span>
                                <?php endif; ?>
                            </td>
                            <td class="text-center">
                                <div class="btn-group">
                                    <a href="<?php echo e(route('distritos.show', $d->id)); ?>" class="btn btn-sm btn-alt-secondary" data-bs-toggle="tooltip" title="Ver">
                                        <i class="fa fa-fw fa-eye"></i>
                                    </a>
                                    <?php if(auth()->user()->tipo == 1): ?>
                                        <a href="<?php echo e(route('distritos.edit', $d->id)); ?>" class="btn btn-sm btn-alt-secondary" data-bs-toggle="tooltip" title="Editar">
                                            <i class="fa fa-fw fa-pencil-alt text-warning"></i>
                                        </a>
                                        <button type="button" class="btn btn-sm btn-alt-secondary" onclick="confirmDelete(<?php echo e($d->id); ?>, '<?php echo e($d->nombre); ?>')" data-bs-toggle="tooltip" title="Eliminar">
                                            <i class="fa fa-fw fa-trash-alt text-danger"></i>
                                        </button>
                                        <form id="delete-form-<?php echo e($d->id); ?>" action="<?php echo e(route('distritos.destroy', $d->id)); ?>" method="POST" style="display: none;">
                                            <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
                                        </form>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="5" class="text-center py-5">
                                <i class="fa fa-map-marked-alt fa-3x text-muted mb-3"></i>
                                <p class="text-muted mb-0">No se encontraron registros para mostrar.</p>
                            </td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>

            
            <?php if($distritos->hasPages()): ?>
            <div class="d-flex flex-column flex-sm-row justify-content-between align-items-center mt-3 mb-4">
                <div class="fs-sm text-muted mb-2 mb-sm-0">
                    Mostrando <strong><?php echo e($distritos->firstItem()); ?></strong> a <strong><?php echo e($distritos->lastItem()); ?></strong> de <strong><?php echo e($distritos->total()); ?></strong>
                </div>
                <div>
                    <?php echo e($distritos->appends(['buscar' => $buscar])->links('pagination::bootstrap-5')); ?>

                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
    // Búsqueda con retardo integrada (Debounce)
    let searchTimer;
    const input = document.getElementById('searchInput');
    const hint = document.getElementById('searchHint');

    if(input) {
        input.addEventListener('keyup', () => {
            clearTimeout(searchTimer);
            if(input.value.length >= 2) {
                hint.innerHTML = '<i class="fa fa-spinner fa-spin me-1"></i> Buscando...';
                searchTimer = setTimeout(() => {
                    document.getElementById('searchForm').submit();
                }, 600);
            }
        });
    }

    // Confirmación SweetAlert2 (Estilo OneUI)
    function confirmDelete(id, nombre) {
        Swal.fire({
            title: '¿Confirmar borrado?',
            html: `Se eliminará permanentemente el distrito: <br><strong>${nombre}</strong>`,
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#6c757d',
            confirmButtonText: 'Sí, eliminar',
            cancelButtonText: 'Cancelar',
            buttonsStyling: true
        }).then((result) => {
            if (result.isConfirmed) {
                document.getElementById('delete-form-' + id).submit();
            }
        });
    }

    // Activar tooltips de OneUI automáticamente
    One.helpersOnLoad(['bs-tooltip']);
</script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /var/www/proyect-laravel/cosmos-app/resources/views/distritos/index.blade.php ENDPATH**/ ?>