@extends('admin.layouts.app')

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

@section('content')
<div class="bg-white rounded-lg shadow">
    <div class="p-6 border-b border-gray-200 flex justify-between items-center">
        <div>
            <h3 class="text-lg font-semibold text-gray-800">Stock History</h3>
            <p class="text-sm text-gray-500">{{ $variant->product->name }} - {{ $variant->name }}</p>
        </div>
        <a href="{{ route('admin.inventory.index') }}" class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded">
            Back to Inventory
        </a>
    </div>

    <div class="p-6">
        <div class="mb-4">
            <p class="text-sm"><strong>Current Stock:</strong> <span class="text-lg font-bold">{{ $variant->stock_quantity }}</span></p>
            <p class="text-sm"><strong>Min Stock Level:</strong> {{ $variant->min_stock_level }}</p>
        </div>

        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Date</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Type</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Quantity</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Previous</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">New</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">User</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Notes</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($history as $entry)
                    <tr>
                        <td class="px-4 py-3 text-sm">{{ $entry->created_at->format('M d, Y h:i A') }}</td>
                        <td class="px-4 py-3">
                            <span class="px-2 py-1 text-xs rounded-full 
                                {{ $entry->type === 'in' ? 'bg-green-100 text-green-800' : '' }}
                                {{ $entry->type === 'out' ? 'bg-red-100 text-red-800' : '' }}
                                {{ $entry->type === 'adjustment' ? 'bg-blue-100 text-blue-800' : '' }}
                                {{ $entry->type === 'sale' ? 'bg-purple-100 text-purple-800' : '' }}">
                                {{ ucfirst($entry->type) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-sm font-semibold 
                            {{ $entry->type === 'in' ? 'text-green-600' : '' }}
                            {{ $entry->type === 'out' ? 'text-red-600' : '' }}">
                            {{ $entry->type === 'in' ? '+' : ($entry->type === 'out' ? '-' : '') }}{{ $entry->quantity }}
                        </td>
                        <td class="px-4 py-3 text-sm">{{ $entry->previous_stock }}</td>
                        <td class="px-4 py-3 text-sm font-semibold">{{ $entry->new_stock }}</td>
                        <td class="px-4 py-3 text-sm">{{ $entry->user->name ?? 'System' }}</td>
                        <td class="px-4 py-3 text-sm text-gray-500">{{ $entry->notes ?? '-' }}</td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="px-4 py-8 text-center text-gray-500">No stock history found</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
