@php
    $title = __('organization.entities.'.$entity);
    $permissionPrefix = str_replace('-', '_', $route);
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => $title],
    ];
    $columns = [
        ['label' => __('organization.fields.code'), 'sort' => 'code'],
        ['label' => __('organization.fields.name'), 'sort' => 'name'],
    ];
    if ($entity === 'job_grade') {
        $columns[] = ['label' => __('organization.fields.level'), 'sort' => 'level'];
        $columns[] = ['label' => __('organization.fields.min_salary'), 'sort' => 'min_salary'];
        $columns[] = ['label' => __('organization.fields.max_salary'), 'sort' => 'max_salary'];
    }
    $columns[] = ['label' => __('organization.fields.status'), 'sort' => 'is_active'];
    $columns[] = ['label' => __('common.actions'), 'align' => 'end', 'width' => '200px'];
    $colspan = count($columns);
    $emptyTitle = request()->hasAny(['q', 'is_active', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('organization.empty');
@endphp

<x-ui.layout.page
    :title="$title"
    :subtitle="__('organization.subtitle')"
    page-icon="fe fe-grid"
    :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' => __('common.search'),
                            'width' => '16rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'is_active',
                            'label' => __('organization.fields.status'),
                            'options' => [
                                '' => __('common.all'),
                                '1' => __('organization.status.active'),
                                '0' => __('organization.status.inactive'),
                            ],
                            'width' => '10rem',
                        ],
                    ]" />
                </x-slot:filters>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('hr.'.$route.'.create')"
                        :label="__('organization.actions.create', ['entity' => __('organization.entities.'.$entity)])"
                        :permission="$permissionPrefix.'.create'"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="$columns">
            @forelse ($records as $record)
                <x-ui.table.row>
                    <td><code>{{ $record->code }}</code></td>
                    <td><strong>{{ $record->name }}</strong></td>
                    @if ($entity === 'job_grade')
                        <td>{{ $record->level }}</td>
                        <td>{{ $record->min_salary !== null ? number_format((float) $record->min_salary, 2) : '—' }}</td>
                        <td>{{ $record->max_salary !== null ? number_format((float) $record->max_salary, 2) : '—' }}</td>
                    @endif
                    <td>
                        <x-ui.operational.status-badge
                            :label="$record->is_active ? __('organization.status.active') : __('organization.status.inactive')"
                            :variant="$record->is_active ? 'success' : 'muted'"
                        />
                    </td>
                    <td class="text-end">
                        <x-ui.action-modal.trigger
                            :entity="__('organization.entities.'.$entity)"
                            :record="$record->name"
                            :edit-url="auth()->user()?->can($permissionPrefix.'.update') ? route('hr.'.$route.'.edit', $record) : null"
                            :deactivate-url="($record->is_active && auth()->user()?->can($permissionPrefix.'.deactivate')) ? route('hr.'.$route.'.deactivate', $record) : null"
                            :deactivate-message="__('common.actions_modal.status_message')"
                            :deactivate-hint="__('organization.confirm.deactivate')"
                        />
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="{{ $colspan }}">
                        <x-ui.operational.empty-state
                            :title="$emptyTitle"
                            :body="__('organization.empty_hint')"
                            :action-href="auth()->user()?->can($permissionPrefix.'.create') ? route('hr.'.$route.'.create') : null"
                            :action-label="auth()->user()?->can($permissionPrefix.'.create') ? __('organization.actions.create', ['entity' => __('organization.entities.'.$entity)]) : null"
                        />
                    </td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>

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

    <x-ui.action-modal />
</x-ui.layout.page>
