<?php
$schedule = $schedule ?? [];
$routes   = $routes ?? [];
$vehicles = $vehicles ?? [];
$drivers  = $drivers ?? [];
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field enterprise-field--full">
        <label class="enterprise-field-label">Title <span class="text-danger">*</span></label>
        <input type="text" name="title" class="form-control" maxlength="200" required
               value="<?= esc(old('title', $schedule['title'] ?? '')) ?>"
               placeholder="e.g. Morning depot run">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Route</label>
        <select name="route_id" class="form-select">
            <option value="">Select route</option>
            <?php foreach ($routes as $r): ?>
                <option value="<?= (int) $r['id'] ?>"
                    <?= (string) old('route_id', $schedule['route_id'] ?? '') === (string) $r['id'] ? 'selected' : '' ?>>
                    <?= esc($r['name']) ?><?= ! empty($r['code']) ? ' (' . esc($r['code']) . ')' : '' ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Vehicle</label>
        <select name="vehicle_id" class="form-select">
            <option value="">Select vehicle</option>
            <?php foreach ($vehicles as $v): ?>
                <option value="<?= (int) $v['id'] ?>"
                    <?= (string) old('vehicle_id', $schedule['vehicle_id'] ?? '') === (string) $v['id'] ? 'selected' : '' ?>>
                    <?= esc($v['registration_number']) ?>
                    <?php if (! empty($v['make']) || ! empty($v['model'])): ?>
                        — <?= esc(trim(($v['make'] ?? '') . ' ' . ($v['model'] ?? ''))) ?>
                    <?php endif; ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Driver</label>
        <select name="driver_id" class="form-select">
            <option value="">Select driver</option>
            <?php foreach ($drivers as $d): ?>
                <option value="<?= (int) $d['id'] ?>"
                    <?= (string) old('driver_id', $schedule['driver_id'] ?? '') === (string) $d['id'] ? 'selected' : '' ?>>
                    <?= esc($d['employee_name'] ?? $d['number']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Schedule Date <span class="text-danger">*</span></label>
        <input type="date" name="schedule_date" class="form-control" required
               value="<?= esc(old('schedule_date', $schedule['schedule_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Departure Time</label>
        <input type="time" name="departure_time" class="form-control"
               value="<?= esc(old('departure_time', $schedule['departure_time'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Arrival Time (outbound)</label>
        <input type="time" name="arrival_time" class="form-control"
               value="<?= esc(old('arrival_time', $schedule['arrival_time'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Shift Start</label>
        <input type="time" name="shift_start_time" class="form-control"
               value="<?= esc(old('shift_start_time', $schedule['shift_start_time'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Shift End</label>
        <input type="time" name="shift_end_time" class="form-control"
               value="<?= esc(old('shift_end_time', $schedule['shift_end_time'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Return Departure</label>
        <input type="time" name="return_departure_time" class="form-control"
               value="<?= esc(old('return_departure_time', $schedule['return_departure_time'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Return Arrival (back at base)</label>
        <input type="time" name="return_arrival_time" class="form-control"
               value="<?= esc(old('return_arrival_time', $schedule['return_arrival_time'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Priority</label>
        <select name="priority" class="form-select">
            <?php foreach (['low', 'normal', 'high', 'urgent'] as $p): ?>
                <option value="<?= $p ?>" <?= old('priority', $schedule['priority'] ?? 'normal') === $p ? 'selected' : '' ?>>
                    <?= esc(ucfirst($p)) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['planned', 'scheduled'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $schedule['status'] ?? 'planned') === $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', $schedule['notes'] ?? '')) ?></textarea>
    </div>
</div>
