<div class="space-y-6">
    <!-- Header -->
    <div class="flex items-center justify-between">
        <div>
            <h1 class="text-2xl font-semibold text-zinc-900 dark:text-white dark:text-white">History for {{ $part->name }}</h1>
            <p class="mt-1 text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                Part #{{ $part->part_number }} - Complete audit trail
            </p>
        </div>
        <div class="flex items-center space-x-3">
            <flux:button wire:click="goBack" variant="outline" size="sm">
                Back to Part
            </flux:button>
        </div>
    </div>

    <!-- Filters -->
    <div class="bg-white dark:bg-zinc-800 border border-zinc-200 dark:border-zinc-700 rounded-lg p-6">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
            <!-- Event Type Filter -->
            <div>
                <flux:field>
                    <flux:label>Event Type</flux:label>
                    <flux:select wire:model.live="eventTypeFilter">
                        <option value="">All Events</option>
                        @foreach($eventTypes as $value => $label)
                            <option value="{{ $value }}">{{ $label }}</option>
                        @endforeach
                    </flux:select>
                </flux:field>
            </div>

            <!-- Date From -->
            <div>
                <flux:field>
                    <flux:label>From Date</flux:label>
                    <flux:input type="date" wire:model.live="dateFrom" />
                </flux:field>
            </div>

            <!-- Date To -->
            <div>
                <flux:field>
                    <flux:label>To Date</flux:label>
                    <flux:input type="date" wire:model.live="dateTo" />
                </flux:field>
            </div>

            <!-- Clear Filters -->
            <div class="flex items-end">
                <flux:button wire:click="clearFilters" variant="outline" size="sm">
                    Clear Filters
                </flux:button>
            </div>
        </div>
    </div>

    <!-- History Timeline -->
    <div class="bg-white dark:bg-zinc-800 border border-zinc-200 dark:border-zinc-700 rounded-lg">
        @if($histories->count() > 0)
            <div class="p-6">
                <div class="flow-root">
                    <ul role="list" class="-mb-8">
                        @foreach($histories as $index => $history)
                            <li>
                                <div class="relative pb-8">
                                    @if(!$loop->last)
                                        <span class="absolute left-4 top-4 -ml-px h-full w-0.5 bg-gray-200 dark:bg-zinc-600" aria-hidden="true"></span>
                                    @endif
                                    <div class="relative flex space-x-3">
                                        <div>
                                            <span class="h-8 w-8 rounded-full bg-{{ $history->event_color }}-500 flex items-center justify-center ring-8 ring-white dark:ring-zinc-800">
                                                <flux:icon variant="outline" size="xs" name="{{ $history->event_icon }}" class="text-white" />
                                            </span>
                                        </div>
                                        <div class="flex min-w-0 flex-1 justify-between space-x-4 pt-1.5">
                                            <div class="min-w-0 flex-1">
                                                <div class="flex items-center space-x-2">
                                                    <flux:badge size="sm" color="{{ $history->event_color }}">
                                                        {{ ucfirst(str_replace('_', ' ', $history->event_type)) }}
                                                    </flux:badge>
                                                    @if($history->quantity_change)
                                                        <span class="text-sm font-medium {{ $history->quantity_change > 0 ? 'text-green-600 dark:text-green-400' : 'text-red-600 dark:text-red-400' }}">
                                                            {{ $history->formatted_quantity_change }}
                                                        </span>
                                                    @endif
                                                </div>

                                                @if($history->notes)
                                                    <p class="text-sm text-zinc-900 dark:text-white dark:text-white mt-1">{{ $history->notes }}</p>
                                                @endif

                                                <!-- Stock Changes -->
                                                @if($history->quantity_before !== null && $history->quantity_after !== null)
                                                    <div class="mt-2 text-xs text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                                        Stock: {{ number_format($history->quantity_before) }} → {{ number_format($history->quantity_after) }}
                                                    </div>
                                                @endif

                                                <!-- Price Changes -->
                                                @if($history->cost_before !== null && $history->cost_after !== null)
                                                    <div class="mt-2 text-xs text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                                        Cost: ${{ number_format($history->cost_before, 2) }} → ${{ number_format($history->cost_after, 2) }}
                                                    </div>
                                                @endif

                                                <!-- Field Changes -->
                                                @if($history->old_values && $history->new_values)
                                                    <div class="mt-2">
                                                        <details class="group">
                                                            <summary class="text-xs text-blue-600 dark:text-blue-400 cursor-pointer hover:text-blue-800 dark:hover:text-blue-300">
                                                                View Changes
                                                            </summary>
                                                            <div class="mt-2 text-xs space-y-1">
                                                                @foreach($history->new_values as $field => $newValue)
                                                                    @if(isset($history->old_values[$field]))
                                                                        <div class="grid grid-cols-3 gap-2">
                                                                            <span class="font-medium text-zinc-600 dark:text-zinc-400 dark:text-gray-300">{{ ucfirst(str_replace('_', ' ', $field)) }}:</span>
                                                                            <span class="text-red-600 dark:text-red-400">{{ $history->old_values[$field] ?? 'N/A' }}</span>
                                                                            <span class="text-green-600 dark:text-green-400">{{ $newValue ?? 'N/A' }}</span>
                                                                        </div>
                                                                    @endif
                                                                @endforeach
                                                            </div>
                                                        </details>
                                                    </div>
                                                @endif

                                                <!-- Reference -->
                                                @if($history->reference_type && $history->reference_id)
                                                    <div class="mt-1 text-xs text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                                        Ref: {{ ucfirst(str_replace('_', ' ', $history->reference_type)) }} #{{ $history->reference_id }}
                                                    </div>
                                                @endif
                                            </div>
                                            <div class="whitespace-nowrap text-right text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                                                <div>{{ $history->created_at->format('M d, Y') }}</div>
                                                <div>{{ $history->created_at->format('g:i A') }}</div>
                                                @if($history->createdBy)
                                                    <div class="text-xs">{{ $history->createdBy->name }}</div>
                                                @endif
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </li>
                        @endforeach
                    </ul>
                </div>
            </div>

            <!-- Pagination -->
            <div class="px-6 py-3 border-t border-zinc-200 dark:border-zinc-700">
                {{ $histories->links() }}
            </div>
        @else
            <div class="p-12 text-center">
                <flux:icon name="clock" size="lg" class="mx-auto h-12 w-12 text-gray-400 dark:text-zinc-500 dark:text-zinc-400" />
                <h3 class="mt-2 text-sm font-medium text-zinc-900 dark:text-white dark:text-white">No history found</h3>
                <p class="mt-1 text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                    No history records match your current filters.
                </p>
            </div>
        @endif
    </div>
</div>
