<div class="header">
    <h1>Providers & Prices</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="get" action="<?= base_url('admin/providers') ?>" style="display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: 1.5rem;">
    <div>
        <label for="q">Search provider name</label>
        <input type="text" name="q" id="q" value="<?= esc($filter_q ?? '') ?>" placeholder="Search…" style="min-width: 180px;">
    </div>
    <div>
        <label for="type">Type</label>
        <select name="type" id="type">
            <option value="">All</option>
            <option value="fuel" <?= ($filter_type ?? '') === 'fuel' ? 'selected' : '' ?>>Fuel</option>
            <option value="gas" <?= ($filter_type ?? '') === 'gas' ? 'selected' : '' ?>>Gas</option>
        </select>
    </div>
    <button type="submit" class="btn btn-primary">Filter</button>
    <?php if (($filter_q ?? '') !== '' || ($filter_type ?? '') !== ''): ?>
    <a href="<?= base_url('admin/providers') ?>" class="btn btn-secondary">Clear</a>
    <?php endif ?>
</form>
<h2 style="margin: 1rem 0 0.5rem;">Add provider</h2>
<form method="post" action="<?= base_url('admin/providers/add') ?>" style="display:flex; align-items:center; gap:0.5rem; flex-wrap:wrap; margin-bottom:1.5rem;">
    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
    <input type="text" name="name" placeholder="Provider name" required style="min-width:160px;">
    <select name="provider_type">
        <option value="fuel">Fuel</option>
        <option value="gas">Gas</option>
    </select>
    <button type="submit" class="btn btn-primary">Add provider</button>
</form>

<table>
    <thead>
        <tr><th>ID</th><th>Name</th><th>Type</th><th>Items / Prices</th><th>Actions</th></tr>
    </thead>
    <tbody>
        <?php foreach ($providers as $p): ?>
        <?php $items = $inv->where('provider_id', $p->id)->findAll(); ?>
        <tr>
            <td><?= $p->id ?></td>
            <td><?= esc($p->name) ?></td>
            <td><?= esc($p->provider_type ?? 'fuel') ?></td>
            <td>
                <?php foreach ($items as $i): ?>
                <?php $isGasSize = ($i->item_type ?? '') === 'gas_size'; ?>
                <form method="post" action="<?= base_url('admin/providers/price') ?>" style="display:flex; align-items:center; gap:0.5rem; margin-bottom:0.5rem; flex-wrap:wrap;">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <input type="hidden" name="inventory_id" value="<?= $i->id ?>">
                    <span style="min-width:80px;"><?= esc($i->item_name) ?>:</span>
                    <?php if ($isGasSize): ?>
                    <span title="Refill price">Refill</span>
                    <?php endif; ?>
                    <input type="number" name="price" value="<?= $i->price ?>" step="0.01" style="width:100px; margin-bottom:0;" title="<?= $isGasSize ? 'Refill price (UGX)' : 'Price (UGX)' ?>">
                    <?php if ($isGasSize): ?>
                    <span title="New kit price">Kit</span>
                    <input type="number" name="price_kit" value="<?= isset($i->price_kit) && $i->price_kit !== null ? (float)$i->price_kit : '' ?>" step="0.01" placeholder="New kit" style="width:100px; margin-bottom:0;" title="New kit price (UGX) – cylinder ownership">
                    <?php endif; ?>
                    <span>UGX</span>
                    <button type="submit" class="btn btn-secondary" style="padding:0.25rem 0.5rem;">Save</button>
                </form>
                <form method="post" action="<?= base_url('admin/providers/item/delete/' . $i->id) ?>" style="display:inline;">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <button type="submit" class="btn btn-secondary" style="padding:0.25rem 0.5rem;" onclick="return confirm('Delete this item?');">Delete</button>
                </form>
                <?php endforeach ?>
                <form method="post" action="<?= base_url('admin/providers/item/add') ?>" style="display:flex; align-items:center; gap:0.5rem; margin-top:0.5rem; flex-wrap:wrap;">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <input type="hidden" name="provider_id" value="<?= $p->id ?>">
                    <input type="text" name="item_name" placeholder="Item name" style="width:100px;">
                    <input type="number" name="price" placeholder="Refill price" step="0.01" style="width:90px;">
                    <input type="number" name="price_kit" placeholder="New kit price" step="0.01" style="width:90px;" title="Only for Gas size – New kit price (UGX)">
                    <input type="number" name="stock_count" placeholder="Stock" value="0" min="0" style="width:60px;">
                    <input type="url" name="image_url" placeholder="Image URL" style="width:140px;">
                    <select name="item_type" style="width:90px;">
                        <option value="fuel">Fuel</option>
                        <option value="gas_size">Gas size</option>
                    </select>
                    <button type="submit" class="btn btn-secondary" style="padding:0.25rem 0.5rem;">Add item</button>
                </form>
            </td>
            <td>
                <a href="<?= base_url('admin/providers/edit/' . $p->id) ?>" class="btn btn-secondary" style="margin-right:0.25rem;">Edit</a>
                <form method="post" action="<?= base_url('admin/providers/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 provider and all its items?');">Delete</button>
                </form>
            </td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>
