

<?php $__env->startSection('content'); ?>

<div class="card">
   <div class="card-header d-flex justify-content-between align-items-center">
       <h5 class="mb-0">All Products</h5>
       <a href="<?php echo e(route('admin.new.product')); ?>" class="btn btn-primary btn-sm">+ Add New Product</a>
   </div>

   <div class="card-body">
       <!-- Filter Form -->
       <form method="GET" class="row g-2 mb-3">
           <div class="col-md-3">
               <input type="text" name="search" class="form-control"
                  value="<?php echo e(request('search')); ?>" placeholder="Search product">
           </div>
           <div class="col-md-3">
               <select name="category" class="form-control">
                   <option value="">All Categories</option>
                   <?php $__currentLoopData = $categories; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $category): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                       <option value="<?php echo e($category->id); ?>"
                           <?php echo e(request('category') == $category->id ? 'selected' : ''); ?>>
                           <?php echo e($category->name); ?>

                       </option>
                   <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
               </select>
           </div>
           <div class="col-md-3">
               <select name="status" class="form-control">
                   <option value="">Status</option>
                   <option value="1" <?php echo e(request('status') === '1' ? 'selected' : ''); ?>>Published</option>
                   <option value="0" <?php echo e(request('status') === '0' ? 'selected' : ''); ?>>Unpublished</option>
               </select>
               
               
           </div>
           <div class="col-md-3">
               <button class="btn btn-secondary w-100">Filter</button>
           </div>
       </form>

       <!-- Products Table -->
       <div class="table-responsive">
           <table class="table table-bordered align-middle">
               <thead class="table-light">
                   <tr>
                       <th width="30"><input type="checkbox" class="check-all"></th>
                       <th>Image</th>
                       <th>Name</th>
                       <th>Price</th>
                       <th>Stock</th>
                       <th>Status</th>
                       <th class="text-end">Action</th>
                   </tr>
               </thead>
               <tbody>
                   <?php $__currentLoopData = $allproducts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $product): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                   <tr>
                       <td><input type="checkbox" class="check-one" value="<?php echo e($product->id); ?>"></td>
                       <td>
                           <img src="<?php echo e($product->thumbnail_img ? asset('storage/' . $product->thumbnail_img) : asset('default-product.png')); ?>"
                               alt="<?php echo e($product->name); ?>" class="img-fluid" style="max-width:100px; max-height:80px; object-fit:cover;">
                       </td>
                       <td>
                           <strong><?php echo e($product->name); ?></strong><br>
                           <small class="text-muted">SKU: <?php echo e($product->sku); ?></small>
                       </td>
                       <td>Rs <?php echo e(number_format($product->unit_price, 2)); ?></td>
                       <td>
                           <?php if($product->current_stock > 0): ?>
                               <span class="badge bg-success">In Stock</span>
                           <?php else: ?>
                               <span class="badge bg-danger">Out of Stock</span>
                           <?php endif; ?>
                       </td>
                       <td>
                           <label class="aiz-switch aiz-switch-success mb-0">
                               <input type="checkbox" onchange="updatePublished(this)" value="<?php echo e($product->id); ?>" <?php echo e($product->published ? 'checked' : ''); ?>>
                               <span></span>
                           </label>
                       </td>
                       <td class="text-end">
                           <a href="<?php echo e(route('admin.products.edit', $product->id)); ?>" class="btn btn-sm btn-soft-primary">Edit</a>
                           <button onclick="deleteProduct(<?php echo e($product->id); ?>)" class="btn btn-sm btn-soft-danger">Delete</button>
                       </td>
                   </tr>
                   <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
               </tbody>
           </table>
       </div>

       <!-- Pagination -->
       <div class="mt-3">
           <?php echo e($allproducts->links()); ?>

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

<?php $__env->startPush('script'); ?>
<script>
    // Check all checkboxes
    document.querySelector('.check-all')?.addEventListener('change', function () {
        document.querySelectorAll('.check-one').forEach(cb => cb.checked = this.checked);
    });

    // Delete product
    function deleteProduct(id) {
        if(confirm('Are you sure?')) {
            // Example AJAX delete (you need your route)
            $.post("<?php echo e(route('admin.products.delete')); ?>", {
                _token: "<?php echo e(csrf_token()); ?>",
                id: id
            }, function(data){
                if(data.success) {
                    location.reload();
                } else {
                    alert('Something went wrong!');
                }
            });
        }
    }

    // Toggle published
    function updatePublished(el) {
        let status = el.checked ? 1 : 0;
        $.post("<?php echo e(route('admin.products.togglePublished')); ?>", {
            _token: "<?php echo e(csrf_token()); ?>",
            id: el.value,
            status: status
        }, function(data){
            if(data.success){
                alert('Status updated');
            } else {
                alert('Failed to update status');
            }
        });
    }
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('Admin.layout.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\wamp64\www\ecommerce\resources\views/admin/partials/products/productlist.blade.php ENDPATH**/ ?>