<div class="header">
    <h1>Service images & brand logos</h1>
</div>
<?php if (session('message')): ?>
<p class="message"><?= esc(session('message')) ?></p>
<?php endif ?>

<h2 style="margin: 0 0 0.5rem;">Service cards (Gas, Fuel, Rescue+)</h2>
<p style="margin-bottom: 1rem; color: var(--text-light);">Image URLs for the Services screen cards. Leave empty for no image.</p>
<table>
    <thead>
        <tr><th>Service</th><th>Image URL</th><th></th></tr>
    </thead>
    <tbody>
        <?php foreach ($services as $s): ?>
        <tr>
            <td><?= esc($s->name) ?></td>
            <td>
                <form method="post" action="<?= base_url('admin/service-images/save') ?>" style="display:flex; align-items:center; gap:0.5rem; flex-wrap:wrap;">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <input type="hidden" name="service_id" value="<?= $s->id ?>">
                    <input type="url" name="image_url" value="<?= esc($s->image_url ?? '') ?>" placeholder="https://..." style="flex:1; min-width:200px; padding:0.5rem;">
                    <button type="submit" class="btn btn-primary">Save</button>
                </form>
            </td>
            <td>
                <?php if (! empty($s->image_url)): ?>
                <img src="<?= esc($s->image_url) ?>" alt="" style="max-width:80px; max-height:50px; object-fit:cover; border-radius:4px;" onerror="this.style.display='none'">
                <?php endif ?>
            </td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>

<h2 style="margin: 2rem 0 0.5rem;">Provider / brand logos</h2>
<p style="margin-bottom: 1rem; color: var(--text-light);">Logo URLs for Shell, Total, Stabex, etc. Shown when user selects a brand in Gas or Fuel flow.</p>
<table>
    <thead>
        <tr><th>Brand</th><th>Type</th><th>Logo URL</th><th></th></tr>
    </thead>
    <tbody>
        <?php foreach ($providers as $p): ?>
        <tr>
            <td><?= esc($p->name) ?></td>
            <td><?= esc($p->provider_type ?? '—') ?></td>
            <td>
                <form method="post" action="<?= base_url('admin/service-images/provider-logo') ?>" style="display:flex; align-items:center; gap: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="url" name="logo_url" value="<?= esc($p->logo_url ?? '') ?>" placeholder="https://..." style="flex:1; min-width:200px; padding:0.5rem;">
                    <button type="submit" class="btn btn-primary">Save</button>
                </form>
            </td>
            <td>
                <?php if (! empty($p->logo_url)): ?>
                <img src="<?= esc($p->logo_url) ?>" alt="" style="max-width:60px; max-height:40px; object-fit:contain; border-radius:4px;" onerror="this.style.display='none'">
                <?php endif ?>
            </td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>

<h2 style="margin: 2rem 0 0.5rem;">Gas sizes</h2>
<p style="margin-bottom: 1rem; color: var(--text-light);">Image URL for each gas size. You can also set it when adding an item under Providers.</p>
<table>
    <thead>
        <tr><th>ID</th><th>Name</th><th>Provider</th><th>Image URL</th><th>Preview</th></tr>
    </thead>
    <tbody>
        <?php foreach ($gasSizes as $row): ?>
        <?php $prov = $providerById[(int)($row->provider_id ?? 0)] ?? null; ?>
        <tr>
            <td><?= (int) $row->id ?></td>
            <td><?= esc($row->item_name) ?></td>
            <td><?= $prov ? esc($prov->name) : '—' ?></td>
            <td>
                <form method="post" action="<?= base_url('admin/service-images/inventory-image') ?>" style="display:flex; align-items:center; gap:0.5rem; flex-wrap:wrap;">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <input type="hidden" name="inventory_id" value="<?= $row->id ?>">
                    <input type="url" name="image_url" value="<?= esc($row->image_url ?? '') ?>" placeholder="https://..." style="flex:1; min-width:200px; padding:0.5rem;">
                    <button type="submit" class="btn btn-primary">Save</button>
                </form>
            </td>
            <td>
                <?php if (! empty($row->image_url)): ?>
                <img src="<?= esc($row->image_url) ?>" alt="" style="max-width:60px; max-height:50px; object-fit:cover; border-radius:4px;" onerror="this.style.display='none'">
                <?php endif ?>
            </td>
        </tr>
        <?php endforeach ?>
        <?php if (empty($gasSizes)): ?>
        <tr><td colspan="5">No gas sizes. Add items under Providers with type "Gas size".</td></tr>
        <?php endif ?>
    </tbody>
</table>

<h2 style="margin: 2rem 0 0.5rem;">Shop products</h2>
<p style="margin-bottom: 1rem; color: var(--text-light);">Image URL for each shop product. You can also set it when adding or editing a product under Shop.</p>
<table>
    <thead>
        <tr><th>ID</th><th>Name</th><th>Provider</th><th>Image URL</th><th>Preview</th></tr>
    </thead>
    <tbody>
        <?php foreach ($shopProducts as $row): ?>
        <?php $prov = $providerById[(int)($row->provider_id ?? 0)] ?? null; ?>
        <tr>
            <td><?= (int) $row->id ?></td>
            <td><?= esc($row->item_name) ?></td>
            <td><?= $prov ? esc($prov->name) : '—' ?></td>
            <td>
                <form method="post" action="<?= base_url('admin/service-images/inventory-image') ?>" style="display:flex; align-items:center; gap:0.5rem; flex-wrap:wrap;">
                    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
                    <input type="hidden" name="inventory_id" value="<?= $row->id ?>">
                    <input type="url" name="image_url" value="<?= esc($row->image_url ?? '') ?>" placeholder="https://..." style="flex:1; min-width:200px; padding:0.5rem;">
                    <button type="submit" class="btn btn-primary">Save</button>
                </form>
            </td>
            <td>
                <?php if (! empty($row->image_url)): ?>
                <img src="<?= esc($row->image_url) ?>" alt="" style="max-width:60px; max-height:50px; object-fit:cover; border-radius:4px;" onerror="this.style.display='none'">
                <?php endif ?>
            </td>
        </tr>
        <?php endforeach ?>
        <?php if (empty($shopProducts)): ?>
        <tr><td colspan="5">No shop products. Add products under Shop.</td></tr>
        <?php endif ?>
    </tbody>
</table>
