<?= $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 = ['approved', 'confirmed', 'in_progress', 'fulfilled', 'cancelled'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Sales & CRM',
    'workspaceCrumbCurrent' => 'Order Tracking',
    'workspaceTitle'        => 'Order Tracking',
    'workspaceDesc'         => 'Track sales order status through fulfillment.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Fulfillment Tracking</h2>
        <p class="enterprise-data-panel__subtitle">Update status and notes for approved orders.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('sale/order-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, quotation"
                           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 (['approved', 'confirmed', 'in_progress', 'fulfilled'] 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 Date</th>
                        <th>Current</th>
                        <th>Update Status</th>
                        <th>Tracking Notes</th>
                        <th class="text-end">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($orders as $o): ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><code><?= esc($o['number']) ?></code></span></td>
                            <td><?= esc($o['company_name'] ?? '—') ?></td>
                            <td><?= esc($o['order_date']) ?></td>
                            <td><?= $statusPill($o['status']) ?></td>
                            <td colspan="3" class="p-2">
                                <form action="<?= base_url('sale/order-tracking/' . $o['id']) ?>" method="post" class="d-flex flex-wrap gap-2 align-items-center justify-content-end">
                                    <?= csrf_field() ?>
                                    <select name="status" class="form-select form-select-sm" style="min-width:140px;" required>
                                        <?php foreach ($trackStatuses as $s): ?>
                                            <option value="<?= $s ?>" <?= $o['status'] === $s ? 'selected' : '' ?>>
                                                <?= esc(ucwords(str_replace('_', ' ', $s))) ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                    <input type="text" name="tracking_notes" class="form-control form-control-sm" style="min-width:180px;flex:1;"
                                           placeholder="Tracking notes" value="<?= esc($o['tracking_notes'] ?? '') ?>">
                                    <button type="submit" class="btn btn-sm btn-primary">Update</button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($orders) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No orders available for tracking.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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