@extends('admin.layouts.app')

@section('content')
<div class="container-fluid px-4">
    @hook('ReportViewPreOutput')
    <x-admin.page-header
        :title="$report->name"
        :subtitle="$report->description"
        icon="fas fa-chart-bar"
        :buttons="[
            ['label' => __('admin/common.edit'), 'route' => route('admin.reports.custom.edit', $report),
             'icon' => 'fas fa-edit', 'color' => 'primary',
             'visible' => auth('admin')->user()?->can('update', $report) ?? false],
            ['label' => __('admin/reports.export_csv'), 'route' => route('admin.reports.custom.export', $report),
             'icon' => 'fas fa-file-csv', 'color' => 'outline-success',
             'permission' => 'export custom reports'],
            ['label' => __('admin/common.back'), 'route' => route('admin.reports.custom.index'),
             'icon' => 'fas fa-arrow-left', 'color' => 'outline-secondary'],
        ]"
    />

    <x-admin.alert />

    @php
        $displayType = $report->ir['display']['type'] ?? 'table';
        $isChart = in_array($displayType, ['bar','line','pie']);
        // @json directive ifadeyi virgülden böler (3+ virgülde bozulur) —
        // payload'ı önce değişkene al, @json'a virgülsüz tek değişken ver.
        $reportPayload = $report->only(['id','uuid','name','description','is_public','owner_admin_id','last_executed_at']);
    @endphp

    @if($isChart)
    <x-admin.card class="mb-3">
        <x-slot:title>{{ __('admin/reports.chart_title') }} — {{ ucfirst($displayType) }}</x-slot:title>
        <canvas id="reportChart" style="max-height: 400px;"></canvas>
    </x-admin.card>
    @endif

    <x-admin.card>
        <x-slot:title>{{ __('admin/reports.table_title') }}</x-slot:title>
        <div class="table-responsive">
            <table class="table table-sm table-bordered" id="reportTable">
                <thead>
                    <tr id="reportTableHead">
                        
                    </tr>
                </thead>
                <tbody>
                    
                </tbody>
            </table>
        </div>
        <p class="small text-muted mb-0">
            {{ __('admin/reports.last_executed') }}:
            <strong>{{ $report->last_executed_at ? $report->last_executed_at->format('Y-m-d H:i:s') : '—' }}</strong>
        </p>
    </x-admin.card>

    @if(auth('admin')->user()?->can('togglePublic', $report))
    <div class="mt-3">
        <form method="POST" action="{{ route('admin.reports.custom.toggle-public', $report) }}" class="d-inline">
            @csrf @method('PATCH')
            <button type="submit" class="btn btn-sm btn-outline-{{ $report->is_public ? 'warning' : 'success' }}">
                {{ $report->is_public ? __('admin/reports.make_private') : __('admin/reports.make_public') }}
            </button>
        </form>
    </div>
    @endif

    @if(auth('admin')->user()?->can('delete', $report))
    <div class="mt-3">
        <form method="POST" action="{{ route('admin.reports.custom.destroy', $report) }}" class="d-inline js-delete-report-form">
            @csrf @method('DELETE')
            <button type="submit" class="btn btn-sm btn-outline-danger">
                <i class="fas fa-trash"></i> {{ __('admin/common.delete') }}
            </button>
        </form>
    </div>
    @endif
    @hook('ReportViewPostOutput')
</div>
@endsection

@push('scripts')
<script>
window.pageConfig = {
    routes: {
        records: '{{ route("api.admin.reports.records", $report) }}',
    },
    report: @json($reportPayload),
    translations: {
        confirm_delete: '{{ __("admin/reports.confirm_delete") }}',
    },
};
</script>
@vite('resources/js/admin/reports/show.js')
@endpush
