<div>
    <x-layouts.app title="{{ __('Categories') }}">
        <!-- Page Header -->
        <div class="page-header d-flex justify-content-between align-items-center">
            <div>
                <h1 class="page-title">{{ __('Categories') }}</h1>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">{{ __('Dashboard') }}</a></li>
                        <li class="breadcrumb-item active">{{ __('Categories') }}</li>
                    </ol>
                </nav>
            </div>
            <div class="d-flex gap-2">
                <button wire:click="export" class="btn btn-outline-success">
                    <i class="fas fa-file-excel me-1"></i>{{ __('Export') }}
                </button>
                <button wire:click="$set('showImportModal', true)" class="btn btn-outline-info">
                    <i class="fas fa-file-import me-1"></i>{{ __('Import') }}
                </button>
                <a href="{{ route('categories.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-1"></i>{{ __('Add Category') }}
                </a>
            </div>
        </div>

        <!-- Search -->
        <div class="card mb-4">
            <div class="card-body py-3">
                <div class="row align-items-center">
                    <div class="col-md-6 d-flex align-items-center">
                        <div class="form-check me-3">
                            <input class="form-check-input" type="checkbox" wire:model.live="selectAll" id="selectAll">
                            <label class="form-check-label small" for="selectAll">{{ __('Select All') }}</label>
                        </div>
                        <div class="input-group flex-grow-1">
                            <span class="input-group-text"><i class="fas fa-search"></i></span>
                            <input type="text" class="form-control" 
                                   wire:model.live.debounce.300ms="search" 
                                   placeholder="{{ __('Search categories...') }}">
                        </div>
                    </div>
                    <div class="col-md-6 text-end">
                        @if(count($selectedRows) > 0)
                            <span class="badge bg-info me-2">{{ count($selectedRows) }} {{ __('selected') }}</span>
                        @endif
                        <span class="text-muted">{{ __('Drag to reorder categories') }}</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Categories Grid -->
        <div class="row g-4">
            @forelse($categories as $category)
                <div class="col-md-6 col-lg-4">
                    <div class="card h-100 {{ in_array($category->id, $selectedRows) ? 'border-primary shadow-sm' : '' }}">
                        <div class="card-body">
                            <div class="d-flex align-items-start justify-content-between">
                                <div class="d-flex align-items-center">
                                    <div class="form-check me-2">
                                        <input class="form-check-input" type="checkbox" wire:model.live="selectedRows" value="{{ $category->id }}">
                                    </div>
                                    <div class="rounded-circle d-flex align-items-center justify-content-center me-3"
                                         style="width: 48px; height: 48px; background-color: {{ $category->color }}20; color: {{ $category->color }};">
                                        <i class="{{ $category->icon ?? 'fas fa-folder' }} fa-lg"></i>
                                    </div>
                                    <div>
                                        <h5 class="mb-1">{{ $category->localized_name }}</h5>
                                        <span class="badge {{ $category->is_active ? 'bg-success' : 'bg-secondary' }}">
                                            {{ $category->is_active ? __('Active') : __('Inactive') }}
                                        </span>
                                    </div>
                                </div>
                                <div class="dropdown">
                                    <button class="btn btn-link text-muted p-0" type="button" data-bs-toggle="dropdown">
                                        <i class="fas fa-ellipsis-v"></i>
                                    </button>
                                    <ul class="dropdown-menu dropdown-menu-end">
                                        <li>
                                            <a class="dropdown-item" href="{{ route('categories.edit', $category) }}">
                                                <i class="fas fa-edit me-2"></i>{{ __('Edit') }}
                                            </a>
                                        </li>
                                        <li><hr class="dropdown-divider"></li>
                                        <li>
                                            <button class="dropdown-item text-danger" wire:click="confirmDelete({{ $category->id }})">
                                                <i class="fas fa-trash me-2"></i>{{ __('Delete') }}
                                            </button>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            
                            @if($category->description)
                                <p class="text-muted mt-3 mb-0 small">
                                    {{ \Str::limit($category->localized_description, 100) }}
                                </p>
                            @endif
                        </div>
                        <div class="card-footer bg-transparent">
                            <div class="d-flex justify-content-between align-items-center text-muted small">
                                <span>
                                    <i class="fas fa-box me-1"></i>{{ $category->products_count }} {{ __('products') }}
                                </span>
                                <span>
                                    <i class="fas fa-sort me-1"></i>{{ __('Order') }}: {{ $category->sort_order }}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            @empty
                <div class="col-12">
                    <div class="card">
                        <div class="card-body text-center py-5">
                            <i class="fas fa-folder-open fa-3x text-muted mb-3"></i>
                            <p class="text-muted mb-0">{{ __('No categories found.') }}</p>
                            <a href="{{ route('categories.create') }}" class="btn btn-primary btn-sm mt-3">
                                <i class="fas fa-plus me-1"></i>{{ __('Add First Category') }}
                            </a>
                        </div>
                    </div>
                </div>
            @endforelse
        </div>

        @endif

        <!-- Import Modal -->
        @if($showImportModal)
            <div class="modal fade show d-block" tabindex="-1" style="background: rgba(0,0,0,0.5);">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">{{ __('Import Categories') }}</h5>
                            <button type="button" class="btn-close" wire:click="$set('showImportModal', false)"></button>
                        </div>
                        <div class="modal-body">
                            <div class="mb-3">
                                <label class="form-label">{{ __('Excel/CSV File') }}</label>
                                <input type="file" class="form-control" wire:model="importFile">
                                <div wire:loading wire:target="importFile" class="text-info small mt-1">
                                    <i class="fas fa-spinner fa-spin me-1"></i>{{ __('Uploading...') }}
                                </div>
                                @error('importFile') <span class="text-danger small">{{ $message }}</span> @enderror
                            </div>
                            <div class="alert alert-info small">
                                <i class="fas fa-info-circle me-1"></i>
                                {{ __('Download the template to ensure correct column mapping.') }}
                                <div class="mt-2">
                                    <button type="button" class="btn btn-sm btn-outline-primary" wire:click="downloadTemplate">
                                        <i class="fas fa-download me-1"></i>{{ __('Download Template') }}
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" wire:click="$set('showImportModal', false)">
                                {{ __('Cancel') }}
                            </button>
                            <button type="button" class="btn btn-primary" wire:click="import" wire:loading.attr="disabled">
                                <span wire:loading.remove wire:target="import">
                                    <i class="fas fa-file-import me-1"></i>{{ __('Import') }}
                                </span>
                                <span wire:loading wire:target="import">
                                    <i class="fas fa-spinner fa-spin me-1"></i>{{ __('Importing...') }}
                                </span>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        @endif
    </x-layouts.app>
</div>
