@extends('layouts.app')
@section('title', $route->name)
@section('page_title', $route->name)
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('transport-routes.index') }}">Routes</a></li><li class="breadcrumb-item active">{{ $route->name }}</li>
@endsection

@section('content')
<div class="row g-3">
    <div class="col-lg-7">
        <div class="card"><div class="card-header"><i class="bi bi-geo-alt me-1"></i>Stops ({{ $route->stops->count() }})</div><div class="card-body">
            <table class="table table-sm align-middle">
                <thead><tr><th>Stop</th><th class="text-end">Fare</th><th>Pickup</th><th></th></tr></thead>
                <tbody>
                    @forelse($route->stops as $stop)
                        <tr>
                            <td class="fw-semibold">{{ $stop->name }}</td>
                            <td class="text-end">{{ number_format($stop->fare) }}</td>
                            <td class="small">{{ $stop->pickup_time ? \Illuminate\Support\Carbon::parse($stop->pickup_time)->format('h:i A') : '—' }}</td>
                            <td class="text-end">@can('transport.delete')<form method="POST" action="{{ route('transport-stops.destroy', $stop) }}" class="d-inline" data-confirm="Remove stop?">@csrf @method('DELETE')<button class="btn btn-sm btn-light border text-danger"><i class="bi bi-x"></i></button></form>@endcan</td>
                        </tr>
                    @empty
                        <tr><td colspan="4" class="text-muted text-center py-3">No stops yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
            @can('transport.create')
                <form method="POST" action="{{ route('transport-routes.stops.store', $route) }}" class="row g-2 align-items-end border-top pt-3">
                    @csrf
                    <div class="col-5"><label class="form-label small">Stop name</label><input name="name" class="form-control form-control-sm" required></div>
                    <div class="col-3"><label class="form-label small">Fare</label><input type="number" step="0.01" name="fare" class="form-control form-control-sm" value="0"></div>
                    <div class="col-2"><label class="form-label small">Pickup</label><input type="time" name="pickup_time" class="form-control form-control-sm"></div>
                    <div class="col-2"><button class="btn btn-sm btn-primary w-100"><i class="bi bi-plus"></i></button></div>
                </form>
            @endcan
        </div></div>
    </div>
    <div class="col-lg-5">
        <div class="card"><div class="card-header"><i class="bi bi-bus-front me-1"></i>Vehicles on this route</div><div class="card-body">
            @forelse($route->vehicles as $v)
                <div class="border rounded p-2 mb-2">
                    <div class="fw-semibold">{{ $v->registration_no }} <span class="text-muted small">{{ $v->model }}</span></div>
                    <div class="small text-muted"><i class="bi bi-person me-1"></i>{{ $v->driver_name ?: 'No driver' }} · {{ $v->driver_phone ?: '—' }}</div>
                </div>
            @empty
                <p class="text-muted text-center py-2">No vehicles assigned. <a href="{{ route('vehicles.create') }}">Add one</a>.</p>
            @endforelse
        </div></div>
    </div>

    {{-- All students assigned to this route (any class) --}}
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-people me-1"></i>Students on this Route ({{ $route->students->count() }})</span>
                @can('transport.update')<a href="{{ route('transport-assign.index') }}" class="btn btn-sm btn-light border"><i class="bi bi-person-plus me-1"></i>Assign Students</a>@endcan
            </div>
            <div class="card-body">
                @if($route->students->isEmpty())
                    <p class="text-muted text-center py-3 mb-0">No students assigned to this route yet.</p>
                @else
                    @php $byClass = $route->students->groupBy(fn ($s) => $s->schoolClass?->name ?? 'Unassigned'); @endphp
                    <div class="small text-muted mb-2">
                        @foreach($byClass as $cls => $group)
                            <span class="badge bg-soft-primary me-1">{{ $cls }}: {{ $group->count() }}</span>
                        @endforeach
                    </div>
                    <div class="table-responsive">
                        <table class="table table-sm table-hover align-middle">
                            <thead><tr><th>#</th><th>Student</th><th>Class / Section</th><th>Stop</th><th class="text-end">Fare</th></tr></thead>
                            <tbody>
                                @foreach($route->students as $i => $s)
                                    <tr>
                                        <td class="small text-muted">{{ $i + 1 }}</td>
                                        <td><a href="{{ route('students.show', $s) }}" class="fw-semibold text-decoration-none">{{ $s->name }}</a>
                                            <span class="text-muted small">({{ $s->admission_number }})</span></td>
                                        <td class="small">{{ $s->schoolClass?->name ?? '—' }}{{ $s->section ? ' / '.$s->section->name : '' }}</td>
                                        <td class="small">{{ $s->transportStop?->name ?? '—' }}</td>
                                        <td class="text-end small">{{ number_format($s->transportStop?->fare ?? $route->fare) }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
