<?php $__env->startSection('title', 'Nueva Reserva'); ?>
<?php $__env->startSection('page-title', 'Nueva Reserva'); ?>
<?php $__env->startSection('breadcrumb'); ?>
    <li class="breadcrumb-item"><a href="<?php echo e(route('reservas.index')); ?>">Reservas</a></li>
    <li class="breadcrumb-item active">Nueva</li>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
<div class="row">
<div class="col-md-8">
<div class="card">
    <div class="card-header"><h3 class="card-title"><i class="fas fa-calendar-plus mr-2"></i>Crear Nueva Reserva</h3></div>
    <form action="<?php echo e(route('reservas.store')); ?>" method="POST" id="formReserva">
        <?php echo csrf_field(); ?>
        <div class="card-body">

            
            <div class="form-group">
                <label>Huésped <span class="text-danger">*</span></label>
                <select name="huesped_id" class="form-control select2 <?php $__errorArgs = ['huesped_id'];
$__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); ?>" required>
                    <option value="">Seleccionar huésped...</option>
                    <?php $__currentLoopData = $huespedes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $h): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($h->id); ?>"
                            <?php echo e((old('huesped_id', $huespedSeleccionado?->id) == $h->id) ? 'selected' : ''); ?>>
                            <?php echo e($h->apellido); ?>, <?php echo e($h->nombre); ?> (<?php echo e($h->tipo_documento); ?>: <?php echo e($h->num_documento); ?>)
                        </option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                <?php $__errorArgs = ['huesped_id'];
$__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); ?>
                <small class="text-muted">
                    ¿Huésped nuevo? <a href="<?php echo e(route('huespedes.create')); ?>" target="_blank">Registrar aquí</a>
                </small>
            </div>

            
            <div class="form-group">
                <label>Habitación <span class="text-danger">*</span></label>
                <select name="habitacion_id" id="habitacion_id" class="form-control select2 <?php $__errorArgs = ['habitacion_id'];
$__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); ?>" required>
                    <option value="">Seleccionar habitación...</option>
                    <?php $__currentLoopData = $habitaciones; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $hab): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($hab->id); ?>"
                            data-precio="<?php echo e($hab->tipoHabitacion->precio_base); ?>"
                            <?php echo e(old('habitacion_id') == $hab->id ? 'selected' : ''); ?>>
                            Hab. <?php echo e($hab->numero); ?> — <?php echo e($hab->tipoHabitacion->nombre); ?>

                            (S/ <?php echo e(number_format($hab->tipoHabitacion->precio_base, 2)); ?>/noche)
                            — <?php echo e(ucfirst($hab->estado)); ?>

                        </option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
                <?php $__errorArgs = ['habitacion_id'];
$__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="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Fecha de Entrada <span class="text-danger">*</span></label>
                        <input type="date" name="fecha_entrada" id="fecha_entrada"
                               class="form-control <?php $__errorArgs = ['fecha_entrada'];
$__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); ?>"
                               value="<?php echo e(old('fecha_entrada', now()->format('Y-m-d'))); ?>"
                               min="<?php echo e(now()->format('Y-m-d')); ?>" required>
                        <?php $__errorArgs = ['fecha_entrada'];
$__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>
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Fecha de Salida <span class="text-danger">*</span></label>
                        <input type="date" name="fecha_salida" id="fecha_salida"
                               class="form-control <?php $__errorArgs = ['fecha_salida'];
$__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); ?>"
                               value="<?php echo e(old('fecha_salida', now()->addDay()->format('Y-m-d'))); ?>"
                               min="<?php echo e(now()->addDay()->format('Y-m-d')); ?>" required>
                        <?php $__errorArgs = ['fecha_salida'];
