@extends('layouts.admin')

@section('title', 'Adjustment History Report')

@section('content')
<x-admin.report-page title="Adjustment History Report" subtitle="Audit and manual stock adjustment history.">
    <x-slot:actions>
        @can('stock-adjustments.manage')
            <a href="{{ route('admin.stock-adjustments.manual') }}" class="ci-btn ci-btn--primary">
                <i class="bi bi-pencil-square"></i> Manual Adjustment
            </a>
        @endcan
    </x-slot:actions>

    <form method="GET" action="{{ route('admin.reports.adjustment-history') }}" class="ci-filters ci-filters--report">
        @include('admin.reports.partials.location-filter', ['ci' => true])
        <div class="ci-filters__field">
            <label for="start_date">Start Date</label>
            <input type="date" name="start_date" id="start_date" value="{{ request('start_date') }}">
        </div>
        <div class="ci-filters__field">
            <label for="end_date">End Date</label>
            <input type="date" name="end_date" id="end_date" value="{{ request('end_date') }}">
        </div>
        <div class="ci-filters__field">
            <label for="reference_type">Type</label>
            <select name="reference_type" id="reference_type">
                <option value="">All</option>
                <option value="audit" {{ request('reference_type') === 'audit' ? 'selected' : '' }}>Audit</option>
                <option value="manual" {{ request('reference_type') === 'manual' ? 'selected' : '' }}>Manual</option>
            </select>
        </div>
        <div class="ci-filters__actions">
            <button type="submit" class="ci-btn ci-btn--primary">Apply</button>
            @if(request()->hasAny(['start_date', 'end_date', 'reference_type', 'location_id']))
                <a href="{{ route('admin.reports.adjustment-history') }}" class="ci-btn ci-btn--ghost ci-btn--icon" title="Reset">
                    <i class="bi bi-arrow-clockwise"></i>
                </a>
            @endif
        </div>
    </form>

    <div class="ci-table-card">
        <div class="ci-table-wrap">
            <table class="ci-table">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Shop</th>
                        <th>Product / Variant</th>
                        <th>Type</th>
                        <th class="ci-end">Quantity</th>
                        <th>Reason</th>
                        <th>Remarks</th>
                        <th>Created By</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($adjustments as $adj)
                        @php
                            $product = $adj->variant_id ? $adj->variant : $adj->product;
                        @endphp
                        <tr>
                            <td class="ci-muted">{{ $adj->created_at->format('Y-m-d H:i') }}</td>
                            <td>{{ $adj->location?->name ?? '—' }}</td>
                            <td>{{ $product?->name ?? '—' }}</td>
                            <td>
                                @if($adj->adjustment_type === 'addition')
                                    <span class="ci-pill ci-pill--success">Addition</span>
                                @else
                                    <span class="ci-pill ci-pill--danger">Subtraction</span>
                                @endif
                            </td>
                            <td class="ci-end ci-num">{{ number_format($adj->adjusted_qty, 2) }}</td>
                            <td>{{ $adj->reason }}</td>
                            <td class="ci-muted">{{ Str::limit($adj->remarks, 50) }}</td>
                            <td>{{ $adj->creator?->name ?? '—' }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8">
                                <div class="ci-empty">
                                    <i class="bi bi-sliders"></i>
                                    <h3>No Adjustments Found</h3>
                                    <p>No adjustments found for the selected filters.</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="ci-pagination">
            {{ $adjustments->links() }}
        </div>
    </div>
</x-admin.report-page>
@endsection
