<div>
    <x-layouts.app title="{{ __('Product Sales Report') }}">
        <!-- Page Header -->
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="page-title">{{ __('Product Sales Report') }}</h1>
                <p class="text-muted mb-0">{{ __('Best sellers and product performance') }}</p>
            </div>
            <div class="d-flex gap-2">
                <button wire:click="export" class="btn btn-outline-success">
                    <i class="fas fa-file-excel me-2"></i>{{ __('Export') }}
                </button>
                <a href="{{ route('pdf.report.product-sales', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'category_id' => $categoryFilter]) }}" class="btn btn-outline-info">
                    <i class="fas fa-download me-2"></i>{{ __('Download PDF') }}
                </a>
                <a href="{{ route('pdf.report.product-sales', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'category_id' => $categoryFilter, 'preview' => true]) }}" target="_blank" class="btn btn-outline-secondary">
                    <i class="fas fa-print me-2"></i>{{ __('Print') }}
                </a>
            </div>
        </div>

        <!-- Summary Cards -->
        <div class="row g-3 mb-4">
            <div class="col-md-6">
                <div class="card bg-primary text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Total Quantity Sold') }}</h6>
                        <h2 class="mb-0">{{ number_format($totals['total_qty'], 0) }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card bg-success text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Total Revenue') }}</h6>
                        <h2 class="mb-0">{{ $currency }} {{ number_format($totals['total_revenue'], 0) }}</h2>
                    </div>
                </div>
            </div>
        </div>

        <!-- Filters -->
        <div class="card mb-4">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">{{ __('From Date') }}</label>
                        <input type="date" class="form-control" wire:model.live="dateFrom">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{{ __('To Date') }}</label>
                        <input type="date" class="form-control" wire:model.live="dateTo">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">{{ __('Category') }}</label>
                        <select class="form-select" wire:model.live="categoryFilter">
                            <option value="">{{ __('All') }}</option>
                            @foreach($categories as $category)
                                <option value="{{ $category->id }}">{{ $category->localized_name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- Products Table -->
        <div class="card">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>#</th>
                                <th>{{ __('Product') }}</th>
                                <th>{{ __('SKU') }}</th>
                                <th>{{ __('Category') }}</th>
                                <th class="text-center cursor-pointer" wire:click="sortBy('qty_sold')">
                                    {{ __('Qty Sold') }}
                                    @if($sortField === 'qty_sold')
                                        <i class="fas fa-sort-{{ $sortDirection === 'asc' ? 'up' : 'down' }} ms-1"></i>
                                    @endif
                                </th>
                                <th class="text-center">{{ __('Orders') }}</th>
                                <th class="text-end cursor-pointer" wire:click="sortBy('revenue')">
                                    {{ __('Revenue') }}
                                    @if($sortField === 'revenue')
                                        <i class="fas fa-sort-{{ $sortDirection === 'asc' ? 'up' : 'down' }} ms-1"></i>
                                    @endif
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($products as $index => $item)
                                <tr wire:key="prod-{{ $item->product_id }}">
                                    <td>
                                        @if($index < 3 && $sortDirection === 'desc' && $sortField === 'revenue')
                                            <span class="badge bg-{{ $index === 0 ? 'warning text-dark' : ($index === 1 ? 'secondary' : 'danger') }}">
                                                #{{ $index + 1 }}
                                            </span>
                                        @else
                                            <span class="text-muted">{{ $products->firstItem() + $index }}</span>
                                        @endif
                                    </td>
                                    <td>
                                        <strong>{{ $item->product?->localized_name }}</strong>
                                    </td>
                                    <td><code>{{ $item->product?->sku }}</code></td>
                                    <td>
                                        <span class="badge" style="background-color: {{ $item->product?->category?->color ?? '#6c757d' }}">
                                            {{ $item->product?->category?->localized_name ?? '-' }}
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        <strong>{{ number_format($item->qty_sold, 0) }}</strong>
                                    </td>
                                    <td class="text-center">
                                        <span class="badge bg-secondary">{{ $item->order_count }}</span>
                                    </td>
                                    <td class="text-end">
                                        <strong>{{ $currency }} {{ number_format($item->revenue, 0) }}</strong>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="7" class="text-center py-4 text-muted">
                                        {{ __('No sales data for selected period.') }}
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @if($products->hasPages())
                <div class="card-footer">
                    {{ $products->links() }}
                </div>
            @endif
        </div>

        <style>
            .cursor-pointer { cursor: pointer; }
        </style>
    </x-layouts.app>
</div>
