@extends('layouts.admin')

@section('title', 'Top Products Report')

@section('content')
<x-admin.report-page title="Top Products Report" subtitle="Best-selling products by units sold and revenue.">
    <form method="GET" action="{{ route('admin.reports.top-products') }}" class="ci-filters ci-filters--report">
        @include('admin.reports.partials.location-filter', ['ci' => true])
        <div class="ci-filters__field">
            <label for="limit">Limit</label>
            <select name="limit" id="limit">
                @foreach([10, 20, 50, 100] as $n)
                    <option value="{{ $n }}" @selected((int) ($limit ?? 20) === $n)>Top {{ $n }}</option>
                @endforeach
            </select>
        </div>
        <div class="ci-filters__actions">
            <button type="submit" class="ci-btn ci-btn--primary">Apply</button>
        </div>
    </form>

    <div class="ci-table-card">
        <div class="ci-table-wrap">
            <table class="ci-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Product Name</th>
                        <th>Total Sold</th>
                        <th class="ci-end">Total Revenue</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($topProducts as $index => $product)
                        <tr>
                            <td class="ci-num">{{ $index + 1 }}</td>
                            <td>
                                <div class="ci-entity">
                                    <span class="ci-avatar">{{ strtoupper(mb_substr($product->name ?? 'P', 0, 2)) }}</span>
                                    <div class="ci-entity__meta">
                                        <p class="ci-entity__name">{{ $product->name }}</p>
                                    </div>
                                </div>
                            </td>
                            <td class="ci-num">{{ $product->total_sold }}</td>
                            <td class="ci-end"><span class="ci-money">₹{{ number_format($product->total_revenue, 2) }}</span></td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="4">
                                <div class="ci-empty">
                                    <i class="bi bi-star"></i>
                                    <h3>No Products Found</h3>
                                    <p>No product sales data available for the selected filters.</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</x-admin.report-page>
@endsection
