<?php if (isset($component)) { $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54 = $attributes; } ?>
<?php $component = App\View\Components\AppLayout::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('app-layout'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\App\View\Components\AppLayout::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
     <?php $__env->slot('header', null, []); ?> 
        Editar Usuario
     <?php $__env->endSlot(); ?>

    <div class="bg-white shadow-lg rounded-sm border border-slate-200">
        <header class="px-5 py-4 border-b border-slate-100">
            <h2 class="font-semibold text-slate-800">Modificar: <?php echo e($user->name); ?> <?php echo e($user->apellido); ?></h2>
        </header>

        <div class="p-5">
            <form method="POST" action="<?php echo e(route('admin.users.update', $user->id)); ?>">
                <?php echo csrf_field(); ?>
                <?php echo method_field('PUT'); ?>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <!-- Cédula + Botón búsqueda RRHH -->
                    <div>
                        <label class="block text-sm font-medium mb-1" for="cedula">Cédula <span
                                class="text-rose-500">*</span></label>
                        <div class="flex gap-2">
                            <input id="cedula" name="cedula" class="form-input w-full" type="text"
                                value="<?php echo e(old('cedula', $user->cedula)); ?>" required placeholder="Ingrese cédula..." />
                            <button type="button" id="btn-buscar-cedula"
                                class="btn flex items-center gap-1 whitespace-nowrap text-sm font-medium transition rounded-md shadow-sm"
                                style="background-color: #0C782BFF; color: #ffffff; padding: 0.5rem 0.75rem; border: none; cursor: pointer;"
                                onmouseover="this.style.backgroundColor='#139937ff'"
                                onmouseout="this.style.backgroundColor='#0c782bff'" title="Buscar en TH">
                                <svg id="icon-buscar" xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none"
                                    viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                        d="M21 21l-4.35-4.35M17 11A6 6 0 1 1 5 11a6 6 0 0 1 12 0z" />
                                </svg>
                                <svg id="icon-spinner" xmlns="http://www.w3.org/2000/svg"
                                    class="h-4 w-4 animate-spin hidden" fill="none" viewBox="0 0 24 24">
                                    <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor"
                                        stroke-width="4" />
                                    <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v8z" />
                                </svg>
                                Buscar
                            </button>
                        </div>
                        <div id="msg-busqueda" class="text-xs mt-1 hidden"></div>
                        <?php $__errorArgs = ['cedula'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    <!-- Username -->
                    <div>
                        <label class="block text-sm font-medium mb-1" for="username">Nombre de Usuario (Para Login)<span
                                class="text-rose-500">*</span></label>
                        <input id="username" name="username" class="form-input w-full" type="text"
                            value="<?php echo e(old('username', $user->username)); ?>" required />
                        <?php $__errorArgs = ['username'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    <!-- Email -->
                    <div>
                        <label class="block text-sm font-medium mb-1" for="email">Correo Electrónico <span
                                class="text-xs text-slate-500 font-normal">(Opcional)</span></label>
                        <input id="email" name="email" class="form-input w-full" type="email"
                            value="<?php echo e(old('email', $user->email)); ?>" />
                        <?php $__errorArgs = ['email'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    <!-- Nombres -->
                    <div>
                        <label class="block text-sm font-medium mb-1" for="name">Nombres <span
                                class="text-rose-500">*</span></label>
                        <input id="name" name="name" class="form-input w-full" type="text"
                            value="<?php echo e(old('name', $user->name)); ?>" required />
                        <?php $__errorArgs = ['name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    <!-- Apellidos -->
                    <div>
                        <label class="block text-sm font-medium mb-1" for="apellido">Apellidos</label>
                        <input id="apellido" name="apellido" class="form-input w-full" type="text"
                            value="<?php echo e(old('apellido', $user->apellido)); ?>" />
                        <?php $__errorArgs = ['apellido'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    <!-- Cargo/Rol -->
                    <div>
                        <label class="block text-sm font-medium mb-1" for="cargo">Cargo / Rol en Sistema <span
                                class="text-rose-500">*</span></label>
                        <select id="cargo" name="cargo" class="form-select w-full" required>
                            <option value="">Seleccione Cargo</option>
                            <?php $__currentLoopData = $cargos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cargo): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($cargo); ?>" <?php echo e(old('cargo', $user->cargo) == $cargo ? 'selected' : ''); ?>>
                                    <?php echo e($cargo); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <?php $__errorArgs = ['cargo'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    <!-- Departamento (Opcional) -->
                    <div id="campo_departamento" class="md:col-span-1 block">
                        <label class="block text-sm font-medium mb-1" for="departamento_id">Departamento <span
                                class="text-xs text-slate-500">(Opcional)</span></label>
                        <select id="departamento_id" name="departamento_id" class="form-select w-full">
                            <option value="">No aplica / Seleccione Departamento</option>
                            <?php $__currentLoopData = $departamentos; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $departamento): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($departamento->id); ?>" <?php echo e(old('departamento_id', $user->departamento_id) == $departamento->id ? 'selected' : ''); ?>>
                                    <?php echo e($departamento->nombre); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <?php $__errorArgs = ['departamento_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    <!-- Gerencia -->
                    <div id="campo_gerencia" class="md:col-span-1 block">
                        <label class="block text-sm font-medium mb-1" for="gerencia_id">Gerencia <span
                                class="text-xs text-slate-500">(Obligatoria si no elige Depto)</span></label>
                        <select id="gerencia_id" name="gerencia_id" class="form-select w-full">
                            <option value="">Seleccione Gerencia</option>
                            <?php $__currentLoopData = $gerencias; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $gerencia): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($gerencia->id); ?>" <?php echo e(old('gerencia_id', $user->gerencia_id) == $gerencia->id ? 'selected' : ''); ?>>
                                    <?php echo e($gerencia->nombre); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <?php $__errorArgs = ['gerencia_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    <!-- Área de Trabajo -->
                    <div class="md:col-span-2">
                        <label class="block text-sm font-medium mb-1" for="area_trabajo_id">Área de Trabajo en la que
                            opera físicamente</label>
                        <select id="area_trabajo_id" name="area_trabajo_id" class="form-select w-full">
                            <option value="">Seleccione Área de Trabajo</option>
                            <?php $__currentLoopData = $areasTrabajo; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $area): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($area->id); ?>" <?php echo e(old('area_trabajo_id', $user->area_trabajo_id) == $area->id ? 'selected' : ''); ?>>
                                    <?php echo e($area->nombre); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <?php $__errorArgs = ['area_trabajo_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                </div>



                <!-- Nota informativa -->
                <div class="mt-3 p-3 bg-blue-50 border border-blue-200 rounded-lg text-xs text-blue-700">
                    <strong>Nota:</strong> El Departamento es opcional. Sin embargo, si el usuario no tiene
                    Departamento, es un requisito obligatorio asignarle una Gerencia en su lugar.
                </div>

                <div class="mt-6 border-t border-slate-100 pt-4">
                    <h3 class="text-sm font-medium text-slate-800 mb-3">Seguridad (Dejar en blanco para mantener la
                        contraseña actual)</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <!-- Contraseña -->
                        <div>
                            <label class="block text-sm font-medium mb-1" for="password">Nueva Contraseña</label>
                            <input id="password" name="password" class="form-input w-full" type="password" />
                            <?php $__errorArgs = ['password'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> <div class="text-xs mt-1 text-rose-500"><?php echo e($message); ?></div> <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <!-- Confirmar Contraseña -->
                        <div>
                            <label class="block text-sm font-medium mb-1" for="password_confirmation">Confirmar
                                Contraseña</label>
                            <input id="password_confirmation" name="password_confirmation" class="form-input w-full"
                                type="password" />
                        </div>
                    </div>
                </div>

                <div class="flex items-center justify-end mt-6 pt-4 border-t border-slate-100">
                    <a href="<?php echo e(route('admin.users.index')); ?>"
                        class="btn border-slate-200 hover:border-slate-300 text-slate-600 mr-3">Cancelar</a>
                    <button type="submit"
                        class="btn bg-green-600 hover:bg-green-700 text-white px-4 py-2 rounded-md shadow-sm font-medium transition">
                        Actualizar
                    </button>
                </div>
            </form>
        </div>
    </div>

    <script>
        document.getElementById('btn-buscar-cedula').addEventListener('click', function () {
            const cedula = document.getElementById('cedula').value.trim();
            const msg = document.getElementById('msg-busqueda');
            const spinner = document.getElementById('icon-spinner');
            const iconBuscar = document.getElementById('icon-buscar');

            if (!cedula) {
                msg.textContent = 'Ingrese una cédula antes de buscar.';
                msg.className = 'text-xs mt-1 text-amber-600';
                msg.classList.remove('hidden');
                return;
            }

            spinner.classList.remove('hidden');
            iconBuscar.classList.add('hidden');
            msg.classList.add('hidden');

            fetch(`<?php echo e(route('admin.users.buscar-cedula')); ?>?cedula=${encodeURIComponent(cedula)}`, {
                headers: { 'X-Requested-With': 'XMLHttpRequest', 'Accept': 'application/json' }
            })
                .then(r => r.json())
                .then(data => {
                    spinner.classList.add('hidden');
                    iconBuscar.classList.remove('hidden');

                    if (!data.encontrado) {
                        msg.textContent = data.mensaje;
                        msg.className = 'text-xs mt-1 text-rose-600';
                        msg.classList.remove('hidden');
                        return;
                    }

                    // Rellenar campos de texto
                    document.getElementById('name').value = data.nomper ?? '';
                    document.getElementById('apellido').value = data.apeper ?? '';

                    // Seleccionar Gerencia
                    const selGerencia = document.getElementById('gerencia_id');
                    if (selGerencia && data.gerencia_id) {
                        selGerencia.value = data.gerencia_id;
                    }

                    // Seleccionar Área de Trabajo
                    const selArea = document.getElementById('area_trabajo_id');
                    if (selArea && data.area_trabajo_id) {
                        selArea.value = data.area_trabajo_id;
                    }

                    // Mensaje de éxito con info de fallback
                    let infoExtra = '';
                    if (!data.gerencia_id && data.denestorg) infoExtra += ` | Gerencia no encontrada (${data.denestorg})`;
                    if (!data.area_trabajo_id && data.denubifis) infoExtra += ` | Área no encontrada (${data.denubifis})`;
                    msg.textContent = '✓ Datos cargados desde TH.' + infoExtra;
                    msg.className = 'text-xs mt-1 ' + (infoExtra ? 'text-amber-600' : 'text-emerald-600');
                    msg.classList.remove('hidden');
                })
                .catch(() => {
                    spinner.classList.add('hidden');
                    iconBuscar.classList.remove('hidden');
                    msg.textContent = 'Error de conexión al intentar buscar.';
                    msg.className = 'text-xs mt-1 text-rose-600';
                    msg.classList.remove('hidden');
                });
        });
    </script>

 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $attributes = $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $component = $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?><?php /**PATH C:\xampp82\htdocs\siser\siser_v2\resources\views/admin/users/edit.blade.php ENDPATH**/ ?>