@extends('layouts.app')

@section('title', $report['title'])

@section('content')
    <x-breadcrumbs :items="[
        ['label' => 'Dashboard', 'url' => route('dashboard')],
        ['label' => 'Reports', 'url' => route('reports.index')],
        ['label' => $report['title']],
    ]" />

    <x-page-header :title="$report['title']" subtitle="Generated report data">
        @if (auth()->user()?->hasPermission('reports.export'))
            <div class="btn-group btn-group-sm">
                <a href="{{ route('reports.export', ['type' => $type, 'format' => 'csv'] + $filters) }}" class="btn btn-outline-secondary">CSV</a>
                <a href="{{ route('reports.export', ['type' => $type, 'format' => 'pdf'] + $filters) }}" class="btn btn-outline-secondary">PDF</a>
                <a href="{{ route('reports.export', ['type' => $type, 'format' => 'print'] + $filters) }}" class="btn btn-outline-secondary" target="_blank">Print</a>
            </div>
        @endif
    </x-page-header>

    <div class="vem-filter-bar mb-3">
        <form method="GET" action="{{ route('reports.show', $type) }}" class="row g-3 align-items-end">
            <div class="col-md-3">
                <label class="form-label" for="date_from">From</label>
                <input type="date" name="date_from" id="date_from" class="form-control form-control-sm" value="{{ $filters['date_from'] ?? '' }}">
            </div>
            <div class="col-md-3">
                <label class="form-label" for="date_to">To</label>
                <input type="date" name="date_to" id="date_to" class="form-control form-control-sm" value="{{ $filters['date_to'] ?? '' }}">
            </div>
            <div class="col-md-3 d-flex gap-2">
                <button type="submit" class="btn btn-primary btn-sm">Apply</button>
                <a href="{{ route('reports.show', $type) }}" class="btn btn-outline-secondary btn-sm">Reset</a>
            </div>
        </form>
    </div>

    <x-card>
        @if ($report['rows']->isEmpty())
            <x-empty-state icon="bar-chart" title="No data" description="No records match the selected filters." />
        @else
            <div class="table-responsive">
                <table class="table table-sm table-striped mb-0">
                    <thead>
                        <tr>
                            @foreach ($report['headers'] as $header)
                                <th>{{ $header }}</th>
                            @endforeach
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($report['rows'] as $row)
                            <tr>
                                @foreach ($report['headers'] as $header)
                                    <td>{{ is_array($row) ? ($row[$header] ?? '') : '' }}</td>
                                @endforeach
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            <p class="text-muted small mt-3 mb-0">{{ $report['rows']->count() }} row(s)</p>
        @endif
    </x-card>
@endsection
