@php
    use App\Modules\Exports\Support\ExportStatus;

    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('exports.title')],
    ];
@endphp

<x-ui.layout.page
    :title="__('exports.title')"
    :subtitle="__('exports.subtitle')"
    page-icon="fe fe-download"
    :breadcrumbs="$breadcrumbs"
>
    <x-ui.operational.index-shell>
        <x-ui.table.table :columns="[
            ['label' => __('exports.columns.id')],
            ['label' => __('exports.columns.type')],
            ['label' => __('exports.columns.status')],
            ['label' => __('exports.columns.filters')],
            ['label' => __('exports.columns.rows')],
            ['label' => __('exports.columns.requested_by')],
            ['label' => __('exports.columns.created')],
            ['label' => __('exports.columns.actions'), 'align' => 'end', 'width' => '160px'],
        ]">
            @forelse ($paginator as $row)
                @php
                    $filterSummary = $row->filters ? \Illuminate\Support\Str::limit(json_encode($row->filters, JSON_UNESCAPED_UNICODE), 120) : '—';
                    $ev = match ($row->status) {
                        ExportStatus::COMPLETED => 'success',
                        ExportStatus::FAILED => 'danger',
                        ExportStatus::PROCESSING => 'warning',
                        default => 'neutral',
                    };
                @endphp
                <x-ui.table.row>
                    <td><code>{{ $row->id }}</code></td>
                    <td>{{ __('exports.types.'.$row->type) }}</td>
                    <td>
                        <x-ui.operational.status-badge :label="__('exports.status.'.$row->status)" :variant="$ev" />
                    </td>
                    <td class="small text-muted font-monospace">{{ $filterSummary }}</td>
                    <td>{{ $row->total_rows ?? '—' }}</td>
                    <td>{{ $row->requester?->name ?? $row->requester?->email ?? '—' }}</td>
                    <td>{{ $row->created_at?->format('Y-m-d H:i') }}</td>
                    <td class="text-end">
                        @if ($row->status === ExportStatus::COMPLETED && $row->file_path)
                            <x-ui.buttons.secondary :href="route('exports.download', $row->id)" icon="fe fe-download" size="sm">
                                {{ __('exports.actions.download') }}
                            </x-ui.buttons.secondary>
                        @else
                            <span class="text-muted small">—</span>
                        @endif
                    </td>
                </x-ui.table.row>
                @if ($row->status === ExportStatus::FAILED && $row->error_message)
                    <x-ui.table.row>
                        <td colspan="8" class="small py-2" style="color: {{ config('theme.ui.severity.danger') }};">
                            <strong>{{ __('exports.error') }}:</strong> {{ \Illuminate\Support\Str::limit($row->error_message, 500) }}
                        </td>
                    </x-ui.table.row>
                @endif
            @empty
                <x-ui.table.row>
                    <td colspan="8" class="p-0">
                        <x-ui.operational.empty-state :title="__('exports.empty')" />
                    </td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>

        <x-slot:pagination>
            @if ($paginator->hasPages())
                <div class="d-flex justify-content-center">{{ $paginator->links() }}</div>
            @endif
        </x-slot:pagination>
    </x-ui.operational.index-shell>
</x-ui.layout.page>
