@extends('layouts.admin')

@section('title', 'Stock History: ' . $product->name)

@section('content')
<div class="admin-page-header">
    <h1>Stock History: {{ $product->name }}</h1>
    <div class="d-flex gap-2">
        <a href="{{ route('admin.products.show', $product->id) }}" class="admin-btn-primary" style="background: #6c757d;">
            <i class="bi bi-arrow-left"></i> Back to Product
        </a>
        @if($product->product_type === 'single')
        <a href="{{ route('admin.products.restock', ['product_id' => $product->id]) }}" class="admin-btn-primary">
            <i class="bi bi-box-arrow-in-down"></i> Restock
        </a>
        @endif
    </div>
</div>

<div class="admin-card">
    <div class="card-body">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h3 style="color: var(--admin-primary);">
                <i class="bi bi-clock-history"></i> Stock Movement History
            </h3>
            <div>
                <span class="badge bg-primary">Current Stock: {{ number_format($product->current_stock) }}</span>
            </div>
        </div>
        
        @if($stockMovements->count() > 0)
            <div class="table-responsive">
                <table class="admin-table table table-hover">
                    <thead>
                        <tr>
                            <th>Date & Time</th>
                            <th>Type</th>
                            <th>Quantity</th>
                            <th>Notes</th>
                            <th>User</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($stockMovements as $movement)
                            <tr>
                                <td>
                                    {{ $movement->created_at->format('d M Y') }}<br>
                                    <small class="text-muted">{{ $movement->created_at->format('h:i A') }}</small>
                                </td>
                                <td>
                                    @if($movement->type === 'opening_stock')
                                        <span class="badge bg-secondary">Opening Stock</span>
                                    @elseif($movement->type === 'restock')
                                        <span class="badge bg-primary">Restock</span>
                                    @elseif($movement->type === 'sale')
                                        <span class="badge bg-warning text-dark">Sale</span>
                                    @endif
                                </td>
                                <td>
                                    @if($movement->quantity > 0)
                                        <span style="color: #198754; font-weight: 600;">+{{ number_format($movement->quantity) }}</span>
                                    @else
                                        <span style="color: #dc3545; font-weight: 600;">{{ number_format($movement->quantity) }}</span>
                                    @endif
                                </td>
                                <td>{{ $movement->notes ?? '-' }}</td>
                                <td>
                                    @if($movement->creator)
                                        {{ $movement->creator->name }}
                                    @else
                                        <span class="text-muted">System</span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            
            <!-- Pagination -->
            @if($stockMovements->hasPages())
            <div class="pagination-wrapper mt-4">
                <div class="d-flex justify-content-end align-items-center gap-3 flex-wrap">
                    <div class="pagination-info-text">
                        Showing <strong>{{ $stockMovements->firstItem() ?? 0 }}</strong>–<strong>{{ $stockMovements->lastItem() ?? 0 }}</strong> 
                        of <strong>{{ $stockMovements->total() }}</strong> movements
                    </div>
                    <div class="pagination-controls">
                        {{ $stockMovements->links('vendor.pagination.admin-categories') }}
                    </div>
                </div>
            </div>
            @endif
        @else
            <div class="text-center py-5">
                <i class="bi bi-inbox" style="font-size: 3rem; color: #dee2e6;"></i>
                <p class="text-muted mt-3">No stock movements found for this product.</p>
            </div>
        @endif
    </div>
</div>

@endsection

