<div>
    <x-layouts.app title="{{ __('Batch & Expiry Report') }}">
        <!-- Page Header -->
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="page-title">{{ __('Batch & Expiry Report') }}</h1>
                <p class="text-muted mb-0">{{ __('Track batch numbers and expiry dates') }}</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.batch-expiry', ['search' => $search, 'location_id' => $locationFilter, 'horizon' => $expiryHorizon, 'expired_only' => $expiredOnly]) }}" class="btn btn-outline-info">
                    <i class="fas fa-download me-2"></i>{{ __('Download PDF') }}
                </a>
                <a href="{{ route('pdf.report.batch-expiry', ['search' => $search, 'location_id' => $locationFilter, 'horizon' => $expiryHorizon, 'expired_only' => $expiredOnly, '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 Batches') }}</h6>
                        <h2 class="mb-0">{{ $totalBatches }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card bg-danger text-white h-100">
                    <div class="card-body text-center">
                        <h6 class="text-white-50">{{ __('Expired Lots') }}</h6>
                        <h2 class="mb-0">{{ $expiredCount }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card bg-warning text-dark h-100">
                    <div class="card-body text-center">
                        <h6 class="text-dark opacity-75">{{ __('Expiring in 30 Days') }}</h6>
                        <h2 class="mb-0">{{ $nearExpiryCount }}</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">{{ __('Search') }}</label>
                        <input type="text" class="form-control" wire:model.live.debounce.300ms="search"
                               placeholder="{{ __('Batch no. or product...') }}">
                    </div>
                    <div class="col-md-3">
                        <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-3">
                        <label class="form-label">{{ __('Expiry Horizon') }}</label>
                        <select class="form-select" wire:model.live="expiryHorizon">
                            <option value="30">{{ __('30 Days') }}</option>
                            <option value="60">{{ __('60 Days') }}</option>
                            <option value="90">{{ __('90 Days') }}</option>
                            <option value="180">{{ __('6 Months') }}</option>
                            <option value="365">{{ __('1 Year') }}</option>
                            <option value="">{{ __('All') }}</option>
                        </select>
                    </div>
                    <div class="col-md-3 d-flex align-items-end">
                        <div class="form-check">
                            <input class="form-check-input" type="checkbox" wire:model.live="expiredOnly" id="expiredOnly">
                            <label class="form-check-label" for="expiredOnly">{{ __('Expired Only') }}</label>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Lots 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>{{ __('Batch No') }}</th>
                                <th>{{ __('Location') }}</th>
                                <th class="text-center">{{ __('Qty Remaining') }}</th>
                                <th class="text-end">{{ __('Cost') }}</th>
                                <th>{{ __('Expiry Date') }}</th>
                                <th>{{ __('Status') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($lots as $lot)
                                @php
                                    $isExpired = $lot->expiry_date && $lot->expiry_date->isPast();
                                    $isNearExpiry = $lot->expiry_date && !$isExpired && $lot->expiry_date->diffInDays(now()) <= 30;
                                    $rowClass = $isExpired ? 'table-danger' : ($isNearExpiry ? 'table-warning' : '');
                                @endphp
                                <tr wire:key="lot-{{ $lot->id }}" class="{{ $rowClass }}">
                                    <td>
                                        <strong>{{ $lot->product?->localized_name ?? '-' }}</strong>
                                        <small class="text-muted d-block">{{ $lot->product?->sku }}</small>
                                    </td>
                                    <td><code>{{ $lot->batch_no ?? '-' }}</code></td>
                                    <td>{{ $lot->location?->localized_name ?? '-' }}</td>
                                    <td class="text-center">{{ number_format($lot->quantity_remaining, 0) }}</td>
                                    <td class="text-end">{{ $currency }} {{ number_format($lot->cost_price, 2) }}</td>
                                    <td>{{ $lot->expiry_date ? $lot->expiry_date->format('Y-m-d') : '-' }}</td>
                                    <td>
                                        @if($isExpired)
                                            <span class="badge bg-danger">{{ __('Expired') }}</span>
                                        @elseif($isNearExpiry)
                                            <span class="badge bg-warning text-dark">{{ __('Near Expiry') }}</span>
                                        @else
                                            <span class="badge bg-success">{{ __('OK') }}</span>
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="7" class="text-center py-4 text-muted">
                                        {{ __('No batch/expiry records found.') }}
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            @if($lots->hasPages())
                <div class="card-footer">
                    {{ $lots->links() }}
                </div>
            @endif
        </div>
    </x-layouts.app>
</div>
