@extends('layouts.admin')
@section('title', 'Inventory Balances')
@section('content')
<div class="tw-migrated space-y-4">
    <x-ui.page-header title="Store / Warehouse Stock">
        <x-slot:actions>
            <x-ui.button href="{{ route('admin.transfers.create') }}" variant="outline" size="sm">Transfer</x-ui.button>
            <x-ui.button href="{{ route('admin.purchase-orders.create') }}" variant="outline" size="sm">Purchase</x-ui.button>
        </x-slot:actions>
    </x-ui.page-header>

    <x-ui.card>
        <form method="get" class="ci-filters inventory-balance-filters mb-0">
            <div class="row g-2 align-items-end justify-content-end flex-nowrap inventory-balance-filters__row">
                <div class="col-lg-2">
                    <label class="admin-form-label">Location</label>
                    <select name="location_id" class="form-select admin-form-control">
                        @foreach($locations as $loc)
                            <option value="{{ $loc->id }}" @selected($locationId == $loc->id)>{{ $loc->name }} ({{ $loc->type }})</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-lg-2">
                    <label class="admin-form-label">Search</label>
                    <div class="products-search-wrap">
                        <i class="bi bi-search"></i>
                        <input type="text" name="q" value="{{ request('q') }}" class="form-control admin-form-control" placeholder="Name / SKU / barcode">
                    </div>
                </div>
                <div class="col-lg-2">
                    <label class="admin-form-label">Stock</label>
                    <select name="stock" class="form-select admin-form-control">
                        <option value="">All</option>
                        <option value="available" @selected(request('stock')==='available')>Available &gt; 0</option>
                        <option value="zero" @selected(request('stock')==='zero')>Zero available</option>
                        <option value="reserved" @selected(request('stock')==='reserved')>Has reserved</option>
                    </select>
                </div>
                <div class="ci-filters__actions col-lg-2">
                    <label class="admin-form-label">&nbsp;</label>
                    <button type="submit" class="ci-btn ci-btn--primary">Filter</button>
                </div>
            </div>
        </form>
    </x-ui.card>

    <x-ui.card>
        <x-ui.table>
            <x-slot:thead>
                <tr>
                    <th>Product</th>
                    <th>SKU</th>
                    <th>On Hand</th>
                    <th>Reserved</th>
                    <th>Damaged</th>
                    <th>Available</th>
                    <th>In Transit In</th>
                    <th>In Transit Out</th>
                </tr>
            </x-slot:thead>
            @forelse($balances as $balance)
            <tr>
                <td>{{ $balance->product?->name }}</td>
                <td>{{ $balance->product?->sku ?? '—' }}</td>
                <td>{{ $balance->on_hand }}</td>
                <td>{{ $balance->reserved }}</td>
                <td>{{ $balance->damaged }}</td>
                <td><strong class="{{ $balance->available() <= 0 ? 'text-danger' : 'text-success' }}">{{ $balance->available() }}</strong></td>
                <td>{{ $balance->in_transit_in }}</td>
                <td>{{ $balance->in_transit_out }}</td>
            </tr>
            @empty
            <tr>
                <td colspan="8" class="text-center text-muted py-4">
                    No balances for this location. Run <code>php artisan inventory:backfill-locations</code> if stock looks empty after migrate.
                </td>
            </tr>
            @endforelse
        </x-ui.table>
        {{ $balances->withQueryString()->links() }}
    </x-ui.card>
</div>
@endsection
