@php($shiftOptions = $assignments->pluck('shift', 'shift_id')->filter())
<x-ui.layout.page :title="__('shifts.calendar.title')" page-icon="fe fe-calendar">
    <form method="GET" class="mb-3">
        <input type="month" name="month" value="{{ $month->format('Y-m') }}">
        <x-ui.buttons.secondary type="submit">{{ __('common.filter') }}</x-ui.buttons.secondary>
    </form>

    <x-ui.table.table :columns="[
        ['label' => __('shifts.fields.employee')],
        ['label' => __('shifts.fields.shift')],
        ['label' => __('shifts.fields.starts_at')],
        ['label' => __('shifts.fields.ends_at')],
    ]">
        @foreach ($assignments as $assignment)
            <x-ui.table.row>
                <td>{{ $assignment->employee?->full_name }}</td>
                <td>{{ $assignment->shift?->name }}</td>
                <td>{{ $assignment->starts_at?->format('Y-m-d') }}</td>
                <td>{{ $assignment->ends_at?->format('Y-m-d') ?? '—' }}</td>
            </x-ui.table.row>
        @endforeach
    </x-ui.table.table>

    @can('shifts.assign')
        <div class="card mt-4"><div class="card-body">
            <h3>{{ __('shifts.rotation.title') }}</h3>
            <form method="POST" action="{{ route('hr.shifts.rotations.store') }}">
                @csrf
                <input class="form-control mb-2" name="name" required placeholder="{{ __('shifts.fields.name') }}">
                <div class="row">
                    @foreach (range(1, 7) as $day)
                        <div class="col-md-3 mb-2">
                            <input type="hidden" name="days[{{ $day }}][day_number]" value="{{ $day }}">
                            <label>{{ __('shifts.rotation.day', ['day' => $day]) }}</label>
                            <select class="form-select" name="days[{{ $day }}][shift_id]">
                                <option value="">{{ __('shifts.rotation.day_off') }}</option>
                                @foreach ($shiftOptions as $shift)
                                    <option value="{{ $shift->id }}">{{ $shift->name }}</option>
                                @endforeach
                            </select>
                        </div>
                    @endforeach
                </div>
                <div class="row mb-2">
                    <div class="col-md-6">
                        <label>{{ __('shifts.rotation.employees') }}</label>
                        <select class="form-select" name="employee_ids[]" multiple>
                            @foreach ($assignments->pluck('employee', 'employee_id')->filter() as $employee)
                                <option value="{{ $employee->id }}">{{ $employee->full_name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label>{{ __('shifts.fields.starts_at') }}</label>
                        <input class="form-control" type="date" name="starts_at" value="{{ now()->toDateString() }}">
                    </div>
                </div>
                <x-ui.buttons.primary type="submit">{{ __('common.save') }}</x-ui.buttons.primary>
            </form>
            <ul class="mt-3">
                @foreach ($rotations as $rotation)
                    <li>{{ $rotation->name }} — {{ $rotation->days->count() }} {{ __('shifts.rotation.days') }}</li>
                @endforeach
            </ul>
        </div></div>

        <div class="card mt-4"><div class="card-body">
            <h3>{{ __('shifts.swap.title') }}</h3>
            <form method="POST" action="{{ route('hr.shifts.swaps.store') }}" class="row g-2">
                @csrf
                @foreach (['assignment_a', 'assignment_b'] as $field)
                    <div class="col-md-4">
                        <select class="form-select" name="{{ $field }}" required>
                            <option value="">{{ __('shifts.swap.select_assignment') }}</option>
                            @foreach ($assignments as $assignment)
                                <option value="{{ $assignment->id }}">
                                    {{ $assignment->employee?->full_name }} — {{ $assignment->shift?->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                @endforeach
                <div class="col-md-2"><input class="form-control" type="date" name="date" required></div>
                <div class="col-md-2"><x-ui.buttons.primary type="submit">{{ __('common.submit') }}</x-ui.buttons.primary></div>
            </form>
            @foreach ($swaps as $swap)
                <div class="mt-2">
                    #{{ $swap->id }} — {{ $swap->date->format('Y-m-d') }} — {{ $swap->status }}
                    @if ($swap->status === 'pending')
                        <form class="d-inline" method="POST" action="{{ route('hr.shifts.swaps.review', $swap) }}">
                            @csrf
                            <button class="btn btn-sm btn-success" name="approve" value="1">{{ __('common.approve') }}</button>
                            <button class="btn btn-sm btn-danger" name="approve" value="0">{{ __('common.reject') }}</button>
                        </form>
                    @endif
                </div>
            @endforeach
        </div></div>
    @endcan
</x-ui.layout.page>
