<?php
/**
 * @var object $products
 * @var string $search
 * @var string $categoryId
 * @var string $supplierId
 * @var string $status
 * @var array $categories
 * @var array $suppliers
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-boxes me-2 text-gold"></i>Products</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Products</h6>
        </div>
        <div class="card-header-right">
            <?php if (can('inventory_create')): ?>
                <a href="<?= url('inventory/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Add Product
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3 g-2">
            <div class="col-md-4">
                <form method="GET" action="<?= url('inventory') ?>" class="d-flex gap-2">
                    <div class="input-group">
                        <span class="input-group-text bg-transparent border-gold">
                            <i class="fas fa-search text-gold"></i>
                        </span>
                        <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                               placeholder="Search by name, SKU or barcode..." value="<?= e($search) ?>">
                        <button type="submit" class="btn btn-sm btn-gold">Search</button>
                        <?php if (!empty($search)): ?>
                            <a href="<?= url('inventory') ?>" class="btn btn-sm btn-outline-secondary">
                                <i class="fas fa-times"></i>
                            </a>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
            <div class="col-md-2">
                <select name="category_id" class="form-select form-select-sm bg-transparent" onchange="filterBy(this, 'category_id')">
                    <option value="">All Categories</option>
                    <?php foreach ($categories as $cat): ?>
                        <option value="<?= $cat->id ?>" <?= $categoryId == $cat->id ? 'selected' : '' ?>>
                            <?= e($cat->name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <select name="supplier_id" class="form-select form-select-sm bg-transparent" onchange="filterBy(this, 'supplier_id')">
                    <option value="">All Suppliers</option>
                    <?php foreach ($suppliers as $sup): ?>
                        <option value="<?= $sup->id ?>" <?= $supplierId == $sup->id ? 'selected' : '' ?>>
                            <?= e($sup->name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <select name="status" class="form-select form-select-sm bg-transparent" onchange="filterBy(this, 'status')">
                    <option value="">All Status</option>
                    <option value="active" <?= $status === 'active' ? 'selected' : '' ?>>Active</option>
                    <option value="inactive" <?= $status === 'inactive' ? 'selected' : '' ?>>Inactive</option>
                    <option value="low_stock" <?= $status === 'low_stock' ? 'selected' : '' ?>>Low Stock</option>
                </select>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Name</th>
                        <th>SKU</th>
                        <th>Category</th>
                        <th class="text-center">Stock</th>
                        <th class="text-end">Price</th>
                        <th>Status</th>
                        <th width="120" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($products->data)): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-4">
                                <i class="fas fa-box-open fa-2x mb-2"></i>
                                <p class="mb-0">No products found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($products->currentPage - 1) * $products->perPage + 1; ?>
                        <?php foreach ($products->data as $product): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td>
                                    <a href="<?= url('inventory/show/' . $product->id) ?>" class="text-gold text-decoration-none">
                                        <strong><?= e($product->name) ?></strong>
                                    </a>
                                </td>
                                <td><code><?= e($product->sku ?? '-') ?></code></td>
                                <td><?= e($product->category_name ?? '-') ?></td>
                                <td class="text-center">
                                    <?php
                                    $stock = (int)$product->current_stock;
                                    $reorder = (int)$product->reorder_level;
                                    if ($stock <= 0):
                                    ?>
                                        <span class="badge bg-danger">
                                            <i class="fas fa-exclamation-triangle me-1"></i><?= $stock ?>
                                        </span>
                                    <?php elseif ($stock <= $reorder): ?>
                                        <span class="badge bg-warning text-dark">
                                            <i class="fas fa-exclamation-circle me-1"></i><?= $stock ?>
                                        </span>
                                    <?php else: ?>
                                        <span class="badge bg-success">
                                            <i class="fas fa-check-circle me-1"></i><?= $stock ?>
                                        </span>
                                    <?php endif; ?>
                                    <?php if (!$product->track_inventory): ?>
                                        <small class="text-muted d-block">Not tracked</small>
                                    <?php endif; ?>
                                </td>
                                <td class="text-end"><?= currency($product->unit_price ?? 0) ?></td>
                                <td>
                                    <span class="badge bg-<?= $product->is_active ? 'success' : 'secondary' ?>">
                                        <?= $product->is_active ? 'Active' : 'Inactive' ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <a href="<?= url('inventory/show/' . $product->id) ?>" class="btn btn-outline-info" title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <?php if (can('inventory_edit')): ?>
                                            <a href="<?= url('inventory/edit/' . $product->id) ?>" class="btn btn-outline-gold" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        <?php endif; ?>
                                        <?php if (can('inventory_delete')): ?>
                                            <button type="button" class="btn btn-outline-danger btn-delete"
                                                    data-id="<?= $product->id ?>"
                                                    data-name="<?= e($product->name) ?>"
                                                    title="Delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($products->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($products->currentPage - 1) * $products->perPage) + 1 ?>
                    to <?= min($products->currentPage * $products->perPage, $products->total) ?>
                    of <?= $products->total ?> entries
                </div>
                <?= paginateLinks($products, url('inventory')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php $inlineScripts = <<<'EOS'
function filterBy(el, param) {
    const url = new URL(window.location.href);
    url.searchParams.set(param, el.value);
    url.searchParams.delete('page');
    window.location.href = url.toString();
}

EOS;
$inlineScripts = str_replace('__URL__', rtrim(APP_URL, '/'), $inlineScripts);
?>
