<?php
$mapSettings = $mapSettings ?? [];
$mapId       = $mapId ?? 'transport-map-picker';
$latInputId  = $latInputId ?? 'latitude';
$lngInputId  = $lngInputId ?? 'longitude';
$latitude    = old('latitude', $latitude ?? '');
$longitude   = old('longitude', $longitude ?? '');

$provider   = (string) ($mapSettings['default_provider'] ?? 'openstreetmap');
$defaultLat = (float) ($mapSettings['default_latitude'] ?? 26.0667);
$defaultLng = (float) ($mapSettings['default_longitude'] ?? 50.5577);
$defaultZoom = (int) ($mapSettings['default_zoom'] ?? 12);
?>
<div class="enterprise-field enterprise-field--full">
    <label class="enterprise-field-label">Map Location</label>
    <p class="text-muted small mb-2">Click the map or drag the marker to set coordinates.</p>
    <div
        id="<?= esc($mapId) ?>"
        data-transport-map-picker
        data-provider="<?= esc($provider) ?>"
        data-default-lat="<?= esc((string) $defaultLat) ?>"
        data-default-lng="<?= esc((string) $defaultLng) ?>"
        data-default-zoom="<?= esc((string) $defaultZoom) ?>"
        data-lat-input="<?= esc($latInputId) ?>"
        data-lng-input="<?= esc($lngInputId) ?>"
    >
        <div class="transport-map-canvas" style="height:320px;border-radius:8px;border:1px solid #e5e7eb;background:#f9fafb;"></div>
    </div>
    <div class="row g-2 mt-2">
        <div class="col-sm-6">
            <label class="enterprise-field-label small">Latitude</label>
            <input type="text" name="latitude" id="<?= esc($latInputId) ?>" class="form-control form-control-sm"
                   value="<?= esc($latitude !== null && $latitude !== '' ? $latitude : '') ?>" readonly>
        </div>
        <div class="col-sm-6">
            <label class="enterprise-field-label small">Longitude</label>
            <input type="text" name="longitude" id="<?= esc($lngInputId) ?>" class="form-control form-control-sm"
                   value="<?= esc($longitude !== null && $longitude !== '' ? $longitude : '') ?>" readonly>
        </div>
    </div>
</div>
