<div class="min-h-screen bg-gray-50 dark:bg-gray-900">
    <div class="bg-white dark:bg-gray-800 shadow-sm border-b border-gray-200 dark:border-gray-700">
        <div class="mx-auto px-4 sm:px-6 lg:px-8">
            <div class="py-6">
                <div class="flex items-center justify-between">
                    <div>
                        <h1 class="text-3xl font-bold text-gray-900 dark:text-white">Table  BulkImport</h1>
                        <p class="mt-2 text-gray-600 dark:text-gray-400">Import tables from CSV or Excel files</p>
                    </div>
                    <div class="flex space-x-3">
                        <x-secondary-button wire:click="downloadSampleFile" class="flex items-center space-x-2">
                            <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                            </svg>
                            <span>Download Sample</span>
                        </x-secondary-button>
                        
                        @if($uploadStage !== 'idle')
                        <x-secondary-button wire:click="resetUploadState" class="flex items-center space-x-2">
                            <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                            </svg>
                            <span>Start New Upload</span>
                        </x-secondary-button>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="w-full px-4 sm:px-6 lg:px-8 py-2">

    @if($uploadStage === 'idle')
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-4">
        <div class="space-y-4">
            <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-4">
                <div class="flex items-center space-x-2 mb-3">
                    <div class="flex-shrink-0 w-6 h-6 bg-blue-100 dark:bg-blue-900/30 rounded-full flex items-center justify-center">
                        <span class="text-blue-600 dark:text-blue-400 font-semibold text-xs">1</span>
                    </div>
                    <h3 class="text-sm font-semibold text-gray-900 dark:text-white">Import Instructions</h3>
                </div>

                <div class="space-y-3">
                    <div>
                        <h4 class="text-xs font-medium text-gray-900 dark:text-white mb-1">Required Columns:</h4>
                        <div class="grid grid-cols-1 gap-1">
                            <div class="flex items-center space-x-2 p-1.5 bg-green-50 dark:bg-green-900/20 rounded">
                                <code class="bg-green-100 dark:bg-green-800 text-green-800 dark:text-green-200 px-1.5 py-0.5 rounded text-xs font-mono">table_code</code>
                                <span class="text-xs text-gray-600 dark:text-gray-400">Unique Table Identifier (e.g., T-01)</span>
                            </div>
                            <div class="flex items-center space-x-2 p-1.5 bg-green-50 dark:bg-green-900/20 rounded">
                                <code class="bg-green-100 dark:bg-green-800 text-green-800 dark:text-green-200 px-1.5 py-0.5 rounded text-xs font-mono">seating_capacity</code>
                                <span class="text-xs text-gray-600 dark:text-gray-400">Number of Seats (e.g., 4)</span>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mt-2 text-xs text-gray-500">
                        <p><strong>Note:</strong> Rows with empty table codes will be ignored. Duplicate table codes in the same branch will be skipped.</p>
                    </div>
                </div>
            </div>

            <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-4">
                <div class="flex items-center space-x-2 mb-3">
                    <div class="flex-shrink-0 w-6 h-6 bg-amber-100 dark:bg-amber-900/30 rounded-full flex items-center justify-center">
                        <svg class="h-3 w-3 text-amber-600 dark:text-amber-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                        </svg>
                    </div>
                    <h3 class="text-sm font-semibold text-gray-900 dark:text-white">System Data</h3>
                </div>

                <div class="space-y-3">
                    <div>
                        <h4 class="text-xs font-medium text-gray-900 dark:text-white mb-1">Available Areas:</h4>
                        <div class="flex flex-wrap gap-1">
                            @foreach($availableAreas as $area)
                            <span class="bg-amber-100 dark:bg-amber-800 text-amber-800 dark:text-amber-200 px-2 py-0.5 rounded-full text-xs">{{ $area->area_name }}</span>
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="space-y-4">
            <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-4">
                <div class="flex items-center space-x-2 mb-3">
                    <div class="flex-shrink-0 w-6 h-6 bg-blue-100 dark:bg-blue-900/30 rounded-full flex items-center justify-center">
                        <span class="text-blue-600 dark:text-blue-400 font-semibold text-xs">2</span>
                    </div>
                    <h3 class="text-sm font-semibold text-gray-900 dark:text-white">Upload File</h3>
                </div>

                <div class="upload-area border-2 border-dashed border-gray-300 dark:border-gray-600 rounded p-3 text-center hover:border-blue-400 dark:hover:border-blue-500 transition-colors duration-200 {{ $uploadFile ? 'border-green-400 bg-green-50 dark:bg-green-900/20' : '' }}">
                    <div class="space-y-2">
                        @if($uploadFile)
                        <div class="mx-auto w-12 h-12 bg-green-100 dark:bg-green-900/30 rounded-full flex items-center justify-center">
                            <svg class="w-6 h-6 text-green-600 dark:text-green-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                            </svg>
                        </div>
                        <div>
                            <h4 class="text-sm font-medium text-green-900 dark:text-green-100">File Selected</h4>
                            <p class="text-xs text-green-700 dark:text-green-300 truncate" title="{{ $uploadFile->getClientOriginalName() }}">{{ $uploadFile->getClientOriginalName() }}</p>
                        </div>
                        @else
                        <div class="mx-auto w-12 h-12 bg-blue-100 dark:bg-blue-900/30 rounded-full flex items-center justify-center">
                            <svg class="w-6 h-6 text-blue-600 dark:text-blue-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" />
                            </svg>
                        </div>
                        <div>
                            <h4 class="text-sm font-medium text-gray-900 dark:text-white">Upload CSV File</h4>
                            <p class="text-xs text-gray-600 dark:text-gray-400">Drag and drop or click to browse</p>
                        </div>
                        @endif

                        <div>
                            <x-input id="uploadFile" type="file" class="block w-full text-xs text-gray-500 file:mr-2 file:py-1 file:px-2 file:rounded file:border-0 file:text-xs file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100 dark:file:bg-blue-900/30 dark:file:text-blue-300"
                                wire:model="uploadFile" accept=".csv,.xlsx,.xls" />
                            <x-input-error for="uploadFile" class="mt-1" />
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-4">
                <div class="flex items-center space-x-2 mb-3">
                    <div class="flex-shrink-0 w-6 h-6 bg-blue-100 dark:bg-blue-900/30 rounded-full flex items-center justify-center">
                        <span class="text-blue-600 dark:text-blue-400 font-semibold text-xs">3</span>
                    </div>
                    <h3 class="text-sm font-semibold text-gray-900 dark:text-white">Select Area</h3>
                </div>

                <div class="space-y-2">
                    @foreach($availableAreas as $area)
                    <label class="flex items-center space-x-2 p-2 border border-gray-200 dark:border-gray-700 rounded cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors {{ $selectedAreaId == $area->id ? 'border-blue-400 bg-blue-50 dark:bg-blue-900/20' : '' }}">
                        <input type="radio" wire:model.live="selectedAreaId" value="{{ $area->id }}" class="text-blue-600 focus:ring-blue-500">
                        <div class="flex-1">
                            <div class="text-sm font-medium text-gray-900 dark:text-white">{{ $area->area_name }}</div>
                        </div>
                        @if($selectedAreaId == $area->id)
                        <svg class="w-4 h-4 text-blue-600 dark:text-blue-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                        </svg>
                        @endif
                    </label>
                    @endforeach
                </div>
                <x-input-error for="selectedAreaId" class="mt-1" />
            </div>

            <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-4">
                <div class="flex items-center space-x-2 mb-3">
                    <div class="flex-shrink-0 w-6 h-6 bg-blue-100 dark:bg-blue-900/30 rounded-full flex items-center justify-center">
                        <span class="text-blue-600 dark:text-blue-400 font-semibold text-xs">4</span>
                    </div>
                    <h3 class="text-sm font-semibold text-gray-900 dark:text-white">Proceed</h3>
                </div>

                <x-button wire:click="goToPreview" wire:loading.attr="disabled" :disabled="!$uploadFile || !$selectedAreaId"
                    class="w-full flex items-center justify-center space-x-1 text-sm py-2 {{ (!$uploadFile || !$selectedAreaId) ? 'opacity-50 cursor-not-allowed' : '' }}">
                    <svg class="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7l5 5m0 0l-5 5m5-5H6" />
                    </svg>
                    <span>Next: Preview Data</span>
                </x-button>
            </div>
        </div>
    </div>
    @endif

    @if($uploadStage === 'preview')
    <div class="w-full">
        <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-3 mb-3">
            <div class="flex items-center justify-between mb-3">
                <div>
                    <h2 class="text-lg font-bold text-gray-900 dark:text-white">CSV Preview</h2>
                    <p class="text-xs text-gray-600 dark:text-gray-400 mt-1">Map your columns to the database fields.</p>
                </div>
                <div class="flex space-x-2">
                    <x-secondary-button wire:click="resetUploadState" class="flex items-center space-x-1 text-xs px-2 py-1">
                        <span>Cancel</span>
                    </x-secondary-button>
                </div>
            </div>

            <div class="bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg p-2 mb-3">
                <div class="flex items-center space-x-2">
                    <div class="flex-shrink-0">
                        <svg class="h-4 w-4 text-blue-600 dark:text-blue-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
                        </svg>
                    </div>
                    <div class="flex-1">
                        <h3 class="text-xs font-medium text-blue-900 dark:text-blue-100 truncate">{{ $uploadFile->getClientOriginalName() }}</h3>
                    </div>
                </div>
            </div>

            <div class="mb-3">
                <h3 class="text-sm font-semibold text-gray-900 dark:text-white mb-2">Column Mapping</h3>
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-2">
                    @foreach($csvHeaders as $header)
                    <div class="flex items-center space-x-1 p-1.5 border border-gray-200 dark:border-gray-700 rounded">
                        <div class="flex-shrink-0 w-24">
                            <span class="text-xs font-medium text-gray-700 dark:text-gray-300 truncate" title="{{ $header }}">{{ $header }}</span>
                        </div>
                        <div class="flex-1">
                            <select wire:model.live="columnMapping.{{ $header }}" class="block w-full text-xs border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white rounded shadow-sm focus:ring-blue-500 focus:border-blue-500">
                                <option value="">Ignore</option>
                                <option value="table_code">Table Code</option>
                                <option value="seating_capacity">Seating Capacity</option>
                            </select>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>

            <div class="mb-3">
                <h3 class="text-sm font-semibold text-gray-900 dark:text-white mb-2">Row Preview (First 5)</h3>
                <div class="border border-gray-200 dark:border-gray-700 rounded overflow-hidden">
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
                            <thead class="bg-gray-50 dark:bg-gray-800">
                                <tr>
                                    @foreach($csvHeaders as $header)
                                    <th class="px-2 py-1.5 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                                        {{ $header }}
                                    </th>
                                    @endforeach
                                </tr>
                            </thead>
                            <tbody class="bg-white dark:bg-gray-900 divide-y divide-gray-200 dark:divide-gray-700">
                                @foreach($previewRows as $row)
                                <tr>
                                    @foreach($row as $cell)
                                    <td class="px-2 py-1.5 text-xs text-gray-900 dark:text-gray-100 whitespace-nowrap">
                                        {{ $cell }}
                                    </td>
                                    @endforeach
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="flex justify-center pt-1">
                <x-button wire:click="startImport" wire:loading.attr="disabled" class="flex items-center space-x-1 px-4 py-1.5 text-sm">
                    <span wire:loading.remove>Start Import</span>
                    <span wire:loading>Processing...</span>
                </x-button>
            </div>
        </div>
    </div>
    @endif

    @if(in_array($uploadStage, ['processing', 'completed']))
    <div class="w-full">
        <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-4">
            <div class="text-center space-y-8">
                
                @if($uploadStage === 'processing')
                    <div class="inline-flex items-center space-x-4 bg-blue-50 dark:bg-blue-900/20 px-8 py-4 rounded-full">
                        <div class="animate-spin rounded-full h-6 w-6 border-b-2 border-blue-600"></div>
                        <span class="text-blue-800 dark:text-blue-200 font-medium text-lg">{{ $currentStage }}</span>
                    </div>
                    <div class="w-full bg-gray-200 rounded-full h-4 dark:bg-gray-700 overflow-hidden">
                         <div class="bg-blue-600 h-4 rounded-full transition-all duration-500" style="width: {{ $uploadProgress }}%"></div>
                    </div>
                @endif

                @if($uploadStage === 'completed')
                    <div class="mx-auto w-20 h-20 bg-green-100 dark:bg-green-900/30 rounded-full flex items-center justify-center">
                        <svg class="w-10 h-10 text-green-600 dark:text-green-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                        </svg>
                    </div>
                    <h3 class="text-2xl font-bold text-green-600 dark:text-green-400 mb-2">Import Completed!</h3>
                    
                    <div class="bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-xl p-6">
                        <div class="grid grid-cols-3 gap-4">
                            <div class="text-center">
                                <div class="text-2xl font-bold text-green-600">{{ $uploadResults['total'] }}</div>
                                <div class="text-sm text-green-700">Total</div>
                            </div>modules.table.bulkImport
                                <div class="text-2xl font-bold text-green-600">{{ $uploadResults['success'] }}</div>
                                <div class="text-sm text-green-700">Success</div>
                            </div>
                            <div class="text-center">
                                <div class="text-2xl font-bold text-yellow-600">{{ $uploadResults['skipped'] }}</div>
                                <div class="text-sm text-yellow-700">Skipped</div>
                            </div>
                        </div>
                    </div>

                    @if(!empty($uploadErrors))
                    <div class="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-xl p-4 text-left mt-4">
                        <h4 class="text-lg font-semibold text-red-800 mb-2">Errors</h4>
                        <ul class="list-disc list-inside text-sm text-red-700">
                            @foreach($uploadErrors as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                    @endif

                    <div class="mt-6">
                         <x-button href="{{ route('tables.index') }}" wire:navigate>Back to Tables</x-button>
                    </div>
                @endif
            </div>
        </div>
    </div>
    @endif

    </div>
</div>