<div>
    <x-layouts.app title="{{ __('Stock by Location') }}">
        <!-- Page Header -->
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="page-title">{{ __('Stock by Location') }}</h1>
                <p class="text-muted mb-0">{{ __('Inventory quantities per storage location') }}</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.stock-by-location', ['search' => $search, 'location_id' => $locationFilter, 'category_id' => $categoryFilter]) }}" class="btn btn-outline-info">
                    <i class="fas fa-download me-2"></i>{{ __('Download PDF') }}
                </a>
                <a href="{{ route('pdf.report.stock-by-location', ['search' => $search, 'location_id' => $locationFilter, '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-3">
                <div class="card bg-primary text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Locations') }}</h6>
                        <h2 class="mb-0">{{ $locationCount }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-secondary text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Products') }}</h6>
                        <h2 class="mb-0">{{ number_format($totalProducts) }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-info text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Total Units') }}</h6>
                        <h2 class="mb-0">{{ number_format($totalUnits, 0) }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-success text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Total Value') }}</h6>
                        <h4 class="mb-0">{{ $currency }} {{ number_format($totalValue, 0) }}</h4>
                    </div>
                </div>
            </div>
        </div>

        <!-- Filters -->
        <div class="card mb-4">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label">{{ __('Search') }}</label>
                        <input type="text" class="form-control" wire:model.live.debounce.300ms="search"
                               placeholder="{{ __('Product name or SKU...') }}">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">{{ __('Location') }}</label>
                        <select class="form-select" wire:model.live="locationFilter">
                            <option value="">{{ __('All Locations') }}</option>
                            @foreach($locations as $location)
                                <option value="{{ $location->id }}">{{ $location->localized_name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-4">
                        <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>

        <!-- Stock 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>{{ __('Product') }}</th>
                                <th>{{ __('SKU') }}</th>
                                <th>{{ __('Location') }}</th>
                                <th class="text-center">{{ __('Quantity') }}</th>
                                <th class="text-end">{{ __('Cost') }}</th>
                                <th class="text-end">{{ __('Value') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($stocks as $stock)
                                <tr wire:key="stock-{{ $stock->id }}">
                                    <td><strong>{{ $stock->product?->localized_name ?? '-' }}</strong></td>
                                    <td><code>{{ $stock->product?->sku ?? '-' }}</code></td>
                                    <td>
                                        <span class="badge bg-info">{{ $stock->location?->localized_name ?? __('Unassigned') }}</span>
                                    </td>
                                    <td class="text-center">{{ number_format($stock->quantity, 0) }}</td>
                                    <td class="text-end">{{ $currency }} {{ number_format($stock->product?->cost_price ?? 0, 2) }}</td>
                                    <td class="text-end">
                                        <strong>{{ $currency }} {{ number_format($stock->quantity * ($stock->product?->cost_price ?? 0), 0) }}</strong>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="6" class="text-center py-4 text-muted">
                                        {{ __('No stock records found.') }}
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @if($stocks->hasPages())
                <div class="card-footer">
                    {{ $stocks->links() }}
                </div>
            @endif
        </div>
    </x-layouts.app>
</div>
