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

<x-ui.layout.page
    :title="__('inventory.warehouses.title')"
    :subtitle="__('inventory.warehouses.subtitle')"
    page-icon="fe fe-home"
    :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' => 'is_active',
                            'label' => __('common.active'),
                            'options' => ['' => __('common.all'), '1' => __('common.active'), '0' => __('common.inactive')],
                            'width' => '9rem',
                        ],
                    ]" />
                </x-slot:filters>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('inventory.warehouses.create')"
                        :label="__('inventory.warehouses.actions.add')"
                        permission="inventory.warehouses.manage"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('inventory.warehouses.fields.code'), 'sort' => 'code'],
            ['label' => __('inventory.warehouses.fields.name'), 'sort' => 'name'],
            ['label' => __('inventory.warehouses.fields.lifecycle')],
            ['label' => __('inventory.warehouses.fields.active'), 'sort' => 'is_active'],
            ['label' => __('inventory.warehouses.fields.sections')],
            ['label' => __('common.view'), 'align' => 'end', 'width' => '100px'],
        ]">
            @forelse ($warehouses as $warehouse)
                <x-ui.table.row>
                    <td><strong>{{ $warehouse->code }}</strong></td>
                    <td>{{ $warehouse->name }}</td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="(string) ($warehouse->lifecycle_state?->value ?? $warehouse->lifecycle_state)"
                            variant="info"
                        />
                    </td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="$warehouse->is_active ? __('common.active') : __('common.inactive')"
                            :variant="$warehouse->is_active ? 'success' : 'neutral'"
                        />
                    </td>
                    <td>{{ $warehouse->sections->count() }}</td>
                    <td class="text-end">
                        <x-ui.buttons.secondary :href="route('inventory.warehouses.show', $warehouse)">
                            {{ __('common.view') }}
                        </x-ui.buttons.secondary>
                    </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="$warehouses" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>
</x-ui.layout.page>
