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

<x-ui.layout.page
    :title="__('modules.title')"
    :subtitle="__('modules.subtitle')"
    page-icon="fe fe-grid"
    :breadcrumbs="$breadcrumbs"
>
    <x-slot:actions>
        @can('modules.create')
            <x-ui.buttons.primary :href="route('admin.modules.create')" icon="fe fe-plus">
                {{ __('modules.actions.add') }}
            </x-ui.buttons.primary>
        @endcan
    </x-slot:actions>

    <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' => 'is_active',
                            'label' => __('modules.fields.status'),
                            'options' => ['' => __('common.all'), '1' => __('common.active'), '0' => __('common.inactive')],
                            'width' => '9rem',
                        ],
                    ]" />
                </x-slot:filters>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('modules.fields.name'), 'sort' => 'name'],
            ['label' => __('modules.fields.slug'), 'sort' => 'slug'],
            ['label' => __('modules.fields.description')],
            ['label' => __('modules.fields.status'), 'sort' => 'is_active'],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '120px'],
        ]">
        @forelse ($modules as $module)
            <x-ui.table.row>
                <td><strong>{{ $module->name }}</strong></td>
                <td><code>{{ $module->slug }}</code></td>
                <td class="text-muted">{{ $module->description ?? '—' }}</td>
                <td>
                    @if ($module->is_active)
                        <span class="badge bg-success">{{ __('common.active') }}</span>
                    @else
                        <span class="badge bg-secondary">{{ __('common.inactive') }}</span>
                    @endif
                </td>
                <td class="text-end">
                    <x-ui.action-modal.trigger
                        :entity="__('modules.singular')"
                        :record="$module->name"
                        :edit-url="auth()->user()?->can('modules.update', $module) ? route('admin.modules.edit', $module) : null"
                        :delete-url="auth()->user()?->can('modules.delete', $module) ? route('admin.modules.destroy', $module) : null"
                    />
                </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="$modules" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>

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