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

<x-ui.layout.page
    :title="__('plans.title')"
    :subtitle="__('plans.subtitle')"
    page-icon="fe fe-package"
    :breadcrumbs="$breadcrumbs"
>
    <x-slot:actions>
        @can('plans.create')
            <x-ui.buttons.primary :href="route('admin.plans.create')" icon="fe fe-plus">
                {{ __('plans.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' => __('tenants.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' => __('plans.fields.name'), 'sort' => 'name'],
            ['label' => __('plans.fields.price'), 'sort' => 'price'],
            ['label' => __('plans.fields.cycle')],
            ['label' => __('plans.fields.modules')],
            ['label' => __('plans.fields.tenants')],
            ['label' => __('plans.fields.flags')],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '120px'],
        ]">
        @forelse ($plans as $plan)
            <x-ui.table.row>
                <td>
                    <strong>{{ $plan->name }}</strong><br>
                    <small class="text-muted">{{ $plan->slug }}</small>
                </td>
                <td>{{ number_format((float) $plan->price, 2) }} {{ $plan->currency }}</td>
                <td>{{ __('plans.cycles.'.$plan->billing_cycle) }}</td>
                <td>
                    @forelse ($plan->modules as $module)
                        <span class="badge bg-primary-subtle text-primary">{{ $module->name }}</span>
                    @empty
                        <span class="text-muted">—</span>
                    @endforelse
                </td>
                <td>{{ $plan->tenants_count ?? 0 }}</td>
                <td>
                    @if ($plan->is_trial)
                        <span class="badge bg-warning-subtle text-warning">{{ __('plans.trial') }}</span>
                    @endif
                    @if ($plan->is_default)
                        <span class="badge bg-info-subtle text-info">{{ __('plans.default') }}</span>
                    @endif
                    @if ($plan->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="__('plans.singular')"
                        :record="$plan->name"
                        :edit-url="auth()->user()?->can('plans.update', $plan) ? route('admin.plans.edit', $plan) : null"
                        :delete-url="auth()->user()?->can('plans.delete', $plan) ? route('admin.plans.destroy', $plan) : null"
                    />
                </td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row>
                <td colspan="7" 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="$plans" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>

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