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

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

<?php
$modeLabel = static function (string $mode): string {
    return $mode === 'add' ? 'Add/Subtract' : 'Set to';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Inventory',
    'workspaceCrumbCurrent' => 'Stock Adjustments',
    'workspaceTitle'        => 'Stock Adjustments',
    'workspaceDesc'         => 'Audit log of quantity changes across warehouses.',
    'workspaceAction'       => [
        'href'  => base_url('inventory/stock-adjustments/create'),
        'label' => 'New Adjustment',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Adjustments</h2>
        <p class="enterprise-data-panel__subtitle">Recorded changes to product quantities with before/after values.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('inventory/stock-adjustments') ?>" 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="Number, product, warehouse, reason"
                           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'] ?>" <?= (string) ($filters['warehouse_id'] ?? '') === (string) $w['id'] ? 'selected' : '' ?>>
                                <?= esc($w['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </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>Number</th>
                        <th>Date</th>
                        <th>Product</th>
                        <th>Warehouse</th>
                        <th>Mode</th>
                        <th class="text-end">Before</th>
                        <th class="text-end">After</th>
                        <th class="text-end">Delta</th>
                        <th>Reason</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($adjustments as $row): ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><?= esc($row['number']) ?></span></td>
                            <td><?= esc($row['adjustment_date']) ?></td>
                            <td>
                                <?= esc($row['product_name'] ?: '—') ?>
                                <?php if (! empty($row['sku'])): ?>
                                    <div class="text-muted small"><code><?= esc($row['sku']) ?></code></div>
                                <?php endif; ?>
                            </td>
                            <td><?= esc($row['warehouse_name'] ?: '—') ?></td>
                            <td><?= esc($modeLabel($row['mode'])) ?></td>
                            <td class="text-end"><?= number_format((float) $row['quantity_before'], 3) ?></td>
                            <td class="text-end"><?= number_format((float) $row['quantity_after'], 3) ?></td>
                            <td class="text-end fw-semibold"><?= number_format((float) $row['delta'], 3) ?></td>
                            <td class="text-muted small"><?= esc($row['reason'] ?: '—') ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($adjustments) === 0): ?>
                        <tr><td colspan="9" class="text-center text-muted py-5">No stock adjustments found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        <?php if ($pager): ?>
            <div class="mt-3"><?= $pager->links() ?></div>
        <?php endif; ?>
    </div>
</div>

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