<?php
    // $item viene desde tu index(): select('pc.*', 's.sede as sede_nombre')->first();
    $isEdit = !empty($item) && !empty($item->id);
    $valEstado = old('estado', $item->estado ?? 'AC');
    $isSuperAdmin = $isSuperAdmin ?? false;
?>

<style>
    .no-select{
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        pointer-events: none;
    }

    .bloque-imagenes{
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 30px;
        flex-wrap: wrap;
        width: 100%;
    }

    .bloque-imagen-izq{
        flex: 0 0 30%;
        max-width: 30%;
    }

    .bloque-imagen-der{
        flex: 0 0 45%;
        max-width: 45%;
        margin-left: auto;
        text-align: left;
    }

    .preview-box{
        border: 1px solid #dee2e6;
        border-radius: .375rem;
        background: #f8f9fa;
        min-height: 160px;
        padding: 10px;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .preview-box img{
        max-height: 220px;
        width: auto;
    }

    @media (max-width: 768px){
        .bloque-imagenes{
            flex-direction: column;
        }

        .bloque-imagen-izq,
        .bloque-imagen-der{
            flex: 0 0 100%;
            max-width: 100%;
            text-align: left;
            margin-left: 0;
        }
    }
</style>

<div>
    <div class="mb-3">
        <h4 class="mb-1">Parámetros de Comprobante</h4>
        <div class="text-muted small">Sede, serie, encabezado, dirección e imagen.</div>
    </div>

    <div class="card shadow-sm">
        <div class="card-body">

                <div class="row g-3">

                    
                    <div class="col-md-4">
                        <label class="form-label fw-semibold">Sede</label>
                        <?php if($isSuperAdmin): ?>
                            <input type="text"
                                   class="form-control bg-light"
                                   readonly
                                   tabindex="-1"
                                   value="<?php echo e(old('sede', $item->sede_nombre ?? '')); ?>">
                        <?php else: ?>
                            <input type="text"
                                   class="form-control bg-light no-select"
                                   readonly
                                   tabindex="-1"
                                   value="<?php echo e(old('sede', $item->sede_nombre ?? '')); ?>">
                        <?php endif; ?>
                    </div>

                    
                    <div class="col-md-4">
                        <label class="form-label fw-semibold">Serie</label>
                        <input type="text"
                               name="serie"
                               value="<?php echo e(old('serie', $item->serie ?? '')); ?>"
                               class="form-control <?php $__errorArgs = ['serie'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                               placeholder="Ej: F001">
                        <?php $__errorArgs = ['serie'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    
                    <div class="col-md-4">
                        <label class="form-label fw-semibold">Estado</label>
                        <input type="text"
                               class="form-control bg-light no-select"
                               value="ACTIVO"
                               readonly
                               tabindex="-1">
                        <input type="hidden" name="estado" value="AC">
                    </div>

                    
                    <div class="col-md-12">
                        <label class="form-label fw-semibold">Encabezado</label>
                        <textarea name="encabezado"
                                  rows="2"
                                  class="form-control <?php $__errorArgs = ['encabezado'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                  placeholder="Encabezado del comprobante..."><?php echo e(old('encabezado', $item->encabezado ?? '')); ?></textarea>
                        <?php $__errorArgs = ['encabezado'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    
                    <div class="col-md-12">
                        <label class="form-label fw-semibold">Dirección</label>
                        <textarea name="direccion"
                                  rows="2"
                                  class="form-control <?php $__errorArgs = ['direccion'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                  placeholder="Dirección..."><?php echo e(old('direccion', $item->direccion ?? '')); ?></textarea>
                        <?php $__errorArgs = ['direccion'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                            <div class="invalid-feedback"><?php echo e($message); ?></div>
                        <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                    </div>

                    
                    <div class="col-12 mt-3">
                        <div class="bloque-imagenes">

                            
                            <div class="bloque-imagen-izq">
                                <label class="form-label fw-semibold d-block text-start">Imagen actual</label>

                                <div class="preview-box">
                                    <?php if(!empty($item->imagen)): ?>
                                        <img id="previewActual"
                                             src="<?php echo e(asset('imagen_comprobante/'.$item->imagen)); ?>"
                                             alt="Imagen actual"
                                             class="img-fluid rounded d-block">
                                    <?php else: ?>
                                        <div class="text-muted small">Sin imagen registrada</div>
                                        <img id="previewActual"
                                             class="img-fluid rounded d-none">
                                    <?php endif; ?>
                                </div>

                                <?php if(!empty($item->imagen)): ?>
                                    <div class="small text-muted mt-2 text-start"><?php echo e($item->imagen); ?></div>
                                <?php endif; ?>
                            </div>

                            
                            <div class="bloque-imagen-der">
                                <label class="form-label fw-semibold d-block">Actualizar imagen</label>

                                <input type="file"
                                       id="imagen"
                                       name="imagen"
                                       accept="image/*"
                                       class="form-control <?php $__errorArgs = ['imagen'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>">

                                <div class="form-text">JPG/PNG/WEBP (opcional)</div>

                                <?php $__errorArgs = ['imagen'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                    <div class="invalid-feedback d-block"><?php echo e($message); ?></div>
                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>

                                <div class="mt-3">
                                    <label class="form-label fw-semibold d-block">Vista previa</label>

                                    <div class="preview-box">
                                        <img id="previewNueva"
                                             class="img-fluid rounded d-none d-block">
                                        <div id="previewTexto" class="text-muted small">
                                            Seleccione una imagen para previsualizar
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>

                </div>

                <div class="d-flex justify-content-end gap-2 mt-4">
                    <a href="<?php echo e(route('admin.comprobante.index', $isSuperAdmin ? ['id_sede' => $item->id_sede] : [])); ?>" class="btn btn-light">Cancelar</a>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save me-2"></i><?php echo e($isEdit ? 'Actualizar' : 'Guardar'); ?>

                    </button>
                </div>

        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', () => {
    const input = document.getElementById('imagen');
    const previewNueva = document.getElementById('previewNueva');
    const previewTexto = document.getElementById('previewTexto');

    if (!input) return;

    input.addEventListener('change', (e) => {
        const file = e.target.files && e.target.files[0];

        if (!file) {
            previewNueva.classList.add('d-none');
            previewTexto.classList.remove('d-none');
            previewNueva.src = '';
            return;
        }

        const url = URL.createObjectURL(file);
        previewNueva.src = url;
        previewNueva.classList.remove('d-none');
        previewTexto.classList.add('d-none');
    });
});
</script>
<?php /**PATH D:\laragon\www\seminario\resources\views/Parametro/comprobante/_form.blade.php ENDPATH**/ ?>