@php
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('recruitment.reports.title'), 'url' => route('hr.recruitment.reports.index')],
        ['label' => __($report['title_key'])],
    ];
    $q = $filters->toQuery();
@endphp

<x-ui.layout.page
    :title="__($report['title_key'])"
    :subtitle="__('recruitment.reports.show_subtitle')"
    page-icon="fe fe-bar-chart-2"
    :breadcrumbs="$breadcrumbs"
>
    <x-slot:actions>
        <x-ui.buttons.secondary :href="route('hr.recruitment.reports.index', $q)" icon="fe fe-arrow-left">
            {{ __('recruitment.common.back') }}
        </x-ui.buttons.secondary>
        <x-ui.buttons.secondary :href="route('hr.recruitment.reports.export.excel', array_merge(['report' => $type], $q))" icon="fe fe-download">
            {{ __('recruitment.reports.actions.export_excel') }}
        </x-ui.buttons.secondary>
        <x-ui.buttons.secondary :href="route('hr.recruitment.reports.export.pdf', array_merge(['report' => $type], $q))" icon="fe fe-file">
            {{ __('recruitment.reports.actions.export_pdf') }}
        </x-ui.buttons.secondary>
    </x-slot:actions>

    @include('pages.hr.recruitment.reports._filters', [
        'action' => route('hr.recruitment.reports.show', $type),
        'filters' => $filters,
        'filterOptions' => $filterOptions,
        'showStatus' => true,
    ])

    @if (! empty($report['summary']))
        <div class="row g-3 mb-4">
            @foreach ($report['summary'] as $key => $value)
                <x-ui.dashboard.stat-card
                    :label="__('recruitment.reports.summary.'.$key)"
                    :value="(string) $value"
                    icon="fe fe-activity"
                />
            @endforeach
        </div>
    @endif

    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <h6 class="mb-3">{{ __('recruitment.reports.saved_filters') }}</h6>
            <div class="d-flex flex-wrap gap-2 mb-3">
                @forelse ($savedFilters as $sf)
                    <a href="{{ route('hr.recruitment.reports.filters.apply', $sf) }}" class="btn btn-sm btn-outline-primary">
                        {{ $sf->name }}@if($sf->is_default) ★@endif
                    </a>
                    <form method="POST" action="{{ route('hr.recruitment.reports.filters.destroy', $sf) }}" class="d-inline">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-sm btn-outline-danger">×</button>
                    </form>
                @empty
                    <span class="text-muted small">{{ __('recruitment.reports.empty_saved_filters') }}</span>
                @endforelse
            </div>
            <form method="POST" action="{{ route('hr.recruitment.reports.filters.store') }}" class="row g-2 align-items-end">
                @csrf
                <input type="hidden" name="report_type" value="{{ $type }}">
                @foreach ($q as $k => $v)
                    <input type="hidden" name="{{ $k }}" value="{{ $v }}">
                @endforeach
                <div class="col-md-4">
                    <x-ui.form.input name="name" :label="__('recruitment.reports.fields.filter_name')" required />
                </div>
                <div class="col-md-2">
                    <div class="form-check mt-4">
                        <input class="form-check-input" type="checkbox" name="is_default" value="1" id="is_default">
                        <label class="form-check-label" for="is_default">{{ __('recruitment.reports.fields.is_default') }}</label>
                    </div>
                </div>
                <div class="col-md-3">
                    <x-ui.buttons.primary type="submit" size="sm">{{ __('recruitment.reports.actions.save_filter') }}</x-ui.buttons.primary>
                </div>
            </form>
        </div>
    </div>

    @can('exports.access')
        <form method="POST" action="{{ route('exports.store') }}" class="mb-3">
            @csrf
            <input type="hidden" name="type" value="recruitment_report">
            <input type="hidden" name="filters[report_type]" value="{{ $type }}">
            @foreach ($q as $k => $v)
                <input type="hidden" name="filters[{{ $k }}]" value="{{ $v }}">
            @endforeach
            <x-ui.buttons.secondary type="submit" size="sm" icon="fe fe-database">
                {{ __('recruitment.reports.actions.queue_export') }}
            </x-ui.buttons.secondary>
        </form>
    @endcan

    <div class="card border-0 shadow-sm">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        @foreach ($report['columns'] as $col)
                            <th>{{ __('recruitment.reports.columns.'.$col) }}</th>
                        @endforeach
                    </tr>
                </thead>
                <tbody>
                    @forelse ($report['rows'] as $row)
                        <tr>
                            @foreach ($row as $cell)
                                <td>{{ $cell }}</td>
                            @endforeach
                        </tr>
                    @empty
                        <tr>
                            <td colspan="{{ count($report['columns']) }}" class="text-center text-muted py-4">
                                {{ __('recruitment.reports.empty') }}
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-ui.layout.page>
