@extends('layouts.app')

@section('title', 'Global Inventory')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">Global Inventory Tracking</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                    <li class="breadcrumb-item active">Inventory</li>
                </ol>
            </nav>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('inventory.movements') }}" class="btn btn-outline-secondary">
                <i class="bi bi-clock-history me-2"></i> Movement Log
            </a>
            <a href="{{ route('inventory.adjust') }}" class="btn btn-primary px-4">
                <i class="bi bi-plus-slash-minus me-2"></i> Stock Adjustment
            </a>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body">
            <form action="{{ route('inventory.index') }}" method="GET" class="row g-3">
                <div class="col-md-5">
                    <div class="input-group">
                        <span class="input-group-text bg-white border-end-0"><i class="bi bi-search"></i></span>
                        <input type="text" name="search" class="form-control border-start-0"
                            placeholder="Search by product or SKU...">
                    </div>
                </div>
                <div class="col-md-4">
                    <select name="warehouse_id" class="form-select">
                        <option value="">All Warehouses</option>
                        @foreach(\App\Models\Warehouse::all() as $wh)
                            <option value="{{ $wh->id }}">{{ $wh->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <button type="submit" class="btn btn-light w-100">Filter Inventory</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Product</th>
                            <th>Location</th>
                            <th class="text-center">On Hand</th>
                            <th class="text-center">Reserved</th>
                            <th class="text-center">Available</th>
                            <th class="text-end pe-4">Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($inventories as $inventory)
                            <tr>
                                <td class="ps-4">
                                    <div class="fw-bold">{{ $inventory->product->name }}</div>
                                    <span class="text-muted small">{{ $inventory->product->sku }}</span>
                                </td>
                                <td>
                                    <span class="badge bg-light text-dark border"><i class="bi bi-geo-alt me-1"></i>
                                        {{ $inventory->warehouse->name }}</span>
                                </td>
                                <td class="text-center fw-bold">{{ number_format($inventory->quantity, 2) }}</td>
                                <td class="text-center text-muted">
                                    @if($inventory->reserved_quantity > 0)
                                        <span
                                            class="text-warning fw-semibold">{{ number_format($inventory->reserved_quantity, 2) }}</span>
                                    @else
                                        -
                                    @endif
                                </td>
                                <td class="text-center">
                                    <span
                                        class="fs-6 fw-bold text-primary">{{ number_format($inventory->quantity - $inventory->reserved_quantity, 2) }}</span>
                                    <small
                                        class="text-muted d-block smallest">{{ $inventory->product->unit_of_measure }}</small>
                                </td>
                                <td class="text-end pe-4">
                                    @php
                                        $status = ($inventory->quantity - $inventory->reserved_quantity) > 10 ? 'success' : 'danger';
                                    @endphp
                                    <span class="badge bg-{{ $status }}-subtle text-{{ $status }} rounded-pill px-3">
                                        {{ $status == 'success' ? 'Healthy' : 'Low Stock' }}
                                    </span>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted italic">No inventory records found matching
                                    your filters.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="mt-4">
        {{ $inventories->links() }}
    </div>

    <style>
        .smallest {
            font-size: 0.7rem;
        }
    </style>
@endsection