<?= $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>';
};
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Dispatch',
    'workspaceTitle'        => 'Dispatch',
    'workspaceDesc'         => 'Plan and release deliveries for dispatch.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Ready to Dispatch</h2>
        <p class="enterprise-data-panel__subtitle">Draft and ready delivery notes awaiting carrier assignment.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/dispatch') ?>" 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 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>Delivery Date</th>
                        <th>Status</th>
                        <th>Carrier / Tracking</th>
                        <th class="text-end">Action</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><?= esc($d['delivery_date']) ?></td>
                            <td><?= $statusPill($d['status']) ?></td>
                            <td colspan="2" class="p-2">
                                <form action="<?= base_url('sale/dispatch/' . $d['id']) ?>" method="post" class="d-flex flex-wrap gap-2 align-items-center justify-content-end">
                                    <?= csrf_field() ?>
                                    <input type="text" name="carrier" class="form-control form-control-sm" style="min-width:140px;"
                                           placeholder="Carrier" value="<?= esc($d['carrier'] ?? '') ?>">
                                    <input type="text" name="tracking_number" class="form-control form-control-sm" style="min-width:160px;flex:1;"
                                           placeholder="Tracking number" value="<?= esc($d['tracking_number'] ?? '') ?>">
                                    <button type="submit" class="btn btn-sm btn-primary">Dispatch</button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($deliveries) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No deliveries ready for dispatch.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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