<?php
/**
 * DX Tracking Modal Body Content (AJAX partial — injected into #LGmodal .modal-body)
 *
 * @var string      $trackingNumber
 * @var bool        $success
 * @var string|null $error
 * @var array       $events  Raw response from DX Tracking/GetConsignment
 */

use yii\helpers\Html;
?>

<?php if (!$success) { ?>
        <div class="alert alert-danger">
            <?= Html::encode($error ?? 'An unknown error occurred.') ?>
        </div>
    <?php } elseif (empty($events)) { ?>
        <p class="text-muted">No tracking events found for this consignment.</p>
    <?php } else { ?>
        <?php
        // Normalise: response may be a flat array of events or wrapped under a key
        $items = [];
        if (isset($events[0]) && is_array($events[0])) {
            $items = $events;
        } elseif (isset($events['items']) && is_array($events['items'])) {
            $items = $events['items'];
        } elseif (isset($events['Events']) && is_array($events['Events'])) {
            $items = $events['Events'];
        } else {
            // Single event or unknown structure – wrap so we can iterate
            $items = [$events];
        }
        ?>
        <div class="table-responsive">
            <table class="table table-sm table-bordered table-hover mb-0">
                <thead class="thead-light">
                    <tr>
                        <th>Date / Time</th>
                        <th>Status</th>
                        <th>Description</th>
                        <th>Location</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $event) { ?>
                        <?php
                        // Handle both PascalCase and snake_case field names from the API
                        $eventDate   = $event['EventDate']        ?? $event['event_date']   ?? $event['DateTime'] ?? $event['date'] ?? '';
                        $status      = $event['StatusCode']       ?? $event['status_code']  ?? $event['Status']   ?? $event['status'] ?? '';
                        $description = $event['StatusDescription'] ?? $event['description'] ?? $event['Description'] ?? $event['detail'] ?? '';
                        $location    = $event['Depot']            ?? $event['depot']        ?? $event['Location'] ?? $event['location'] ?? '';
                        ?>
                        <tr>
                            <td class="text-nowrap"><?= Html::encode($eventDate) ?></td>
                            <td><span class="badge badge-secondary"><?= Html::encode($status) ?></span></td>
                            <td><?= Html::encode($description) ?></td>
                            <td><?= Html::encode($location) ?></td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>

        <?php
        // Show any non-array top-level fields as a summary (e.g. ConsignmentStatus)
        $summary = array_filter($events, fn($v) => !is_array($v));
        if ($summary) {
        ?>
            <dl class="row small mt-3 mb-0">
                <?php foreach ($summary as $key => $value) { ?>
                    <dt class="col-sm-4"><?= Html::encode(ucwords(str_replace('_', ' ', (string) $key))) ?></dt>
                    <dd class="col-sm-8"><?= Html::encode((string) $value) ?></dd>
                <?php } ?>
            </dl>
        <?php } ?>
    <?php } ?>

