<x-app-layout>
    <x-slot name="header">
        <div class="flex items-center">
            <a href="{{ route('imports.create') }}" class="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300 me-4">
                <i class="fas fa-arrow-left"></i>
            </a>
            <h2 class="text-xl font-bold text-gray-900 dark:text-white">
                {{ __('Preview Import') }}
            </h2>
        </div>
    </x-slot>

    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <!-- Summary -->
        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm p-6 mb-6">
            <div class="grid grid-cols-1 sm:grid-cols-4 gap-4">
                <div class="text-center">
                    <p class="text-3xl font-bold text-gray-900 dark:text-white">{{ $totalRows ?? 0 }}</p>
                    <p class="text-sm text-gray-500">Total Rows</p>
                </div>
                <div class="text-center">
                    <p class="text-3xl font-bold text-green-600">{{ $validRows ?? 0 }}</p>
                    <p class="text-sm text-gray-500">Valid</p>
                </div>
                <div class="text-center">
                    <p class="text-3xl font-bold text-red-600">{{ $errorRows ?? 0 }}</p>
                    <p class="text-sm text-gray-500">Errors</p>
                </div>
                <div class="text-center">
                    <p class="text-3xl font-bold text-yellow-600">{{ $duplicateRows ?? 0 }}</p>
                    <p class="text-sm text-gray-500">Duplicates</p>
                </div>
            </div>
        </div>

        <!-- Preview Table -->
        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm overflow-hidden mb-6">
            <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-900">
                        <tr>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
                            <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">Description</th>
                            <th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase">Amount</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">Category</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100 dark:divide-gray-700">
                        @foreach($preview ?? [] as $row)
                            <tr class="{{ $row['status'] === 'error' ? 'bg-red-50 dark:bg-red-900/10' : '' }}">
                                <td class="px-4 py-3">
                                    @if($row['status'] === 'valid')
                                        <span class="text-green-500"><i class="fas fa-check-circle"></i></span>
                                    @elseif($row['status'] === 'duplicate')
                                        <span class="text-yellow-500"><i class="fas fa-exclamation-circle"></i></span>
                                    @else
                                        <span class="text-red-500" title="{{ $row['error'] ?? '' }}"><i class="fas fa-times-circle"></i></span>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-sm text-gray-900 dark:text-white">{{ $row['date'] ?? '' }}</td>
                                <td class="px-4 py-3 text-sm text-gray-900 dark:text-white">{{ $row['description'] ?? '' }}</td>
                                <td class="px-4 py-3 text-sm text-right {{ ($row['type'] ?? '') === 'expense' ? 'text-red-600' : 'text-green-600' }}">
                                    {{ number_format($row['amount'] ?? 0, 0, ',', '.') }}
                                </td>
                                <td class="px-4 py-3 text-sm text-gray-600 dark:text-gray-400">{{ ucfirst($row['type'] ?? '') }}</td>
                                <td class="px-4 py-3 text-sm text-gray-600 dark:text-gray-400">{{ $row['category'] ?? '' }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Actions -->
        <div class="flex items-center justify-end space-x-4">
            <a href="{{ route('imports.create') }}" class="px-4 py-2 text-gray-600 hover:text-gray-800">
                <i class="fas fa-arrow-left me-2"></i>Back
            </a>
            <form action="{{ route('imports.process') }}" method="POST">
                @csrf
                <input type="hidden" name="import_id" value="{{ $importId ?? '' }}">
                <x-primary-button>
                    <i class="fas fa-check me-2"></i>Import {{ $validRows ?? 0 }} Rows
                </x-primary-button>
            </form>
        </div>
    </div>
</x-app-layout>
