<div>
    <x-layouts.app title="{{ __('FIFO Cost Layers') }}">
        <!-- Page Header -->
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="page-title">{{ __('FIFO Cost Layers') }}</h1>
                <p class="text-muted mb-0">{{ __('Active inventory lots ordered by FIFO') }}</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.fifo-layers', ['location_id' => $locationFilter]) }}" class="btn btn-outline-info">
                    <i class="fas fa-download me-2"></i>{{ __('Download PDF') }}
                </a>
                <a href="{{ route('pdf.report.fifo-layers', ['location_id' => $locationFilter, '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-4">
                <div class="card bg-primary text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Active Lots') }}</h6>
                        <h2 class="mb-0">{{ number_format($totalLots) }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <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-4">
                <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-6">
                        <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-6">
                        <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>
            </div>
        </div>

        <!-- Cost Layers 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>{{ __('Location') }}</th>
                                <th>{{ __('Batch') }}</th>
                                <th class="text-end">{{ __('Cost/Unit') }}</th>
                                <th class="text-center">{{ __('Received') }}</th>
                                <th class="text-center">{{ __('Remaining') }}</th>
                                <th class="text-end">{{ __('Layer Value') }}</th>
                                <th>{{ __('Source') }}</th>
                                <th>{{ __('Date') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($lots as $lot)
                                <tr wire:key="lot-{{ $lot->id }}">
                                    <td>
                                        <strong>{{ $lot->product?->localized_name ?? '-' }}</strong>
                                        <small class="text-muted d-block">{{ $lot->product?->sku }}</small>
                                    </td>
                                    <td>{{ $lot->location?->localized_name ?? '-' }}</td>
                                    <td><code>{{ $lot->batch_no ?? '-' }}</code></td>
                                    <td class="text-end">{{ $currency }} {{ number_format($lot->cost_price, 2) }}</td>
                                    <td class="text-center">{{ number_format($lot->quantity_received, 0) }}</td>
                                    <td class="text-center">
                                        <span class="badge bg-{{ $lot->quantity_remaining > 0 ? 'success' : 'secondary' }}">
                                            {{ number_format($lot->quantity_remaining, 0) }}
                                        </span>
                                    </td>
                                    <td class="text-end">
                                        <strong>{{ $currency }} {{ number_format($lot->quantity_remaining * $lot->cost_price, 2) }}</strong>
                                    </td>
                                    <td>
                                        <span class="badge bg-outline-secondary border text-dark">{{ $lot->source_type ?? '-' }}</span>
                                    </td>
                                    <td>{{ $lot->created_at->format('Y-m-d') }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="9" class="text-center py-4 text-muted">
                                        {{ __('No active cost layers found.') }}
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @if($lots->hasPages())
                <div class="card-footer">
                    {{ $lots->links() }}
                </div>
            @endif
        </div>
    </x-layouts.app>
</div>
