<div class="header">
    <h1>Rescue+</h1>
</div>

<h2 style="margin: 2rem 0 1rem;">Rescue requests</h2>
<form method="get" action="<?= base_url('admin/rescue') ?>" style="display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: 1.5rem;">
    <div>
        <label for="q">Search (user name/phone)</label>
        <input type="text" name="q" id="q" value="<?= esc($filter_q ?? '') ?>" placeholder="Search…" style="min-width: 180px;">
    </div>
    <div>
        <label for="date_from">From date</label>
        <input type="date" name="date_from" id="date_from" value="<?= esc($filter_date_from ?? '') ?>">
    </div>
    <div>
        <label for="date_to">To date</label>
        <input type="date" name="date_to" id="date_to" value="<?= esc($filter_date_to ?? '') ?>">
    </div>
    <div>
        <label for="status">Status</label>
        <select name="status" id="status">
            <option value="">All</option>
            <option value="pending" <?= ($filter_status ?? '') === 'pending' ? 'selected' : '' ?>>Pending</option>
            <option value="paid" <?= ($filter_status ?? '') === 'paid' ? 'selected' : '' ?>>Paid</option>
            <option value="dispatched" <?= ($filter_status ?? '') === 'dispatched' ? 'selected' : '' ?>>Dispatched</option>
            <option value="delivered" <?= ($filter_status ?? '') === 'delivered' ? 'selected' : '' ?>>Delivered</option>
            <option value="cancelled" <?= ($filter_status ?? '') === 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
            <option value="pending_group" <?= ($filter_status ?? '') === 'pending_group' ? 'selected' : '' ?>>Pending (all)</option>
        </select>
    </div>
    <button type="submit" class="btn btn-primary">Filter</button>
    <?php if (($filter_q ?? '') !== '' || ($filter_date_from ?? '') !== '' || ($filter_date_to ?? '') !== '' || ($filter_status ?? '') !== ''): ?>
    <a href="<?= base_url('admin/rescue') ?>" class="btn btn-secondary">Clear</a>
    <?php endif ?>
</form>
<table>
    <thead>
        <tr><th>ID</th><th>Status</th><th>User</th><th>Amount</th><th>Date</th><th></th></tr>
    </thead>
    <tbody>
        <?php foreach ($orders as $o): ?>
        <?php
        $userLabel = trim(($o->user_name ?? '') . ' ' . ($o->user_username ?? ''));
        if ($userLabel === '') { $userLabel = $o->user_phone ?? '#' . $o->user_id; }
        ?>
        <tr>
            <td>#<?= $o->id ?></td>
            <td><span style="display: inline-block; padding: 0.25rem 0.75rem; background: #e5e7eb; border-radius: 9999px; font-size: 0.75rem; font-weight: 600;"><?= esc($o->status) ?></span></td>
            <td><?= esc($userLabel) ?></td>
            <td><?= number_format($o->total_amount) ?> UGX</td>
            <td><?= esc($o->created_at) ?></td>
            <td><a href="<?= base_url('admin/orders/' . $o->id) ?>" class="btn btn-secondary">View / Assign</a></td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>

<h2 style="margin: 2rem 0 1rem;">Rescue service types</h2>
<table>
    <thead>
        <tr><th>ID</th><th>Name</th><th>Price</th></tr>
    </thead>
    <tbody>
        <?php foreach ($services as $s): ?>
        <tr>
            <td><?= $s->id ?></td>
            <td><?= esc($s->name) ?></td>
            <td><?= number_format($s->price) ?> UGX</td>
        </tr>
        <?php endforeach ?>
    </tbody>
</table>

<div style="background: var(--surface); padding: 1.5rem; border-radius: var(--radius); box-shadow: var(--shadow); margin-top: 2rem;">
    <h3 style="margin-bottom: 1rem;">Add new service type</h3>
    <form method="post" action="<?= base_url('admin/rescue/service') ?>" style="display: flex; gap: 1rem; align-items: flex-end; flex-wrap: wrap;">
        <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
        <div style="flex: 1 1 200px;">
            <label>Service name</label>
            <input type="text" name="name" required style="width:100%;">
        </div>
        <div style="flex: 0 1 150px;">
            <label>Price (UGX)</label>
            <input type="number" name="price" step="0.01" min="0" value="0" style="width:100%;">
        </div>
        <button type="submit" class="btn btn-primary">Add service</button>
    </form>
</div>