<?php
$route     = $route ?? [];
$stops     = $stops ?? [];
$locations = $locations ?? [];

$oldStopLocations = old('stop_location_id');
$oldStopOffsets   = old('stop_offset_minutes');
if (is_array($oldStopLocations)) {
    $stopRows = [];
    foreach ($oldStopLocations as $idx => $locId) {
        $stopRows[] = [
            'location_id'                      => $locId,
            'estimated_arrival_offset_minutes' => $oldStopOffsets[$idx] ?? '',
        ];
    }
} elseif (count($stops) > 0) {
    $stopRows = $stops;
} else {
    $stopRows = [['location_id' => '', 'estimated_arrival_offset_minutes' => '']];
}
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" maxlength="200" required
               value="<?= esc(old('name', $route['name'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Code</label>
        <input type="text" name="code" class="form-control" maxlength="30"
               value="<?= esc(old('code', $route['code'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Origin Location</label>
        <select name="origin_location_id" class="form-select">
            <option value="">Select origin</option>
            <?php foreach ($locations as $loc): ?>
                <option value="<?= (int) $loc['id'] ?>"
                    <?= (string) old('origin_location_id', $route['origin_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">Destination Location</label>
        <select name="destination_location_id" class="form-select">
            <option value="">Select destination</option>
            <?php foreach ($locations as $loc): ?>
                <option value="<?= (int) $loc['id'] ?>"
                    <?= (string) old('destination_location_id', $route['destination_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">Distance (km)</label>
        <input type="number" name="distance_km" class="form-control" min="0" step="0.01"
               value="<?= esc(old('distance_km', $route['distance_km'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Est. Duration (minutes)</label>
        <input type="number" name="estimated_duration_minutes" class="form-control" min="0"
               value="<?= esc(old('estimated_duration_minutes', $route['estimated_duration_minutes'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['active', 'inactive'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $route['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', $route['notes'] ?? '')) ?></textarea>
    </div>
</div>

<div class="mt-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h3 class="h6 mb-0">Intermediate Stops</h3>
        <button type="button" class="btn btn-sm btn-outline-primary" id="route-add-stop">
            <i class="fa-solid fa-plus me-1"></i> Add Stop
        </button>
    </div>
    <div id="route-stops-container">
        <?php foreach ($stopRows as $stop): ?>
            <div class="route-stop-row border rounded p-3 mb-2">
                <div class="row g-2 align-items-end">
                    <div class="col-md-7">
                        <label class="enterprise-field-label small">Location</label>
                        <select name="stop_location_id[]" class="form-select form-select-sm">
                            <option value="">Select stop location</option>
                            <?php foreach ($locations as $loc): ?>
                                <option value="<?= (int) $loc['id'] ?>"
                                    <?= (string) ($stop['location_id'] ?? '') === (string) $loc['id'] ? 'selected' : '' ?>>
                                    <?= esc($loc['name']) ?><?= ! empty($loc['code']) ? ' (' . esc($loc['code']) . ')' : '' ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="enterprise-field-label small">Offset (min)</label>
                        <input type="number" name="stop_offset_minutes[]" class="form-control form-control-sm" min="0"
                               value="<?= esc($stop['estimated_arrival_offset_minutes'] ?? '') ?>"
                               placeholder="Optional">
                    </div>
                    <div class="col-md-2 text-end">
                        <button type="button" class="btn btn-sm btn-outline-danger route-remove-stop" title="Remove stop">
                            <i class="fa-solid fa-trash"></i>
                        </button>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    <template id="route-stop-template">
        <div class="route-stop-row border rounded p-3 mb-2">
            <div class="row g-2 align-items-end">
                <div class="col-md-7">
                    <label class="enterprise-field-label small">Location</label>
                    <select name="stop_location_id[]" class="form-select form-select-sm">
                        <option value="">Select stop location</option>
                        <?php foreach ($locations as $loc): ?>
                            <option value="<?= (int) $loc['id'] ?>">
                                <?= esc($loc['name']) ?><?= ! empty($loc['code']) ? ' (' . esc($loc['code']) . ')' : '' ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="enterprise-field-label small">Offset (min)</label>
                    <input type="number" name="stop_offset_minutes[]" class="form-control form-control-sm" min="0"
                           placeholder="Optional">
                </div>
                <div class="col-md-2 text-end">
                    <button type="button" class="btn btn-sm btn-outline-danger route-remove-stop" title="Remove stop">
                        <i class="fa-solid fa-trash"></i>
                    </button>
                </div>
            </div>
        </div>
    </template>
</div>
