<?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-folder-open text-primary me-2"></i>Gestión de Trámites
                </h1>
                <h2 class="fs-base lh-base fw-medium text-muted mb-0">
                    Búsqueda y actualización de estados documentales por cliente y ubicación.
                </h2>
            </div>
        </div>
    </div>
</div>

<div class="content">
    
    <?php if(session('success')): ?>
        <div class="alert alert-success d-flex align-items-center mb-4" role="alert">
            <i class="fa fa-check-circle me-2"></i>
            <p class="mb-0"><?php echo e(session('success')); ?></p>
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    
    <div class="block block-rounded block-bordered shadow-sm">
        <div class="block-content block-content-full bg-body-light">
            <form method="GET" action="<?php echo e(route('tramites.index')); ?>" id="formBusqueda">
                <div class="row g-3 align-items-end">
                    
                    <div class="col-md-3">
                        <label class="form-label fs-xs text-uppercase fw-bold text-muted">Urbanización</label>
                        <select name="urb" id="selUrb" class="form-select form-select-alt fs-sm">
                            <option value="0">--- Todas ---</option>
                            <?php $__currentLoopData = $urbanizaciones; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $u): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($u->id); ?>" <?php echo e($urb == $u->id ? 'selected' : ''); ?>>
                                    <?php echo e($u->nombre); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>

                    
                    <div class="col-md-2">
                        <label class="form-label fs-xs text-uppercase fw-bold text-muted">Distrito</label>
                        <select name="dis" id="selDis" class="form-select form-select-alt fs-sm">
                            <option value="0">--- Todos ---</option>
                            <?php $__currentLoopData = $distritos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($d->id); ?>" <?php echo e($dis == $d->id ? 'selected' : ''); ?>>
                                    <?php echo e($d->nombre); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>

                    
                    <div class="col-md-2">
                        <label class="form-label fs-xs text-uppercase fw-bold text-muted">Manzano / Lote</label>
                        <div class="input-group">
                            <select name="manzano" id="selMan" class="form-select form-select-alt fs-sm">
                                <option value="">Mz.</option>
                                <?php $__currentLoopData = $manzanos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $m): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($m); ?>" <?php echo e($manzano == $m ? 'selected' : ''); ?>><?php echo e($m); ?></option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <input type="text" class="form-control form-control-alt fs-sm" name="cliente" 
                                   value="<?php echo e($cliente); ?>" placeholder="ID Cliente" style="width: 40%">
                        </div>
                    </div>

                    
                    <div class="col-md-3 d-flex gap-2">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fa fa-search me-1"></i> Buscar
                        </button>
                        <a href="<?php echo e(route('tramites.index')); ?>" class="btn btn-alt-secondary" title="Limpiar">
                            <i class="fa fa-sync-alt"></i>
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    
    <div class="block block-rounded shadow-sm overflow-hidden">
        <div class="block-header block-header-default">
            <h3 class="block-title">Resultados de Trámites</h3>
            <div class="block-options text-muted fs-xs">
                Total: <strong><?php echo e($tramites->total()); ?></strong> registros
            </div>
        </div>
        <div class="block-content p-0">
            <div class="table-responsive">
                <table class="table table-vcenter table-hover table-striped mb-0 fs-sm">
                    <thead class="bg-body-dark text-uppercase fs-xs">
                        <tr>
                            <th class="text-center" style="width: 80px;">N°</th>
                            <th>Cliente Titular</th>
                            <th>Urbanización</th>
                            <th>Distrito / Sector</th>
                            <th class="text-center" style="width: 80px;">Mz.</th>
                            <th class="text-center" style="width: 80px;">Lote</th>
                            <th class="text-center" style="width: 120px;">Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $__empty_1 = true; $__currentLoopData = $tramites; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tr): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr>
                            <td class="text-center fw-medium text-muted">#<?php echo e($tr->id); ?></td>
                            <td>
                                <div class="fw-bold text-primary"><?php echo e($tr->nom_cliente ?? 'Desconocido'); ?></div>
                                <div class="fs-xs text-muted">ID: <?php echo e($tr->idcliente ?? '—'); ?></div>
                            </td>
                            <td class="fw-medium"><?php echo e($tr->nom_urb); ?></td>
                            <td>
                                <span class="badge bg-body-dark text-dark border"><?php echo e($tr->nom_dis); ?></span>
                            </td>
                            <td class="text-center fw-bold"><?php echo e($tr->manzano); ?></td>
                            <td class="text-center fw-bold"><?php echo e($tr->lote); ?></td>
                            <td class="text-center">
                                <div class="btn-group">
                                    <a href="<?php echo e(route('tramites.show', $tr->id)); ?>"
                                       class="btn btn-sm btn-alt-info" data-bs-toggle="tooltip" title="Ver Detalle">
                                        <i class="fa fa-eye"></i>
                                    </a>
                                    <a href="<?php echo e(route('tramites.edit', $tr->id)); ?>"
                                       class="btn btn-sm btn-alt-warning" data-bs-toggle="tooltip" title="Editar">
                                        <i class="fa fa-pencil-alt"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="7" class="text-center py-5">
                                <i class="fa fa-folder-open fa-3x text-muted mb-3 opacity-25"></i>
                                <p class="text-muted mb-0">No se encontraron trámites para los criterios ingresados.</p>
                            </td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php if($tramites->hasPages()): ?>
            <div class="block-content block-content-full block-content-sm bg-body-light border-top text-center">
                <?php echo e($tramites->links()); ?>

            </div>
        <?php endif; ?>
    </div>
</div>

<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
// Selects en cascada optimizados
document.getElementById('selUrb').addEventListener('change', async function () {
    const idUrb = this.value;
    const selDis = document.getElementById('selDis');
    const selMan = document.getElementById('selMan');

    selDis.innerHTML = '<option value="0">--- Cargando... ---</option>';
    selMan.innerHTML = '<option value="">Mz.</option>';

    if (idUrb == 0) {
        selDis.innerHTML = '<option value="0">--- Todos ---</option>';
        return;
    }

    try {
        const res  = await fetch(`<?php echo e(url('tramites/distritos')); ?>/${idUrb}`);
        const data = await res.json();
        selDis.innerHTML = '<option value="0">--- Todos ---</option>';
        data.forEach(d => {
            selDis.innerHTML += `<option value="${d.id}">${d.nombre}</option>`;
        });
    } catch (e) {
        selDis.innerHTML = '<option value="0">--- Error ---</option>';
    }
});

document.getElementById('selDis').addEventListener('change', async function () {
    const idDis = this.value;
    const selMan = document.getElementById('selMan');
    selMan.innerHTML = '<option value="">...</option>';
    
    if (idDis == 0) {
        selMan.innerHTML = '<option value="">Mz.</option>';
        return;
    }

    try {
        const res  = await fetch(`<?php echo e(url('tramites/manzanos')); ?>/${idDis}`);
        const data = await res.json();
        selMan.innerHTML = '<option value="">Mz.</option>';
        data.forEach(m => {
            selMan.innerHTML += `<option value="${m}">${m}</option>`;
        });
    } catch (e) {
        selMan.innerHTML = '<option value="">Error</option>';
    }
});
</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/tramites/index.blade.php ENDPATH**/ ?>