<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$statusPill = static function (string $status): string {
    $label = ucwords(str_replace('_', ' ', $status));
    $active = ['approved', 'confirmed', 'fulfilled', 'delivered', 'dispatched', 'in_transit', 'sent', 'converted'];
    $inactive = ['cancelled', 'rejected'];
    $mod = '';
    if (in_array($status, $active, true)) {
        $mod = ' enterprise-status-pill--active';
    } elseif (in_array($status, $inactive, true)) {
        $mod = ' enterprise-status-pill--inactive';
    }

    return '<span class="enterprise-status-pill' . $mod . '">' . esc($label) . '</span>';
};

$trackStatuses = ['dispatched', 'in_transit', 'delivered', 'cancelled'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Tracking',
    'workspaceTitle'        => 'Delivery Tracking',
    'workspaceDesc'         => 'Track delivery status and shipment progress.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">In Transit</h2>
        <p class="enterprise-data-panel__subtitle">Update carrier details and delivery progress.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/delivery-tracking') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Number, company, tracking"
                           value="<?= esc($filters['search'] ?? '') ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['dispatched', 'in_transit', 'delivered'] as $s): ?>
                            <option value="<?= $s ?>" <?= ($filters['status'] ?? '') === $s ? 'selected' : '' ?>>
                                <?= esc(ucwords(str_replace('_', ' ', $s))) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Number</th>
                        <th>Company</th>
                        <th>Order</th>
                        <th>Current</th>
                        <th>Update</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($deliveries as $d): ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><code><?= esc($d['number']) ?></code></span></td>
                            <td><?= esc($d['company_name'] ?? '—') ?></td>
                            <td><?= ! empty($d['order_number']) ? '<code>' . esc($d['order_number']) . '</code>' : '—' ?></td>
                            <td><?= $statusPill($d['status']) ?></td>
                            <td class="p-2">
                                <form action="<?= base_url('sale/delivery-tracking/' . $d['id']) ?>" method="post" class="row g-2 align-items-end">
                                    <?= csrf_field() ?>
                                    <div class="col-md-2">
                                        <label class="enterprise-field-label">Status</label>
                                        <select name="status" class="form-select form-select-sm" required>
                                            <?php foreach ($trackStatuses as $s): ?>
                                                <option value="<?= $s ?>" <?= $d['status'] === $s ? 'selected' : '' ?>>
                                                    <?= esc(ucwords(str_replace('_', ' ', $s))) ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    <div class="col-md-2">
                                        <label class="enterprise-field-label">Carrier</label>
                                        <input type="text" name="carrier" class="form-control form-control-sm"
                                               value="<?= esc($d['carrier'] ?? '') ?>">
                                    </div>
                                    <div class="col-md-3">
                                        <label class="enterprise-field-label">Tracking Number</label>
                                        <input type="text" name="tracking_number" class="form-control form-control-sm"
                                               value="<?= esc($d['tracking_number'] ?? '') ?>">
                                    </div>
                                    <div class="col-md-3">
                                        <label class="enterprise-field-label">Notes</label>
                                        <input type="text" name="notes" class="form-control form-control-sm"
                                               value="<?= esc($d['notes'] ?? '') ?>">
                                    </div>
                                    <div class="col-md-2">
                                        <button type="submit" class="btn btn-sm btn-primary w-100">Update</button>
                                    </div>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($deliveries) === 0): ?>
                        <tr><td colspan="5" class="text-center text-muted py-5">No deliveries available for tracking.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
