@extends('layouts.app')

@section('title', 'Stock History - ' . $product->name)

@section('actions')
    <a href="{{ route('products.show', $product) }}" class="btn btn-secondary">
        <i class="fas fa-arrow-left"></i> Back to Product
    </a>
@endsection

@section('content')
<div class="container-fluid">
    <div class="card">
        <div class="card-header">
            <div class="d-flex justify-content-between align-items-center">
                <h5 class="mb-0">Stock History: {{ $product->name }}</h5>
                <span class="badge badge-primary">Current Stock: {{ $product->stock_quantity }}</span>
            </div>
        </div>
        <div class="card-body">
            @if($history->count() > 0)
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Adjustment</th>
                            <th>Quantity</th>
                            <th>Old Stock</th>
                            <th>New Stock</th>
                            <th>Reason</th>
                            <th>Adjusted By</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($history as $record)
                        <tr>
                            <td>{{ $record->created_at->format('d M, Y H:i') }}</td>
                            <td>
                                <span class="badge badge-{{ $record->adjustment_color }}">
                                    <i class="fas fa-{{ $record->adjustment_icon }}"></i>
                                    {{ $record->adjustment_type_text }}
                                </span>
                            </td>
                            <td class="{{ $record->adjustment_type == 'add' ? 'text-success' : 'text-danger' }}">
                                @if($record->adjustment_type == 'add')
                                +{{ $record->quantity }}
                                @else
                                -{{ $record->quantity }}
                                @endif
                            </td>
                            <td>{{ $record->old_stock }}</td>
                            <td>{{ $record->new_stock }}</td>
                            <td>{{ $record->reason ?? 'N/A' }}</td>
                            <td>{{ $record->adjuster->name ?? 'System' }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            
            <div class="mt-3">
                {{ $history->links() }}
            </div>
            @else
            <div class="text-center py-5">
                <i class="fas fa-history fa-3x text-muted mb-3"></i>
                <h5>No Stock History</h5>
                <p class="text-muted">No stock adjustments have been made for this product yet.</p>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection