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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Inventory',
    'workspaceCrumbCurrent' => 'Stock',
    'workspaceTitle'        => 'Stock Balances',
    'workspaceDesc'         => 'On-hand quantities by product and warehouse.',
    'workspaceAction'       => [
        'href'  => base_url('inventory/stock/adjust'),
        'label' => 'Adjust Stock',
        'icon'  => 'fa-solid fa-sliders',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Stock</h2>
        <p class="enterprise-data-panel__subtitle">Filter by warehouse or show items at or below reorder level.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('inventory/stock') ?>" 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="Product, SKU, or warehouse"
                           value="<?= esc($filters['search']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Warehouse</label>
                    <select name="warehouse_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($warehouses as $w): ?>
                            <option value="<?= (int) $w['id'] ?>" <?= (int) ($filters['warehouse_id'] ?? 0) === (int) $w['id'] ? 'selected' : '' ?>><?= esc($w['name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="d-flex align-items-end pb-1">
                    <div class="form-check">
                        <input type="checkbox" name="low_stock" value="1" class="form-check-input" id="low_stock"
                               <?= ! empty($filters['low_stock']) ? 'checked' : '' ?>>
                        <label class="form-check-label small" for="low_stock">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>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>SKU</th>
                        <th>Warehouse</th>
                        <th class="text-end">Quantity</th>
                        <th class="text-end">Reserved</th>
                        <th>Unit</th>
                        <th class="text-end">Reorder</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($balances as $row): ?>
                        <?php $low = (float) $row['reorder_level'] > 0 && (float) $row['quantity'] <= (float) $row['reorder_level']; ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><?= esc($row['product_name']) ?></span></td>
                            <td><code><?= esc($row['sku']) ?></code></td>
                            <td><?= esc($row['warehouse_name']) ?></td>
                            <td class="text-end<?= $low ? ' text-danger fw-semibold' : '' ?>"><?= number_format((float) $row['quantity'], 3) ?></td>
                            <td class="text-end"><?= number_format((float) $row['reserved_qty'], 3) ?></td>
                            <td><?= esc($row['unit_code'] ?: '—') ?></td>
                            <td class="text-end"><?= number_format((float) $row['reorder_level'], 3) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($balances) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No stock balances found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php if ($pager): ?>
            <div class="mt-3"><?= $pager->links() ?></div>
        <?php endif; ?>
    </div>
</div>

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