<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="row justify-content-center">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-box-seam" style="color: var(--brand);"></i> <?= $item ? 'Edit Inventory Item' : 'New Inventory Item' ?>
            </div>
            <div class="card-body">
                <form action="<?= $item ? site_url('inventory/' . $item->id) : site_url('inventory') ?>" method="post">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label class="form-label small fw-semibold">Item Name</label>
                        <input type="text" class="form-control" name="name" value="<?= esc(old('name', $item->name ?? '')) ?>" required>
                    </div>
                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Category</label>
                            <select class="form-select" name="category" required>
                                <?php foreach ($lookups->inventoryCategories as $key => $meta): ?>
                                    <option value="<?= $key ?>" <?= old('category', $item->category ?? '') === $key ? 'selected' : '' ?>><?= esc($meta['label']) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label small fw-semibold">Unit</label>
                            <input type="text" class="form-control" name="unit" placeholder="pcs, kg, rolls, sets..." value="<?= esc(old('unit', $item->unit ?? '')) ?>" required>
                        </div>
                    </div>
                    <div class="row g-3 mb-4">
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Quantity in Stock</label>
                            <input type="number" min="0" class="form-control" name="quantity_in_stock" value="<?= esc(old('quantity_in_stock', $item->quantity_in_stock ?? 0)) ?>">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Reorder Level</label>
                            <input type="number" min="0" class="form-control" name="reorder_level" value="<?= esc(old('reorder_level', $item->reorder_level ?? 0)) ?>">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label small fw-semibold">Unit Cost (GHS)</label>
                            <input type="text" class="form-control" name="unit_cost" value="<?= esc(old('unit_cost', $item->unit_cost ?? '0.00')) ?>">
                        </div>
                    </div>
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="bi bi-check2 me-1"></i>Save Item</button>
                        <a href="<?= site_url('inventory') ?>" class="btn btn-outline-secondary">Cancel</a>
                        <?php if ($item): ?>
                            <form action="<?= site_url('inventory/' . $item->id . '/delete') ?>" method="post" class="ms-auto" onsubmit="return confirm('Delete this item?');">
                                <?= csrf_field() ?>
                                <button type="submit" class="btn btn-outline-danger"><i class="bi bi-trash me-1"></i>Delete</button>
                            </form>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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