@extends('layouts.portal')

@section('title', 'Item FnB terlaris — '.config('portal.product_line'))
@section('page_heading', 'Laporan — Item FnB terlaris')
@section('page_subheading', 'Menu diurutkan dari jumlah terjual (qty) pada periode terpilih. Data dari SysOrder (POS).')

@section('content')
    @include('owner.reports._nav')
    @include('owner.reports._filters')

    @if (!empty($data['errors']))
        <div class="mb-6 rounded-xl border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-950" role="alert">
            <p class="font-semibold text-amber-900">Perlu perhatian</p>
            <ul class="mt-2 list-disc space-y-1 pl-5">
                @foreach ($data['errors'] as $err)
                    <li><span class="font-medium">{{ $err['source'] }}</span>: {{ $err['message'] }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
        <div class="border-b border-slate-100 px-4 py-3 md:px-5">
            <h2 class="text-sm font-semibold text-slate-900">Detail item</h2>
            <p class="mt-0.5 text-xs text-slate-500">Total {{ $itemsPaginator->total() }} SKU · halaman {{ $itemsPaginator->currentPage() }} dari {{ max(1, $itemsPaginator->lastPage()) }}.</p>
        </div>
        @if ($itemsPaginator->total() > 0)
            <div class="overflow-x-auto">
                <table class="min-w-full text-sm">
                    <thead class="bg-slate-50 text-left text-xs uppercase text-slate-600">
                        <tr>
                            <th class="px-4 py-3">#</th>
                            <th class="px-4 py-3">Nama menu</th>
                            <th class="px-4 py-3 text-right">Qty terjual</th>
                            <th class="px-4 py-3 text-right">Omzet</th>
                            <th class="px-4 py-3 text-right">HPP</th>
                            <th class="px-4 py-3 text-right">Laba kotor</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100">
                        @foreach ($itemsPaginator as $idx => $it)
                            <tr class="hover:bg-slate-50/80">
                                <td class="px-4 py-2.5 tabular-nums text-slate-500">{{ $itemsPaginator->firstItem() + $idx }}</td>
                                <td class="px-4 py-2.5 font-medium text-slate-800">{{ $it['product_name'] ?? '—' }}</td>
                                <td class="px-4 py-2.5 text-right tabular-nums">{{ number_format($it['qty_sold'] ?? 0, 0, ',', '.') }}</td>
                                <td class="px-4 py-2.5 text-right tabular-nums">Rp {{ number_format($it['revenue'] ?? 0, 0, ',', '.') }}</td>
                                <td class="px-4 py-2.5 text-right tabular-nums text-slate-600">Rp {{ number_format($it['cogs'] ?? 0, 0, ',', '.') }}</td>
                                <td class="px-4 py-2.5 text-right tabular-nums font-medium text-emerald-700">Rp {{ number_format($it['gross_profit'] ?? 0, 0, ',', '.') }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            <div class="border-t border-slate-100 px-4 py-3">
                {{ $itemsPaginator->withQueryString()->links('vendor.pagination.tailwind') }}
            </div>
        @else
            <div class="px-4 py-10 text-center text-sm text-slate-500">Tidak ada data item. Pastikan backend SysOrder (POS) sudah di-upgrade dan API key punya akses laporan.</div>
        @endif
    </div>
@endsection
