<?= $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 {
    $class = $status === 'active' ? 'active' : 'inactive';

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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Transport',
    'workspaceCrumbCurrent' => $location['name'],
    'workspaceTitle'        => esc($location['name']),
    'workspaceDesc'         => esc(ucfirst($location['location_type'] ?? 'Location')),
]) ?>

<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-map-location-dot"></i>
                <h2 class="enterprise-settings-card__title">Location 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($location['number']) ?></code></dd>
                <dt class="col-sm-4 text-muted">Name</dt>
                <dd class="col-sm-8"><?= esc($location['name']) ?></dd>
                <dt class="col-sm-4 text-muted">Code</dt>
                <dd class="col-sm-8"><?= esc($location['code'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Type</dt>
                <dd class="col-sm-8"><?= esc(ucfirst($location['location_type'] ?? '—')) ?></dd>
                <dt class="col-sm-4 text-muted">Address</dt>
                <dd class="col-sm-8"><?= esc($location['address'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">City</dt>
                <dd class="col-sm-8"><?= esc($location['city'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Country</dt>
                <dd class="col-sm-8"><?= esc($location['country'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Contact</dt>
                <dd class="col-sm-8"><?= esc($location['contact_name'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Phone</dt>
                <dd class="col-sm-8"><?= esc($location['contact_phone'] ?: '—') ?></dd>
                <dt class="col-sm-4 text-muted">Coordinates</dt>
                <dd class="col-sm-8">
                    <?php if ($hasCoords): ?>
                        <?= esc($location['latitude']) ?>, <?= esc($location['longitude']) ?>
                    <?php else: ?>
                        —
                    <?php endif; ?>
                </dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($location['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Notes</dt>
                <dd class="col-sm-8"><?= nl2br(esc($location['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"></i>
                    <h2 class="enterprise-settings-card__title">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/locations/' . $location['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/locations/' . $location['id'] . '/delete') ?>" method="post"
                      onsubmit="return confirm('Delete this location 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/locations') ?>" class="btn btn-outline-secondary btn-sm">Back to Locations</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; ?>
