<x-admin-layout title="Vehicles">
    <div class="card">
        <div class="card-body">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h5 class="fw-bold mb-0">Vehicles</h5>
                @if (auth()->user()->hasPermission('vehicles.manage'))
                    <a href="{{ route('admin.vehicles.create') }}" class="btn btn-dark btn-sm">
                        <i class="bi bi-plus-lg me-1"></i>Add Vehicle
                    </a>
                @endif
            </div>

            @if ($vehicles->isEmpty())
                <div class="text-center text-muted py-5">
                    <i class="bi bi-car-front-fill fs-1 d-block mb-2"></i>
                    No vehicles yet.
                </div>
            @else
                <div class="table-responsive">
                    <table class="table align-middle datatable">
                        <thead>
                            <tr>
                                <th>Registration</th>
                                <th>Type</th>
                                <th>Vehicle</th>
                                <th>Driver</th>
                                <th>Status</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($vehicles as $vehicle)
                                <tr>
                                    <td class="fw-semibold">{{ $vehicle->registration_number }}</td>
                                    <td>{{ $vehicle->vehicleType->name ?? '—' }}</td>
                                    <td>{{ trim("{$vehicle->make} {$vehicle->model}") ?: '—' }}</td>
                                    <td>{{ $vehicle->driver->full_name ?? 'Unassigned' }}</td>
                                    <td>
                                        @php
                                            $statusColor = ['active' => 'is-green', 'pending' => 'is-amber', 'expired' => 'is-rose'][$vehicle->status] ?? 'is-blue';
                                        @endphp
                                        <span class="badge {{ $statusColor }}">{{ ucfirst($vehicle->status) }}</span>
                                    </td>
                                    <td class="text-end">
                                        @if (auth()->user()->hasPermission('vehicles.manage'))
                                            <a href="{{ route('admin.vehicles.edit', $vehicle) }}" class="btn btn-sm btn-outline-secondary">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                            <form method="POST" action="{{ route('admin.vehicles.destroy', $vehicle) }}" class="d-inline"
                                                  data-confirm-title="Delete vehicle?" data-confirm-text="Delete {{ $vehicle->registration_number }}? This cannot be undone."
                                                  data-confirm-button="Delete">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></button>
                                            </form>
                                        @else
                                            <span class="text-muted small">—</span>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        </div>
    </div>
</x-admin-layout>
