<?php
$vehicle   = $vehicle ?? [];
$drivers   = $drivers ?? [];
$locations = $locations ?? [];
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Registration Number <span class="text-danger">*</span></label>
        <input type="text" name="registration_number" class="form-control" maxlength="50" required
               value="<?= esc(old('registration_number', $vehicle['registration_number'] ?? '')) ?>"
               placeholder="e.g. ABC-1234">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Make</label>
        <input type="text" name="make" class="form-control" maxlength="100"
               value="<?= esc(old('make', $vehicle['make'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Model</label>
        <input type="text" name="model" class="form-control" maxlength="100"
               value="<?= esc(old('model', $vehicle['model'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Year</label>
        <input type="number" name="year" class="form-control" min="1900" max="2100"
               value="<?= esc(old('year', $vehicle['year'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Vehicle Type</label>
        <select name="vehicle_type" class="form-select">
            <option value="">Select type</option>
            <?php foreach (['car', 'van', 'truck', 'bus', 'motorcycle', 'other'] as $t): ?>
                <option value="<?= $t ?>" <?= old('vehicle_type', $vehicle['vehicle_type'] ?? '') === $t ? 'selected' : '' ?>>
                    <?= esc(ucfirst($t)) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Fuel Type</label>
        <select name="fuel_type" class="form-select">
            <option value="">Select fuel</option>
            <?php foreach (['petrol', 'diesel', 'electric', 'hybrid', 'other'] as $f): ?>
                <option value="<?= $f ?>" <?= old('fuel_type', $vehicle['fuel_type'] ?? '') === $f ? 'selected' : '' ?>>
                    <?= esc(ucfirst($f)) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Capacity</label>
        <input type="number" name="capacity" class="form-control" min="0"
               value="<?= esc(old('capacity', $vehicle['capacity'] ?? '')) ?>"
               placeholder="Passengers or load units">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Odometer (km)</label>
        <input type="number" name="odometer_km" class="form-control" min="0" step="0.01"
               value="<?= esc(old('odometer_km', $vehicle['odometer_km'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Last Service Date</label>
        <input type="date" name="last_service_date" class="form-control"
               value="<?= esc(old('last_service_date', $vehicle['last_service_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Current Driver</label>
        <select name="current_driver_id" class="form-select">
            <option value="">Unassigned</option>
            <?php foreach ($drivers as $d): ?>
                <option value="<?= (int) $d['id'] ?>"
                    <?= (string) old('current_driver_id', $vehicle['current_driver_id'] ?? '') === (string) $d['id'] ? 'selected' : '' ?>>
                    <?= esc($d['employee_name'] ?? $d['number']) ?>
                    <?php if (! empty($d['number'])): ?>(<?= esc($d['number']) ?>)<?php endif; ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Current Location</label>
        <select name="current_location_id" class="form-select">
            <option value="">None</option>
            <?php foreach ($locations as $loc): ?>
                <option value="<?= (int) $loc['id'] ?>"
                    <?= (string) old('current_location_id', $vehicle['current_location_id'] ?? '') === (string) $loc['id'] ? 'selected' : '' ?>>
                    <?= esc($loc['name']) ?><?= ! empty($loc['code']) ? ' (' . esc($loc['code']) . ')' : '' ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['active', 'maintenance', 'retired'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $vehicle['status'] ?? 'active') === $s ? 'selected' : '' ?>>
                    <?= esc(ucfirst($s)) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="3"><?= esc(old('notes', $vehicle['notes'] ?? '')) ?></textarea>
    </div>
</div>

<?= view('Modules\Transport\Views\partials\map_picker', [
    'mapSettings' => $mapSettings ?? [],
    'latitude'    => old('latitude', $vehicle['latitude'] ?? ''),
    'longitude'   => old('longitude', $vehicle['longitude'] ?? ''),
    'mapId'       => 'vehicle-map-picker',
]) ?>
