@extends('layouts.admin')

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

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Catalog</span>
            <h1>Inventory History</h1>
        </div>
        <a class="button ghost" href="{{ route('admin.products.index') }}">Products</a>
    </section>

    <section class="panel">
        <form method="GET" action="{{ route('admin.inventory.index') }}" class="admin-form inventory-filter-form">
            <div class="form-grid two-columns">
                <label>
                    <span>Product</span>
                    <select name="product_id">
                        <option value="">All products</option>
                        @foreach ($products as $product)
                            <option value="{{ $product->id }}" @selected($selectedProductId === $product->id)>{{ $product->name }}</option>
                        @endforeach
                    </select>
                </label>

                <label>
                    <span>Movement Type</span>
                    <select name="type">
                        <option value="">All types</option>
                        @foreach ($types as $key => $label)
                            <option value="{{ $key }}" @selected($selectedType === $key)>{{ $label }}</option>
                        @endforeach
                    </select>
                </label>
            </div>
            <div class="table-actions">
                <button class="button primary" type="submit">Filter</button>
                <a class="button ghost" href="{{ route('admin.inventory.index') }}">Reset</a>
            </div>
        </form>

        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Product</th>
                        <th>Variant</th>
                        <th>Type</th>
                        <th>Change</th>
                        <th>Before</th>
                        <th>After</th>
                        <th>Admin</th>
                        <th>Reference</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($movements as $movement)
                        <tr>
                            <td>{{ $movement->created_at->format('d M Y, h:i A') }}</td>
                            <td>{{ $movement->product?->name ?? 'Deleted product' }}</td>
                            <td>{{ $movement->variant?->label() ?? 'Base stock' }}</td>
                            <td><span class="status pending">{{ $movement->typeLabel() }}</span></td>
                            <td>
                                <strong @class(['stock-positive' => $movement->quantity_change > 0, 'stock-negative' => $movement->quantity_change < 0])>
                                    {{ $movement->quantity_change > 0 ? '+' : '' }}{{ $movement->quantity_change }}
                                </strong>
                                @if ($movement->note)
                                    <small>{{ $movement->note }}</small>
                                @endif
                            </td>
                            <td>{{ $movement->stock_before }}</td>
                            <td>{{ $movement->stock_after }}</td>
                            <td>{{ $movement->admin?->name ?? 'System' }}</td>
                            <td>
                                @if ($movement->order)
                                    <a class="text-button" href="{{ route('admin.orders.show', $movement->order) }}">{{ $movement->order->order_number }}</a>
                                @else
                                    -
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="9">No inventory movement recorded yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{ $movements->links() }}
    </section>
@endsection
