<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Inventory',
    'workspaceCrumbCurrent' => 'Reorder Levels',
    'workspaceTitle'        => 'Reorder Levels',
    'workspaceDesc'         => 'Set minimum stock thresholds and monitor current totals.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Products</h2>
        <p class="enterprise-data-panel__subtitle">Edit reorder levels inline and save all changes at once.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('inventory/reorder-levels') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Name, SKU, or barcode"
                           value="<?= esc($filters['search']) ?>">
                </div>
                <div class="d-flex align-items-end">
                    <div class="form-check">
                        <input type="checkbox" name="low_stock" value="1" class="form-check-input" id="lowStockFilter"
                               <?= $filters['low_stock'] ? 'checked' : '' ?>>
                        <label class="form-check-label small" for="lowStockFilter">Low stock only</label>
                    </div>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                </div>
            </div>
        </form>
    </div>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small mx-3 mt-3">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <form action="<?= base_url('inventory/reorder-levels') ?>" method="post">
        <?= csrf_field() ?>
        <div class="enterprise-data-panel__body">
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th>SKU</th>
                            <th class="text-end">Total Stock</th>
                            <th style="width:160px;">Reorder Level</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($products as $row): ?>
                            <?php
                            $totalStock   = (float) ($row['total_stock'] ?? 0);
                            $reorderLevel = (float) ($row['reorder_level'] ?? 0);
                            $isLow        = $reorderLevel > 0 && $totalStock <= $reorderLevel;
                            ?>
                            <tr class="<?= $isLow ? 'table-warning' : '' ?>">
                                <td>
                                    <span class="enterprise-data-table__primary"><?= esc($row['name']) ?></span>
                                    <?php if ($isLow): ?>
                                        <span class="badge bg-warning text-dark ms-1">Low</span>
                                    <?php endif; ?>
                                </td>
                                <td><code><?= esc($row['sku']) ?></code></td>
                                <td class="text-end"><?= number_format($totalStock, 3) ?></td>
                                <td>
                                    <input type="hidden" name="product_id[]" value="<?= (int) $row['id'] ?>">
                                    <input type="number" step="0.001" min="0" name="reorder_level[]" class="form-control form-control-sm"
                                           value="<?= esc($reorderLevel) ?>">
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (count($products) === 0): ?>
                            <tr><td colspan="4" class="text-center text-muted py-5">No products found.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <?php if (count($products) > 0): ?>
                <div class="mt-3 text-end">
                    <button type="submit" class="btn btn-primary">
                        <i class="fa-solid fa-floppy-disk me-1"></i> Save Reorder Levels
                    </button>
                </div>
            <?php endif; ?>
        </div>
    </form>
</div>

<?= $this->endSection() ?>
