@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('shifts.title')],
    ];
    $emptyTitle = request()->hasAny(['q', 'type', 'is_active', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('shifts.empty');
@endphp

<x-ui.layout.page
    :title="__('shifts.title')"
    :subtitle="__('shifts.subtitle')"
    page-icon="fe fe-clock"
    :breadcrumbs="$breadcrumbs"
>
    <x-ui.operational.index-shell>
        <x-slot:toolbar>
            <x-ui.operational.action-bar>
                <x-slot:filters>
                    <x-ui.operational.index-filters :fields="[
                        [
                            'type' => 'search',
                            'name' => 'q',
                            'label' => __('common.search'),
                            'width' => '14rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'type',
                            'label' => __('shifts.fields.type'),
                            'options' => ['' => __('common.all')] + collect(['fixed', 'flexible', 'rotational', 'factory', 'split', 'night', 'seasonal'])
                                ->mapWithKeys(fn ($type) => [$type => __('shifts.types.'.$type)])->all(),
                            'width' => '10rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'is_active',
                            'label' => __('shifts.fields.status'),
                            'options' => [
                                '' => __('common.all'),
                                '1' => __('shifts.status.active'),
                                '0' => __('shifts.status.inactive'),
                            ],
                            'width' => '9rem',
                        ],
                    ]" />
                </x-slot:filters>
                <x-slot:secondary>
                    @can('shifts.view')
                        <x-ui.buttons.secondary :href="route('hr.shifts.assignments.index')" icon="fe fe-list">
                            {{ __('shifts.actions.assignments') }}
                        </x-ui.buttons.secondary>
                    @endcan
                    @can('shifts.assign')
                        <x-ui.buttons.secondary :href="route('hr.shifts.assignments.create')" icon="fe fe-user-plus">
                            {{ __('shifts.actions.assign') }}
                        </x-ui.buttons.secondary>
                    @endcan
                </x-slot:secondary>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('hr.shifts.create')"
                        :label="__('shifts.actions.create')"
                        permission="shifts.create"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('shifts.fields.name'), 'sort' => 'name'],
            ['label' => __('shifts.fields.start_time')],
            ['label' => __('shifts.fields.end_time')],
            ['label' => __('shifts.fields.working_days')],
            ['label' => __('shifts.fields.status'), 'sort' => 'is_active'],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '200px'],
        ]">
            @forelse ($shifts as $shift)
                <x-ui.table.row>
                    <td><strong>{{ $shift->name }}</strong></td>
                    <td><code>{{ \Illuminate\Support\Str::substr((string) ($shift->getAttributes()['start_time'] ?? ''), 0, 5) }}</code></td>
                    <td><code>{{ \Illuminate\Support\Str::substr((string) ($shift->getAttributes()['end_time'] ?? ''), 0, 5) }}</code></td>
                    <td class="small">{{ implode(', ', $shift->working_days ?? []) }}</td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="$shift->is_active ? __('shifts.status.active') : __('shifts.status.inactive')"
                            :variant="$shift->is_active ? 'success' : 'neutral'"
                        />
                    </td>
                    <td class="text-end">
                        <x-ui.action-modal.trigger
                            :entity="__('shifts.title')"
                            :record="$shift->name"
                            :edit-url="auth()->user()?->can('shifts.update', $shift) ? route('hr.shifts.edit', $shift) : null"
                            :activate-url="(! $shift->is_active && auth()->user()?->can('shifts.update', $shift)) ? route('hr.shifts.reactivate', $shift) : null"
                            :deactivate-url="($shift->is_active && auth()->user()?->can('shifts.deactivate', $shift)) ? route('hr.shifts.deactivate', $shift) : null"
                            :deactivate-message="__('common.actions_modal.status_message')"
                        />
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="6" class="p-0">
                        <x-ui.operational.empty-state :title="$emptyTitle" />
                    </td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>

        <x-slot:pagination>
            <x-ui.operational.pagination :paginator="$shifts" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>

    <x-ui.action-modal />

    @push('scripts')
        @vite(['resources/js/modules/hr/shifts/main.js'])
    @endpush
</x-ui.layout.page>
