

<?php $__env->startSection('content'); ?>
<div class="d-flex align-items-center mb-4">
    <a href="<?php echo e(route('products.index')); ?>" class="btn btn-outline-secondary btn-sm me-3"><i class="bi bi-arrow-left"></i></a>
    <div>
        <h2 class="fw-bold text-dark mb-0">Editar Producto</h2>
        <p class="text-muted mb-0">Gestiona detalles y receta</p>
    </div>
</div>

<form action="<?php echo e(route('products.update', $product->id)); ?>" method="POST" enctype="multipart/form-data" class="row g-4">
    <?php echo csrf_field(); ?>
    <?php echo method_field('PUT'); ?>

    <div class="col-md-8">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body p-4">
                <h6 class="fw-bold text-primary mb-3">Información General</h6>

                <div class="mb-3">
                    <label class="form-label fw-bold small text-muted">Nombre del Producto</label>
                    <input type="text" name="name" class="form-control" value="<?php echo e($product->name); ?>" required>
                </div>

                <div class="row g-3 mb-3">
                    <div class="col-md-6">
                        <label class="form-label fw-bold small text-muted">Categoría</label>
                        <select name="category_id" class="form-select" required>
                            <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $cat): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($cat->id); ?>" <?php echo e($product->category_id == $cat->id ? 'selected' : ''); ?>><?php echo e($cat->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-bold small text-muted">Código de Barras</label>
                        <div class="input-group">
                            <span class="input-group-text bg-white"><i class="bi bi-upc-scan"></i></span>
                            <input type="text" name="barcode" class="form-control" value="<?php echo e($product->barcode); ?>" placeholder="Escanear...">
                        </div>
                    </div>
                </div>

                <div class="row g-3 mb-3">
                    <div class="col-md-6">
                        <label class="form-label fw-bold small text-muted">Precio (S/)</label>
                        <input type="number" step="0.01" name="price" class="form-control" value="<?php echo e($product->price); ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label fw-bold small text-muted">Stock Actual (Lectura)</label>
                        <input type="text" class="form-control bg-light" value="<?php echo e($product->stock); ?>" readonly>
                        <small class="text-muted" style="font-size: 0.7rem;">Si tiene receta, el stock dependerá de los insumos.</small>
                    </div>
                </div>

                <div class="form-check form-switch mb-4 bg-light p-3 rounded border">
                    <input class="form-check-input" type="checkbox" name="is_saleable" id="saleableCheck" <?php echo e($product->is_saleable ? 'checked' : ''); ?>>
                    <label class="form-check-label fw-bold ms-2" for="saleableCheck">Disponible para Venta en POS</label>
                    <div class="small text-muted ms-1 mt-1">Si desmarcas esto, el producto servirá como <strong>Insumo</strong> (para recetas) pero no aparecerá en el menú de ventas.</div>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold small text-muted">Imagen</label>
                    <div class="d-flex align-items-center gap-3">
                        <?php if($product->image): ?>
                            <img src="<?php echo e(asset('storage/'.$product->image)); ?>" class="rounded border" width="60" height="60" style="object-fit: cover;">
                        <?php endif; ?>
                        <input type="file" name="image" class="form-control" accept="image/*">
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-primary text-white py-2 d-flex justify-content-between align-items-center">
                <h6 class="mb-0 fw-bold"><i class="bi bi-basket me-2"></i>Receta / Insumos</h6>
                <button type="button" class="btn btn-sm btn-light text-primary fw-bold" onclick="addIngredient()">+ Agregar</button>
            </div>
            <div class="card-body p-3 bg-light overflow-auto" style="max-height: 400px;">
                <p class="small text-muted mb-3">Selecciona los insumos que componen este plato. Al venderlo, se descontarán automáticamente.</p>
                
                <div id="ingredients-list">
                    <?php $__currentLoopData = $product->ingredients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ingredient): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="input-group mb-2" id="row-<?php echo e($ingredient->id); ?>">
                            <span class="input-group-text bg-white border-end-0" style="font-size: 0.8rem; width: 60%;"><?php echo e($ingredient->name); ?></span>
                            <input type="number" step="0.01" name="ingredients[<?php echo e($ingredient->id); ?>]" value="<?php echo e($ingredient->pivot->quantity); ?>" class="form-control form-control-sm text-center border-start-0" placeholder="Cant.">
                            <button type="button" class="btn btn-sm btn-outline-danger bg-white" onclick="document.getElementById('row-<?php echo e($ingredient->id); ?>').remove()"><i class="bi bi-x"></i></button>
                        </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </div>

                <div class="d-none" id="ingredient-select-template">
                    <div class="input-group mb-2 ingredient-row">
                        <select class="form-select form-select-sm" style="width: 55%;" onchange="setIngredientName(this)">
                            <option value="">- Insumo -</option>
                            <?php $__currentLoopData = $ingredients; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ing): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($ing->id); ?>"><?php echo e($ing->name); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <input type="number" step="0.01" class="form-control form-control-sm text-center" placeholder="Cant.">
                        <button type="button" class="btn btn-sm btn-danger" onclick="this.parentElement.remove()"><i class="bi bi-x"></i></button>
                    </div>
                </div>

            </div>
            <div class="card-footer bg-white border-0 text-end">
                <button type="submit" class="btn btn-success fw-bold w-100">
                    <i class="bi bi-check-lg me-2"></i> Guardar Cambios
                </button>
            </div>
        </div>
    </div>
</form>

<script>
    function addIngredient() {
        let container = document.getElementById('ingredients-list');
        let template = document.getElementById('ingredient-select-template').innerHTML;
        let div = document.createElement('div');
        div.innerHTML = template;
        container.appendChild(div.firstElementChild);
    }

    function setIngredientName(select) {
        let row = select.parentElement;
        let inputQty = row.querySelector('input[type="number"]');
        if (select.value) {
            inputQty.name = "ingredients[" + select.value + "]";
            inputQty.required = true;
        }
    }
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\restaurante\resources\views/products/edit.blade.php ENDPATH**/ ?>