@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')],
        ['label' => __('organization.fields.name')],
    ];
    if ($entity === 'job_grade') {
        $columns[] = ['label' => __('organization.fields.level')];
        $columns[] = ['label' => __('organization.fields.min_salary')];
        $columns[] = ['label' => __('organization.fields.max_salary')];
    }
    $columns[] = ['label' => __('organization.fields.status')];
    $columns[] = ['label' => __('common.actions'), 'align' => 'end', 'width' => '200px'];
    $colspan = count($columns);
@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: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">
                        <div class="d-inline-flex gap-2">
                            @can($permissionPrefix.'.update')
                                <x-ui.buttons.secondary size="sm" :href="route('hr.'.$route.'.edit', $record)">
                                    {{ __('common.edit') }}
                                </x-ui.buttons.secondary>
                            @endcan
                            @can($permissionPrefix.'.deactivate')
                                @if ($record->is_active)
                                    <form
                                        method="POST"
                                        action="{{ route('hr.'.$route.'.deactivate', $record) }}"
                                        onsubmit="return confirm(@js(__('organization.confirm.deactivate')));"
                                    >
                                        @csrf
                                        <x-ui.buttons.danger outline size="sm" type="submit">
                                            {{ __('organization.actions.deactivate') }}
                                        </x-ui.buttons.danger>
                                    </form>
                                @endif
                            @endcan
                        </div>
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="{{ $colspan }}">
                        <x-ui.operational.empty-state
                            :title="__('organization.empty')"
                            :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-ui.operational.index-shell>
</x-ui.layout.page>
