<?php $__env->startSection('title', 'Products'); ?>

<?php $__env->startSection('styles'); ?>
<style>
.stock-badge-in_stock    { background:#d1fae5; color:#065f46; }
.stock-badge-low         { background:#fef3c7; color:#92400e; }
.stock-badge-out_of_stock{ background:#fee2e2; color:#991b1b; }
.stock-badge-negative    { background:#ede9fe; color:#5b21b6; }
.product-img { width:40px; height:40px; object-fit:cover; border-radius:6px; background:#f1f5f9; display:flex;align-items:center;justify-content:center;font-size:1.2rem; }
.filter-bar  { background:#fff; border:1px solid #e2e8f0; border-radius:10px; padding:14px 18px; margin-bottom:1rem; }
</style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <div>
        <h4 class="mb-0 fw-bold"><i class="bi bi-box-seam text-primary me-2"></i>Products</h4>
        <p class="text-muted mb-0 small"><?php echo e($products->total()); ?> total products</p>
    </div>
    <div class="d-flex gap-2">
        <a href="<?php echo e(route('products.import')); ?>" class="btn btn-outline-secondary btn-sm"><i class="bi bi-upload me-1"></i>Import</a>
        <a href="<?php echo e(route('products.export')); ?>" class="btn btn-outline-success btn-sm"><i class="bi bi-download me-1"></i>Export</a>
        <a href="<?php echo e(route('products.barcodes.print')); ?>" class="btn btn-outline-warning btn-sm"><i class="bi bi-upc-scan me-1"></i>Barcodes</a>
        <a href="<?php echo e(route('products.create')); ?>" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i>New Product</a>
    </div>
</div>


<div class="filter-bar">
    <form method="GET" action="<?php echo e(route('products.index')); ?>" class="row g-2 align-items-end">
        <div class="col-md-3">
            <input type="text" name="search" class="form-control form-control-sm" placeholder="Search name, code, barcode..." value="<?php echo e(request('search')); ?>">
        </div>
        <div class="col-md-2">
            <select name="category_id" class="form-select form-select-sm">
                <option value="">All Categories</option>
                <?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(request('category_id') == $cat->id ? 'selected' : ''); ?>><?php echo e($cat->name_en); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>
        <div class="col-md-2">
            <select name="brand_id" class="form-select form-select-sm">
                <option value="">All Brands</option>
                <?php $__currentLoopData = $brands; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $brand): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <option value="<?php echo e($brand->id); ?>" <?php echo e(request('brand_id') == $brand->id ? 'selected' : ''); ?>><?php echo e($brand->name_en); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>
        <div class="col-md-2">
            <select name="stock_status" class="form-select form-select-sm">
                <option value="">All Stock Status</option>
                <option value="in_stock"     <?php echo e(request('stock_status') == 'in_stock' ? 'selected' : ''); ?>>In Stock</option>
                <option value="low"          <?php echo e(request('stock_status') == 'low' ? 'selected' : ''); ?>>Low Stock</option>
                <option value="out_of_stock" <?php echo e(request('stock_status') == 'out_of_stock' ? 'selected' : ''); ?>>Out of Stock</option>
            </select>
        </div>
        <div class="col-md-2">
            <select name="status" class="form-select form-select-sm">
                <option value="">All Status</option>
                <option value="active"   <?php echo e(request('status') == 'active' ? 'selected' : ''); ?>>Active</option>
                <option value="inactive" <?php echo e(request('status') == 'inactive' ? 'selected' : ''); ?>>Inactive</option>
            </select>
        </div>
        <div class="col-md-1">
            <button class="btn btn-primary btn-sm w-100" type="submit"><i class="bi bi-search"></i></button>
        </div>
    </form>
</div>


<div class="card shadow-sm border-0">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover table-dense mb-0">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Code / Barcode</th>
                        <th>Category</th>
                        <th>Brand</th>
                        <th>Unit</th>
                        <th class="text-end">Cost Price</th>
                        <th class="text-end">Sale Price</th>
                        <th class="text-end">Stock</th>
                        <th class="text-center">VAT</th>
                        <th class="text-center">Status</th>
                        <th class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $products; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $product): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <tr>
                        <td>
                            <div class="fw-semibold"><?php echo e($product->name_en); ?></div>
                            <div class="text-muted small" dir="rtl"><?php echo e($product->name_ar); ?></div>
                        </td>
                        <td>
                            <div class="text-monospace small fw-semibold"><?php echo e($product->product_code); ?></div>
                            <?php if($product->barcode): ?>
                                <div class="text-muted small"><?php echo e($product->barcode); ?></div>
                            <?php endif; ?>
                        </td>
                        <td class="small"><?php echo e($product->category?->name_en ?? '—'); ?></td>
                        <td class="small"><?php echo e($product->brand?->name_en ?? '—'); ?></td>
                        <td class="small"><?php echo e($product->baseUnit?->code ?? '—'); ?></td>
                        <td class="text-end small"><?php echo e(number_format($product->cost_price, 2)); ?></td>
                        <td class="text-end small fw-semibold"><?php echo e(number_format($product->sale_price, 2)); ?></td>
                        <td class="text-end">
                            <?php
                                $stockQty  = $product->total_stock ?? 0;
                                $status    = $stockQty < 0 ? 'negative' : ($stockQty == 0 ? 'out_of_stock' : ($product->reorder_level > 0 && $stockQty <= $product->reorder_level ? 'low' : 'in_stock'));
                                $statusLabel = match($status) {
                                    'negative'     => 'Negative',
                                    'out_of_stock' => 'Out',
                                    'low'          => 'Low',
                                    default        => 'In Stock'
                                };
                            ?>
                            <span class="badge rounded-pill stock-badge-<?php echo e($status); ?>">
                                <?php echo e(number_format($stockQty, 0)); ?>

                            </span>
                        </td>
                        <td class="text-center">
                            <?php if($product->vat_applicable): ?>
                                <span class="badge bg-success-subtle text-success"><?php echo e(number_format($product->vat_rate, 0)); ?>%</span>
                            <?php else: ?>
                                <span class="badge bg-secondary-subtle text-secondary">Exempt</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <span class="badge <?php echo e($product->status == 'active' ? 'bg-success-subtle text-success' : 'bg-danger-subtle text-danger'); ?>">
                                <?php echo e(ucfirst($product->status)); ?>

                            </span>
                        </td>
                        <td class="text-center">
                            <div class="d-flex justify-content-center gap-1">
                                <a href="<?php echo e(route('products.show', $product)); ?>" class="btn btn-xs btn-outline-info" title="View"><i class="bi bi-eye"></i></a>
                                <a href="<?php echo e(route('products.edit', $product)); ?>" class="btn btn-xs btn-outline-primary" title="Edit"><i class="bi bi-pencil"></i></a>
                                <a href="<?php echo e(route('stock-ledger.by-warehouse', $product)); ?>" class="btn btn-xs btn-outline-secondary" title="Stock by Warehouse"><i class="bi bi-boxes"></i></a>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <tr>
                        <td colspan="11" class="text-center py-5 text-muted">
                            <i class="bi bi-box-seam display-6 d-block mb-2"></i>
                            No products found.
                            <a href="<?php echo e(route('products.create')); ?>" class="d-block mt-2">Create your first product</a>
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php if($products->hasPages()): ?>
    <div class="card-footer bg-transparent d-flex justify-content-between align-items-center">
        <small class="text-muted">Showing <?php echo e($products->firstItem()); ?>–<?php echo e($products->lastItem()); ?> of <?php echo e($products->total()); ?></small>
        <?php echo e($products->links()); ?>

    </div>
    <?php endif; ?>
</div>

<style>
.btn-xs { padding: 0.15rem 0.45rem; font-size: 0.75rem; }
</style>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\saudi\resources\views/products/index.blade.php ENDPATH**/ ?>