$__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>
            </div>

            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Nro. Personas <span class="text-danger">*</span></label>
                        <input type="number" name="num_personas" class="form-control"
                               value="<?php echo e(old('num_personas', 1)); ?>" min="1" max="10" required>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Origen</label>
                        <select name="origen" class="form-control select2">
                            <?php $__currentLoopData = ['presencial','telefono','web','agencia']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $o): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($o); ?>" <?php echo e(old('origen') == $o ? 'selected' : ''); ?>><?php echo e(ucfirst($o)); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label>Descuento (S/)</label>
                        <input type="number" name="descuento" id="descuento" class="form-control"
                               value="<?php echo e(old('descuento', 0)); ?>" min="0" step="0.01">
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label>Observaciones</label>
                <textarea name="observaciones" class="form-control" rows="2" placeholder="Notas adicionales..."><?php echo e(old('observaciones')); ?></textarea>
            </div>
        </div>

        <div class="card-footer">
            <button type="submit" class="btn btn-primary"><i class="fas fa-save mr-2"></i>Confirmar Reserva</button>
            <a href="<?php echo e(route('reservas.index')); ?>" class="btn btn-secondary ml-2">Cancelar</a>
        </div>
    </form>
</div>
</div>


<div class="col-md-4">
    <div class="card" id="panelCalculo" style="display:none;">
        <div class="card-header bg-success text-white">
            <h3 class="card-title"><i class="fas fa-calculator mr-2"></i>Resumen de Costo</h3>
        </div>
        <div class="card-body">
            <table class="table table-sm table-borderless mb-0">
                <tr><td>Precio por noche:</td><td class="text-right font-weight-bold" id="precioNoche">—</td></tr>
                <tr><td>Número de noches:</td><td class="text-right font-weight-bold" id="numNoches">—</td></tr>
                <tr><td>Subtotal:</td><td class="text-right font-weight-bold" id="subtotal">—</td></tr>
                <tr class="text-danger"><td>Descuento:</td><td class="text-right" id="descuentoVal">—</td></tr>
                <tr class="font-weight-bold text-primary border-top">
                    <td>TOTAL:</td><td class="text-right h5" id="totalCalc">—</td>
                </tr>
            </table>
        </div>
    </div>

    <div class="card" id="alertaDisp" style="display:none;">
        <div class="card-body" id="alertaDispBody"></div>
    </div>
</div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
function calcular() {
    const habId = $('#habitacion_id').val();
    const entrada = $('#fecha_entrada').val();
    const salida = $('#fecha_salida').val();
    const descuento = parseFloat($('#descuento').val()) || 0;

    if (!habId || !entrada || !salida || entrada >= salida) {
        $('#panelCalculo').hide();
        return;
    }

    $.get("<?php echo e(route('reservas.disponibilidad')); ?>", {
        habitacion_id: habId,
        fecha_entrada: entrada,
        fecha_salida: salida
    }, function(data) {
        const body = $('#alertaDispBody');
        if (data.disponible) {
            $('#alertaDisp').hide();
            const total = data.subtotal - descuento;
            $('#precioNoche').text('S/ ' + parseFloat(data.precio_noche).toFixed(2));
            $('#numNoches').text(data.num_noches + ' noches');
            $('#subtotal').text('S/ ' + parseFloat(data.subtotal).toFixed(2));
            $('#descuentoVal').text('- S/ ' + descuento.toFixed(2));
            $('#totalCalc').text('S/ ' + Math.max(0, total).toFixed(2));
            $('#panelCalculo').show();
        } else {
            $('#panelCalculo').hide();
            body.html('<div class="alert alert-danger mb-0"><i class="fas fa-times-circle mr-2"></i>Habitación no disponible para las fechas seleccionadas.</div>');
            $('#alertaDisp').show();
        }
    });
}

$('#habitacion_id, #fecha_entrada, #fecha_salida, #descuento').on('change input', calcular);
$(document).ready(calcular);
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\Webs\PHP\sistema-hospedaje\resources\views/reservas/create.blade.php ENDPATH**/ ?>