@extends('layouts.main')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h4>Inventory Log</h4>
    <div>
        @if(Auth::user()->hasPermission('inventory.stock-in'))
        <a href="{{ route('inventory.stock-in') }}" class="btn btn-success">
            <i class="bi bi-plus-circle"></i> Stock In
        </a>
        @endif
        @if(Auth::user()->hasPermission('inventory.adjustment'))
        <a href="{{ route('inventory.adjustment') }}" class="btn btn-warning">
            <i class="bi bi-arrow-repeat"></i> Adjustment
        </a>
        @endif
        @if(Auth::user()->hasPermission('inventory.report'))
        <a href="{{ route('inventory.report') }}" class="btn btn-info">
            <i class="bi bi-graph-up"></i> Report
        </a>
        @endif
    </div>
</div>

    <div class="card">
    <div class="card-header">
        <form method="GET" action="{{ route('inventory.index') }}" class="row g-3">
            <div class="col-md-2">
                <input type="text" name="barcode" class="form-control" placeholder="Search by Barcode" value="{{ request('barcode') }}">
            </div>
            <div class="col-md-2">
                <select name="product_id" class="form-select">
                    <option value="">All Products</option>
                    @foreach($products as $product)
                        <option value="{{ $product->id }}" {{ request('product_id') == $product->id ? 'selected' : '' }}>
                            {{ $product->name }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2">
                <select name="type" class="form-select">
                    <option value="">All Types</option>
                    <option value="stock_in" {{ request('type') == 'stock_in' ? 'selected' : '' }}>Stock In</option>
                    <option value="stock_out" {{ request('type') == 'stock_out' ? 'selected' : '' }}>Stock Out</option>
                    <option value="adjustment" {{ request('type') == 'adjustment' ? 'selected' : '' }}>Adjustment</option>
                </select>
            </div>
            <div class="col-md-2">
                <input type="date" name="date_from" class="form-control" value="{{ request('date_from') }}">
            </div>
            <div class="col-md-2">
                <input type="date" name="date_to" class="form-control" value="{{ request('date_to') }}">
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100">Filter</button>
            </div>
            <div class="col-md-2">
                <a href="{{ route('inventory.index') }}" class="btn btn-secondary w-100">Reset</a>
            </div>
        </form>
    </div>
    <div class="card-body table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Product</th>
                    <th>Type</th>
                    <th>Qty</th>
                    <th>Previous</th>
                    <th>New</th>
                    <th>Reference</th>
                    <th>User</th>
                </tr>
            </thead>
            <tbody>
                @forelse($logs as $log)
                    <tr>
                        <td>{{ $log->created_at->format('M d, Y H:i') }}</td>
                        <td>{{ $log->product->name ?? 'N/A' }}</td>
                        <td>
                            <span class="badge {{ $log->type === 'stock_in' ? 'bg-success' : ($log->type === 'stock_out' ? 'bg-danger' : 'bg-warning') }}">
                                {{ ucfirst(str_replace('_', ' ', $log->type)) }}
                            </span>
                        </td>
                        <td>{{ $log->quantity }}</td>
                        <td>{{ $log->previous_stock }}</td>
                        <td>{{ $log->new_stock }}</td>
                        <td>{{ $log->reference_number ?? '-' }}</td>
                        <td>{{ $log->user->name ?? 'N/A' }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="8" class="text-center">No inventory logs found.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    <div class="card-footer">
        {{ $logs->withQueryString()->links() }}
    </div>
</div>
@endsection
