@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('payroll.title'), 'url' => route('payroll.periods.index')],
        ['label' => __('payslips.index_title')],
    ];
    $emptyTitle = request()->hasAny(['q', 'period_id', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('payslips.empty');
    $periodOptions = ['' => __('common.all')] + $periods->pluck('name', 'id')->all();
@endphp

<x-ui.layout.page
    :title="__('payslips.index_title')"
    :subtitle="__('payslips.index_subtitle')"
    page-icon="fe fe-file-text"
    :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'),
                            'placeholder' => __('payslips.fields.employee'),
                            'width' => '16rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'period_id',
                            'label' => __('payslips.fields.period'),
                            'options' => $periodOptions,
                            'width' => '14rem',
                        ],
                    ]" />
                </x-slot:filters>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('payslips.fields.employee')],
            ['label' => __('payslips.fields.period')],
            ['label' => __('payslips.fields.generated_at'), 'sort' => 'generated_at'],
            ['label' => __('payslips.fields.gross_amount'), 'sort' => 'gross_amount'],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '140px'],
        ]">
            @forelse ($snapshots as $snap)
                <x-ui.table.row>
                    <td>
                        <strong>{{ $snap->employee?->full_name ?? '—' }}</strong>
                        <div class="small text-muted"><code>{{ $snap->employee?->employee_code ?? '—' }}</code></div>
                    </td>
                    <td>{{ $snap->period?->name ?? '—' }}</td>
                    <td>{{ $snap->generated_at?->format('Y-m-d H:i') ?? '—' }}</td>
                    <td>{{ $snap->gross_amount ?? '—' }}</td>
                    <td class="text-end">
                        <x-ui.buttons.secondary :href="route('payroll.payslips.show', $snap->id)" size="sm" icon="fe fe-eye">
                            {{ __('common.view') }}
                        </x-ui.buttons.secondary>
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="5" 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="$snapshots" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>
</x-ui.layout.page>
