<div class="header">
    <h1>Edit product</h1>
</div>

<?php if (session('message')): ?>
<p class="message"><?= esc(session('message')) ?></p>
<?php endif ?>
<?php if (session('error')): ?>
<p class="error"><?= esc(session('error')) ?></p>
<?php endif ?>

<form method="post" action="<?= base_url('admin/shop/product/edit/' . $product->id) ?>">
    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
    <p>
        <label>Name</label><br>
        <input type="text" name="item_name" value="<?= esc($product->item_name) ?>" required>
    </p>
    <p>
        <label>Price (UGX)</label><br>
        <input type="number" name="price" value="<?= (float) $product->price ?>" step="0.01" min="0">
    </p>
    <p>
        <label>Stock</label><br>
        <input type="number" name="stock_count" value="<?= (int) $product->stock_count ?>" min="0">
    </p>
    <p>
        <label>Image URL</label><br>
        <input type="url" name="image_url" value="<?= esc(old('image_url', $product->image_url ?? '')) ?>" placeholder="https://...">
    </p>
    <p>
        <label>Category</label><br>
        <select name="category_id">
            <option value="">— None —</option>
            <?php foreach ($categories as $c): ?>
            <option value="<?= $c->id ?>" <?= ((int)($product->category_id ?? 0) === (int)$c->id) ? 'selected' : '' ?>><?= esc($c->name) ?></option>
            <?php endforeach ?>
        </select>
    </p>
    <p>
        <button type="submit" class="btn btn-primary">Save</button>
        <a href="<?= base_url('admin/shop') ?>" class="btn btn-secondary">Cancel</a>
    </p>
</form>
