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

<?= $this->section('styles') ?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.min.css">
<?= $this->endSection() ?>

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'active'      => 'active',
        'maintenance' => 'on_leave',
        'retired'     => 'terminated',
    ];
    $class = $map[$status] ?? 'inactive';

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

$hasCoords = ! empty($vehicle['latitude']) && ! empty($vehicle['longitude']);
$mapMarkers = $hasCoords ? json_encode([[
    'lat'   => $vehicle['latitude'],
    'lng'   => $vehicle['longitude'],
    'title' => $vehicle['registration_number'],
]], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) : '[]';
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Transport',
    'workspaceCrumbCurrent' => $vehicle['registration_number'],
    'workspaceTitle'        => esc($vehicle['registration_number']),
    'workspaceDesc'         => esc(trim(($vehicle['make'] ?? '') . ' ' . ($vehicle['model'] ?? '')) ?: 'Fleet vehicle'),
]) ?>

<div class="row g-4">
    <div class="col-lg-8">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-truck"></i>
                <h2 class="enterprise-settings-card__title">Vehicle Information</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Number</dt>
                <dd class="col-sm-8 fw-semibold"><code><?= esc($vehicle['number']) ?></code></dd>
                <dt class="col-sm-4 text-muted">Registration</dt>
                <dd class="col-sm-8"><?= esc($vehicle['registration_number']) ?></dd>
                <dt class="col-sm-4 text-muted">Make / Model</dt>
                <dd class="col-sm-8"><?= esc(trim(($vehicle['make'] ?? '') . ' ' . ($vehicle['model'] ?? '')) ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Year</dt>
                <dd class="col-sm-8"><?= esc($vehicle['year'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Type</dt>
                <dd class="col-sm-8"><?= esc(ucfirst($vehicle['vehicle_type'] ?? '—')) ?></dd>
                <dt class="col-sm-4 text-muted">Fuel</dt>
                <dd class="col-sm-8"><?= esc(ucfirst($vehicle['fuel_type'] ?? '—')) ?></dd>
                <dt class="col-sm-4 text-muted">Capacity</dt>
                <dd class="col-sm-8"><?= esc($vehicle['capacity'] !== null && $vehicle['capacity'] !== '' ? $vehicle['capacity'] : '—') ?></dd>
                <dt class="col-sm-4 text-muted">Odometer</dt>
                <dd class="col-sm-8"><?= $vehicle['odometer_km'] !== null && $vehicle['odometer_km'] !== '' ? esc(number_format((float) $vehicle['odometer_km'], 1)) . ' km' : '—' ?></dd>
                <dt class="col-sm-4 text-muted">Last Service</dt>
                <dd class="col-sm-8"><?= esc($vehicle['last_service_date'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Current Driver</dt>
                <dd class="col-sm-8"><?= esc($vehicle['driver_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Current Location</dt>
                <dd class="col-sm-8"><?= esc($vehicle['location_name'] ?? '—') ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($vehicle['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Coordinates</dt>
                <dd class="col-sm-8">
                    <?php if ($hasCoords): ?>
                        <?= esc($vehicle['latitude']) ?>, <?= esc($vehicle['longitude']) ?>
                    <?php else: ?>
                        —
                    <?php endif; ?>
                </dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($vehicle['notes'] ?: '—')) ?></dd>
            </dl>
        </article>

        <?php if ($hasCoords): ?>
            <article class="enterprise-settings-card mt-4">
                <div class="enterprise-settings-card__head">
                    <i class="fa-solid fa-map-location-dot"></i>
                    <h2 class="enterprise-settings-card__title">Location Map</h2>
                </div>
                <div
                    data-transport-fleet-map
                    data-provider="<?= esc($mapSettings['default_provider'] ?? 'openstreetmap') ?>"
                    data-default-lat="<?= esc((string) ($mapSettings['default_latitude'] ?? 26.0667)) ?>"
                    data-default-lng="<?= esc((string) ($mapSettings['default_longitude'] ?? 50.5577)) ?>"
                    data-default-zoom="<?= esc((string) ($mapSettings['default_zoom'] ?? 12)) ?>"
                    data-markers='<?= $mapMarkers ?>'
                >
                    <div class="transport-map-canvas" style="height:320px;border-radius:8px;border:1px solid #e5e7eb;"></div>
                </div>
            </article>
        <?php endif; ?>
    </div>

    <div class="col-lg-4">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-bolt"></i>
                <h2 class="enterprise-settings-card__title">Actions</h2>
            </div>
            <div class="d-grid gap-2">
                <a href="<?= base_url('transport/vehicles/' . $vehicle['id'] . '/edit') ?>" class="btn btn-outline-primary btn-sm">
                    <i class="fa-solid fa-pen me-1"></i> Edit
                </a>
                <form action="<?= base_url('transport/vehicles/' . $vehicle['id'] . '/delete') ?>" method="post"
                      onsubmit="return confirm('Delete this vehicle permanently?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-outline-danger btn-sm w-100">
                        <i class="fa-solid fa-trash me-1"></i> Delete
                    </button>
                </form>
                <a href="<?= base_url('transport/vehicles') ?>" class="btn btn-outline-secondary btn-sm">Back to Vehicles</a>
            </div>
        </article>
    </div>
</div>

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

<?php if ($hasCoords): ?>
<?= $this->section('scripts') ?>
<?php if (($mapSettings['default_provider'] ?? 'openstreetmap') === 'google' && ! empty($mapSettings['google_api_key'])): ?>
    <script src="https://maps.googleapis.com/maps/api/js?key=<?= esc($mapSettings['google_api_key']) ?>"></script>
<?php else: ?>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.min.js"></script>
<?php endif; ?>
<script src="<?= base_url('assets/js/transport-map.js') ?>"></script>
<?= $this->endSection() ?>
<?php endif; ?>
