{{--
    The one view every report renders through. Everything here is driven by
    $definition (columns, filters, sortable whitelist) and $rows (a
    paginator whose items are already the plain arrays $definition->row
    produced) — this file has never seen an Asset, an Employee or an
    AuditLog, and none of the twenty-two reports gets a view of its own.
--}}
@extends('layouts.app')

@section('title', $definition->title)

@section('content')
    <x-page-header :title="$definition->title" :subtitle="$definition->description">
        <x-slot:breadcrumbs>
            <li class="breadcrumb-item"><a href="{{ route('reports.index') }}">Reports</a></li>
            <li class="breadcrumb-item active" aria-current="page">{{ $definition->title }}</li>
        </x-slot:breadcrumbs>

        <a href="{{ route('reports.export.excel', array_merge(['report' => $definition->key], $filters)) }}" class="btn btn-outline-secondary">
            <i class="bi bi-file-earmark-excel" aria-hidden="true"></i>Export Excel
        </a>
        <a href="{{ route('reports.export.pdf', array_merge(['report' => $definition->key], $filters)) }}" class="btn btn-outline-secondary">
            <i class="bi bi-file-earmark-pdf" aria-hidden="true"></i>Export PDF
        </a>
        <button type="button" class="btn btn-outline-secondary" data-ams-print>
            <i class="bi bi-printer" aria-hidden="true"></i>Print
        </button>
    </x-page-header>

    <x-table.wrapper>
        <x-slot:toolbar>
            <x-filter-bar :action="route('reports.show', $definition->key)" :active="$hasFilters">
                @foreach ($definition->filters as $filter)
                    @if ($filter->type === \App\Support\Reports\ReportFilterType::Search)
                        <x-filter.search :value="$filters['search'] ?? null" :placeholder="$filter->placeholder" />
                    @elseif ($filter->type === \App\Support\Reports\ReportFilterType::Select)
                        <x-filter.select
                            :name="$filter->key"
                            :label="$filter->label"
                            :selected="$filters[$filter->key] ?? null"
                            :options="$filter->options"
                            :placeholder="$filter->placeholder" />
                    @elseif ($filter->type === \App\Support\Reports\ReportFilterType::DateRange)
                        <x-filter.date-range
                            :from="$filters['date_from'] ?? null"
                            :to="$filters['date_to'] ?? null"
                            :label="$filter->label" />
                    @endif
                @endforeach
            </x-filter-bar>
        </x-slot:toolbar>

        <x-slot:head>
            <tr>
                @foreach ($definition->columns as $column)
                    <th class="{{ $column->numeric ? 'text-end' : '' }}">
                        @if (array_key_exists($column->key, $definition->sortable))
                            <x-table.sort-link
                                :field="$column->key"
                                :label="$column->label"
                                :sort="$filters['sort'] ?? $definition->defaultSort"
                                :direction="$filters['direction'] ?? $definition->defaultDirection" />
                        @else
                            {{ $column->label }}
                        @endif
                    </th>
                @endforeach
            </tr>
        </x-slot:head>

        @forelse ($rows as $row)
            <tr>
                @foreach ($definition->columns as $column)
                    <td class="{{ $column->numeric ? 'text-end ams-mono' : '' }}">
                        {{ $row[$column->key] ?? '—' }}
                    </td>
                @endforeach
            </tr>
        @empty
            <tr>
                <td colspan="{{ count($definition->columns) }}" class="p-0">
                    <x-table.empty-state
                        :icon="$hasFilters ? 'funnel' : 'inbox'"
                        :title="$hasFilters ? $definition->emptyTitle : 'No data yet'"
                        :message="$hasFilters ? $definition->emptyMessage : 'This report will populate as data is added to the system.'" />
                </td>
            </tr>
        @endforelse

        <x-slot:footer>
            <x-table.count :paginator="$rows" />
            {{ $rows->links() }}
        </x-slot:footer>
    </x-table.wrapper>
@endsection
