<div class="header">
    <h1>Shop</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 ?>

<h2 style="margin: 2rem 0 1rem;">Categories</h2>
<form method="post" action="<?= base_url('admin/shop/category/add') ?>" style="display:flex; align-items:center; gap:0.5rem; margin-bottom:1rem;">
    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
    <input type="text" name="name" placeholder="Category name" required>
    <button type="submit" class="btn btn-primary">Add category</button>
</form>
<table>
    <thead>
        <tr><th>ID</th><th>Name</th><th>Actions</th></tr>
    </thead>
    <tbody>
        <?php foreach ($categories as $c): ?>
        <tr>
            <td><?= $c->id ?></td>
            <td><?= esc($c->name) ?></td>
            <td>
                <a href="<?= base_url('admin/shop/category/edit/' . $c->id) ?>" class="btn btn-secondary" style="margin-right:0.25rem;">Edit</a>
                <form method="post" action="<?= base_url('admin/shop/category/delete/' . $c->id) ?>" style="display:inline;">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <button type="submit" class="btn btn-secondary" onclick="return confirm('Delete this category?');">Delete</button>
                </form>
            </td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>

<h2 style="margin: 2rem 0 1rem;">Products</h2>
<form method="get" action="<?= base_url('admin/shop') ?>" style="display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: 1rem;">
    <div>
        <label for="q">Search product name</label>
        <input type="text" name="q" id="q" value="<?= esc($filter_q ?? '') ?>" placeholder="Search…" style="min-width: 180px;">
    </div>
    <button type="submit" class="btn btn-primary">Search</button>
    <?php if (($filter_q ?? '') !== ''): ?>
    <a href="<?= base_url('admin/shop') ?>" class="btn btn-secondary">Clear</a>
    <?php endif ?>
</form>
<form method="post" action="<?= base_url('admin/shop/product/add') ?>" style="display:flex; align-items:center; gap:0.5rem; flex-wrap:wrap; margin-bottom:1rem;">
    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
    <input type="text" name="item_name" placeholder="Product name" required style="min-width:140px;">
    <input type="number" name="price" placeholder="Price" step="0.01" style="width:100px;">
    <input type="number" name="stock_count" placeholder="Stock" value="0" min="0" style="width:70px;">
    <input type="url" name="image_url" placeholder="Image URL" style="min-width:140px;">
    <select name="category_id" style="min-width:140px;">
        <option value="">— Category —</option>
        <?php foreach ($categories as $c): ?>
        <option value="<?= $c->id ?>"><?= esc($c->name) ?></option>
        <?php endforeach ?>
    </select>
    <select name="provider_id" style="min-width:100px;">
        <?php foreach ($providers ?? [] as $pr): ?>
        <option value="<?= $pr->id ?>"><?= esc($pr->name) ?></option>
        <?php endforeach ?>
    </select>
    <button type="submit" class="btn btn-primary">Add product</button>
</form>
<table>
    <thead>
        <tr><th>ID</th><th>Name</th><th>Price</th><th>Stock</th><th>Category</th><th>Actions</th></tr>
    </thead>
    <tbody>
        <?php foreach ($products as $p): ?>
        <?php
        $catName = '—';
        if (!empty($p->category_id)) {
            foreach ($categories as $c) {
                if ((int)$c->id === (int)$p->category_id) { $catName = esc($c->name); break; }
            }
        }
        ?>
        <tr>
            <td><?= $p->id ?></td>
            <td><?= esc($p->item_name) ?></td>
            <td><?= number_format($p->price) ?> UGX</td>
            <td><?= $p->stock_count ?></td>
            <td><?= $catName ?></td>
            <td>
                <a href="<?= base_url('admin/shop/product/edit/' . $p->id) ?>" class="btn btn-secondary" style="margin-right:0.25rem;">Edit</a>
                <form method="post" action="<?= base_url('admin/shop/product/delete/' . $p->id) ?>" style="display:inline;">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <button type="submit" class="btn btn-secondary" onclick="return confirm('Delete this product?');">Delete</button>
                </form>
            </td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>
