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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Inventory',
    'workspaceCrumbCurrent' => 'New Adjustment',
    'workspaceTitle'        => 'Create Stock Adjustment',
    'workspaceDesc'         => 'Set or add quantity for a product in a warehouse with a reason.',
]) ?>

<article class="enterprise-settings-card" style="max-width:640px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-sliders"></i>
        <h2 class="enterprise-settings-card__title">Adjustment</h2>
    </div>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <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/stock-adjustments') ?>" method="post" novalidate>
        <?= csrf_field() ?>
        <div class="enterprise-settings-fields enterprise-settings-fields--single">
            <div class="enterprise-field">
                <label class="enterprise-field-label">Product <span class="text-danger">*</span></label>
                <select name="product_id" class="form-select" required>
                    <option value="">Select product</option>
                    <?php foreach ($products as $p): ?>
                        <option value="<?= (int) $p['id'] ?>" <?= (int) old('product_id') === (int) $p['id'] ? 'selected' : '' ?>>
                            <?= esc($p['name']) ?> (<?= esc($p['sku']) ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Warehouse <span class="text-danger">*</span></label>
                <select name="warehouse_id" class="form-select" required>
                    <option value="">Select warehouse</option>
                    <?php foreach ($warehouses as $w): ?>
                        <option value="<?= (int) $w['id'] ?>" <?= (int) old('warehouse_id') === (int) $w['id'] ? 'selected' : '' ?>>
                            <?= esc($w['name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Mode <span class="text-danger">*</span></label>
                <select name="mode" class="form-select" required>
                    <option value="set" <?= old('mode', 'set') === 'set' ? 'selected' : '' ?>>Set quantity to</option>
                    <option value="add" <?= old('mode') === 'add' ? 'selected' : '' ?>>Add / subtract quantity</option>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Quantity <span class="text-danger">*</span></label>
                <input type="number" step="0.001" name="quantity" class="form-control" value="<?= esc(old('quantity', '0')) ?>" required>
                <div class="form-text">For “add”, use a negative value to reduce stock.</div>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Reason</label>
                <input type="text" name="reason" class="form-control" maxlength="150" value="<?= esc(old('reason')) ?>" placeholder="e.g. Damaged goods, cycle count correction">
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Notes</label>
                <textarea name="notes" class="form-control" rows="2"><?= esc(old('notes')) ?></textarea>
            </div>
        </div>
        <div class="enterprise-settings-card__footer">
            <a href="<?= base_url('inventory/stock-adjustments') ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Apply Adjustment</button>
        </div>
    </form>
</article>

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