@extends('layouts.admin')

@section('title', 'Variant Stock History - ' . $variant->barcode)

@section('content')
<div class="admin-page-header">
    <h1>Stock History: {{ $variant->parent->name ?? $variant->name }} -
        @if($variant->attributeValues->isNotEmpty())
            @foreach($variant->attributeValues as $attrValue)
                    {{ $attrValue->attribute->name }}: {{ $attrValue->value }}
            @endforeach
        @endif
    </h1>
    <a href="{{ route('admin.products.variants.index', $variant->parent_id) }}" class="admin-btn-primary" style="background: #6c757d;">
        <i class="bi bi-arrow-left"></i> Back to Variants
    </a>
</div>

<div class="admin-card mb-3">
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <h5>Variant Information</h5>
                <p><strong>Product:</strong> {{ $variant->parent->name ?? $variant->name }}</p>
                <p><strong>Attributes:</strong>
                    @if($variant->attributeValues->isNotEmpty())
                        @foreach($variant->attributeValues as $attrValue)
                            <span class="badge bg-secondary me-1">
                                {{ $attrValue->attribute->name }}: {{ $attrValue->value }}
                            </span>
                        @endforeach
                    @else
                        <span class="text-muted">No attributes</span>
                    @endif
                </p>
                <p><strong>Barcode:</strong> {{ $variant->barcode }}</p>
            </div>
            <div class="col-md-6">
                <h5>Stock Summary</h5>
                <p><strong>Current Stock:</strong> <span class="badge bg-primary">{{ $variant->current_stock }}</span></p>
                <p><strong>Opening Stock:</strong> {{ $variant->opening_stock }}</p>
            </div>
        </div>
    </div>
</div>

<div class="admin-card">
    <div class="card-body">
        <h3 class="mb-4" style="color: var(--admin-primary);">
            <i class="bi bi-clock-history"></i> Stock Movement History
        </h3>
        
        @if($stockMovements->isEmpty())
            <div class="text-center py-5">
                <i class="bi bi-inbox" style="font-size: 3rem; color: #6c757d;"></i>
                <h5 class="mt-3">No Stock Movements</h5>
                <p class="text-muted">This variant has no stock movement history yet.</p>
            </div>
        @else
            <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>Created By</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $runningBalance = $variant->current_stock;
                        @endphp
                        @foreach($stockMovements as $movement)
                            @php
                                // Calculate running balance (working backwards from current stock)
                                $runningBalance -= $movement->quantity;
                            @endphp
                            <tr>
                                <td>
                                    {{ $movement->created_at->format('M d, 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-info">Opening Stock</span>
                                    @elseif($movement->type === 'restock')
                                        <span class="badge bg-success">Restock</span>
                                    @elseif($movement->type === 'sale')
                                        <span class="badge bg-danger">Sale</span>
                                    @else
                                        <span class="badge bg-secondary">{{ ucfirst($movement->type) }}</span>
                                    @endif
                                </td>
                                <td>
                                    @if($movement->quantity > 0)
                                        <span class="text-success fw-bold">+{{ $movement->quantity }}</span>
                                    @else
                                        <span class="text-danger fw-bold">{{ $movement->quantity }}</span>
                                    @endif
                                </td>
                                <td>
                                    @if($movement->notes)
                                        {{ $movement->notes }}
                                    @else
                                        <span class="text-muted">-</span>
                                    @endif
                                </td>
                                <td>
                                    @if($movement->creator)
                                        {{ $movement->creator->name }}
                                    @else
                                        <span class="text-muted">System</span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            <!-- Pagination - Right Aligned -->
            <div class="pagination-wrapper">
                <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> stock movements
                    </div>
                    @if($stockMovements->hasPages())
                    <div class="pagination-controls">
                        {{ $stockMovements->links('vendor.pagination.admin-categories') }}
                    </div>
                    @endif
                </div>
            </div>
        @endif
    </div>
</div>

@endsection

