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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'planned'     => 'draft',
        'scheduled'   => 'default',
        'in_progress' => 'active',
        'completed'   => 'active',
        'cancelled'   => 'terminated',
    ];
    $class = $map[$status] ?? 'inactive';

    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">'
        . esc(ucwords(str_replace('_', ' ', $status))) . '</span>';
};

$priorityPill = static function (string $priority): string {
    $map = ['low' => 'inactive', 'normal' => 'default', 'high' => 'on_leave', 'urgent' => 'terminated'];
    $class = $map[$priority] ?? 'default';

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Transport',
    'workspaceCrumbCurrent' => 'Dashboard',
    'workspaceTitle'        => 'Transport Overview',
    'workspaceDesc'         => 'Fleet vehicles, drivers, routes, scheduling, and live map tracking.',
    'workspaceAction'       => [
        'href'  => base_url('transport/scheduling/create'),
        'label' => 'Schedule Trip',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<section class="enterprise-kpi-grid mb-4">
    <?php foreach ($kpis as $kpi): ?>
        <article class="enterprise-kpi-card">
            <div class="enterprise-kpi-header">
                <span class="enterprise-kpi-label"><?= esc($kpi['label']) ?></span>
                <span class="enterprise-kpi-icon"><i class="fa-solid <?= esc($kpi['icon']) ?>"></i></span>
            </div>
            <div class="enterprise-kpi-value"><?= esc($kpi['value']) ?></div>
            <div class="enterprise-kpi-footer">
                <span class="enterprise-kpi-badge enterprise-kpi-badge--<?= esc($kpi['badgeType']) ?>">
                    <?= esc($kpi['badge']) ?>
                </span>
                <span class="enterprise-kpi-dept"><?= esc($kpi['dept']) ?></span>
            </div>
        </article>
    <?php endforeach; ?>
</section>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Upcoming Trips</h2>
        <a href="<?= base_url('transport/scheduling') ?>" class="btn btn-sm btn-outline-primary">View All</a>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Number</th>
                        <th>Title</th>
                        <th>Date</th>
                        <th>Departure</th>
                        <th>Route</th>
                        <th>Vehicle</th>
                        <th>Driver</th>
                        <th>Priority</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($upcoming as $row): ?>
                        <tr>
                            <td>
                                <a href="<?= base_url('transport/scheduling/' . $row['id']) ?>" class="enterprise-data-table__primary">
                                    <code><?= esc($row['number']) ?></code>
                                </a>
                            </td>
                            <td><?= esc($row['title']) ?></td>
                            <td><?= esc($row['schedule_date']) ?></td>
                            <td><?= esc($row['departure_time'] ?: '—') ?></td>
                            <td><?= esc($row['route_name'] ?? '—') ?></td>
                            <td><?= esc($row['vehicle_registration'] ?? '—') ?></td>
                            <td><?= esc($row['driver_name'] ?? '—') ?></td>
                            <td><?= $priorityPill($row['priority'] ?? 'normal') ?></td>
                            <td><?= $statusPill($row['status']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($upcoming) === 0): ?>
                        <tr><td colspan="9" class="text-center text-muted py-5">No upcoming trips scheduled.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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