<x-admin-layout title="Driver Shifts">
    <div class="card">
        <div class="card-body">
            <h5 class="fw-bold mb-3">Driver Shifts</h5>

            @if ($shifts->isEmpty())
                <div class="text-center text-muted py-5">
                    <i class="bi bi-clock-history fs-1 d-block mb-2"></i>
                    No shifts recorded yet.
                </div>
            @else
                <div class="table-responsive">
                    <table class="table align-middle datatable">
                        <thead>
                            <tr>
                                <th>Driver</th>
                                <th>Vehicle</th>
                                <th>Started</th>
                                <th>Ended</th>
                                <th>Availability</th>
                                <th>Status</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($shifts as $shift)
                                <tr>
                                    <td class="fw-semibold">{{ $shift->driver->full_name ?? '—' }}</td>
                                    <td>{{ $shift->vehicle->registration_number ?? '—' }}</td>
                                    <td>{{ $shift->started_at->format('d M Y, H:i') }}</td>
                                    <td>{{ $shift->ended_at?->format('d M Y, H:i') ?? '—' }}</td>
                                    <td>
                                        @if (!$shift->ended_at)
                                            <span class="badge {{ $shift->is_available ? 'is-green' : 'is-amber' }}">
                                                {{ $shift->is_available ? 'Available' : 'Busy' }}
                                            </span>
                                        @else
                                            <span class="text-muted">—</span>
                                        @endif
                                    </td>
                                    <td>
                                        <span class="badge {{ $shift->ended_at ? 'is-rose' : 'is-blue' }}">
                                            {{ $shift->ended_at ? 'Ended' : 'Active' }}
                                        </span>
                                    </td>
                                    <td class="text-end">
                                        @if (!$shift->ended_at && auth()->user()->hasPermission('shifts.manage'))
                                            <form method="POST" action="{{ route('admin.shifts.force-end', $shift) }}" class="d-inline"
                                                  data-confirm-title="End shift for {{ $shift->driver->full_name }}?"
                                                  data-confirm-text="Any of their in-progress bookings will be automatically cancelled."
                                                  data-confirm-button="End Shift"
                                                  data-confirm-input-label="Reason (optional)">
                                                @csrf
                                                @method('PATCH')
                                                <input type="hidden" name="reason" class="confirm-input-target">
                                                <button type="submit" class="btn btn-sm btn-outline-danger">End Shift</button>
                                            </form>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        </div>
    </div>
</x-admin-layout>
