<div class="header">
    <h1>Order #<?= $order->id ?></h1>
</div>
<div style="background: var(--surface); padding: 1.5rem; border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 2rem;">
    <p><strong>Type:</strong> <?= esc($order->order_type ?? '—') ?> | <strong>Status:</strong> <span style="display: inline-block; padding: 0.25rem 0.75rem; background: #e5e7eb; border-radius: 9999px; font-size: 0.75rem; font-weight: 600;"><?= esc($order->status) ?></span> | <strong>Total:</strong> <?= number_format($order->total_amount) ?> UGX</p>
    <p><strong>User:</strong> <?= $user ? esc($user->phone) : (empty($order->user_id) ? 'Account removed' : '—') ?></p>
    <p><strong>Driver:</strong> <?= $driver ? esc($driver->display_name ?? $driver->username ?? $driver->phone) : '—' ?> <?= $driver && $driver->phone ? '(' . esc($driver->phone) . ')' : '' ?></p>
    <p><strong>Address:</strong> <?= $location ? esc(! empty($location->plus_code) ? $location->plus_code : ($location->latitude !== null && $location->longitude !== null ? $location->latitude . ',' . $location->longitude : ($location->address_name ?? '—'))) : '—' ?></p>
    <?php
    $meta = isset($order->order_meta) ? (is_string($order->order_meta) ? json_decode($order->order_meta, true) : $order->order_meta) : [];
    if (! empty($meta['car_plate'])): ?>
    <p><strong>Car number plate:</strong> <?= esc($meta['car_plate']) ?> <span style="color: var(--text-light);">(for driver to locate vehicle)</span></p>
    <?php endif;
    if (! empty($meta['kit_addons']) && is_array($meta['kit_addons'])): ?>
    <p><strong>New kit add-ons:</strong></p>
    <ul style="margin: 0.25rem 0 0 1.5rem;">
        <?php foreach ($meta['kit_addons'] as $a): ?>
        <li><?= esc($a['name'] ?? '—') ?> — <?= number_format((float) ($a['price'] ?? 0)) ?> UGX</li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>
</div>

<?php if (in_array($order->status, ['paid', 'pending']) && $order->driver_id == null): ?>
<form method="post" action="<?= base_url('admin/orders/' . $order->id . '/assign') ?>" style="background: var(--surface); padding: 1.5rem; border-radius: var(--radius); box-shadow: var(--shadow); margin: 1.5rem 0;">
    <input type="hidden" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>">
    <label style="font-weight: 600; margin-bottom: 0.5rem;">Assign driver (user ID):</label>
    <input type="number" name="driver_id" min="1" placeholder="Driver user ID" style="max-width: 200px;">
    <button type="submit" class="btn btn-primary" style="margin-left: 1rem;">Assign</button>
</form>
<?php endif ?>

<p><a href="<?= base_url('admin/orders') ?>" class="btn btn-secondary">← Back to list</a></p>