@extends('layouts.admin')

@section('title', 'Categories')

@section('content')
<div class="max-w-[1600px] mx-auto">
    <div class="flex flex-col md:flex-row md:items-center justify-between gap-6 mb-10">
        <div class="space-y-1">
            <h1 class="text-3xl font-bold text-gray-900 tracking-tight">Categories</h1>
            <p class="text-gray-500 text-sm">Organize your catalog with clean, simple categories.</p>
        </div>
        <div>
            <a href="{{ route('admin.categories.create') }}" class="inline-flex items-center gap-2 px-5 py-3 btn-brand text-white rounded-xl text-sm font-semibold shadow-md shadow-brand/20">
                <i class="fas fa-plus text-[12px]"></i>
                Add Category
            </a>
        </div>
    </div>

    @if(session('success'))
        <div x-data="{ show: true }" x-show="show" class="bg-emerald-50 text-emerald-800 px-5 py-4 rounded-xl mb-8 border border-emerald-100 flex items-center justify-between">
            <div class="flex items-center gap-3">
                <i class="fas fa-check-circle"></i>
                <p class="text-sm font-medium">Success: {{ session('success') }}</p>
            </div>
            <button @click="show = false" class="w-8 h-8 flex items-center justify-center hover:bg-emerald-100 rounded-lg transition-colors">
                <i class="fas fa-times text-xs"></i>
            </button>
        </div>
    @endif

    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
        <div class="bg-white rounded-2xl p-6 border border-gray-100">
            <div class="flex items-center gap-3 text-gray-500 mb-2">
                <i class="fas fa-sitemap"></i>
                <span class="text-sm font-medium">Total categories</span>
            </div>
            <div class="text-3xl font-bold text-gray-900">{{ $categories->total() }}</div>
        </div>
        <div class="bg-white rounded-2xl p-6 border border-gray-100">
            <div class="flex items-center gap-3 text-gray-500 mb-2">
                <i class="fas fa-check-circle"></i>
                <span class="text-sm font-medium">Active categories</span>
            </div>
            <div class="text-3xl font-bold text-gray-900">{{ $categories->where('is_active', true)->count() }}</div>
        </div>
        <div class="bg-white rounded-2xl p-6 border border-gray-100">
            <div class="flex items-center gap-3 text-gray-500 mb-2">
                <i class="fas fa-box"></i>
                <span class="text-sm font-medium">Total products</span>
            </div>
            <div class="text-3xl font-bold text-gray-900">{{ number_format($categories->sum('products_count')) }}</div>
        </div>
    </div>

    <div class="bg-white p-5 mb-8 rounded-2xl border border-gray-100 shadow-sm flex flex-col md:flex-row items-center justify-between gap-4">
        <div class="relative w-full md:w-96">
            <i class="fas fa-search absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 text-sm"></i>
            <input type="text" class="w-full pl-10 pr-4 py-3 bg-white border border-gray-200 rounded-xl text-sm placeholder:text-gray-400 focus:border-emerald-300 focus:ring-0 transition" placeholder="Search categories...">
        </div>
        <div class="relative">
            <select class="appearance-none pl-4 pr-10 py-3 bg-white border border-gray-200 rounded-xl text-sm text-gray-700 focus:border-emerald-300 focus:ring-0 cursor-pointer min-w-[180px]">
                <option>All Status</option>
                <option>Active</option>
                <option>Inactive</option>
            </select>
            <i class="fas fa-chevron-down absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 text-xs pointer-events-none"></i>
        </div>
    </div>

    <div class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden mb-10">
        <div class="p-6">
            @if($categories->count())
            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
                @foreach($categories as $category)
                <div class="group border border-gray-100 rounded-2xl overflow-hidden bg-white hover:shadow-md transition-all">
                    <div class="relative aspect-[4/3] bg-gray-50 overflow-hidden">
                        @if($category->image)
                            @php
                                $imagePath = $category->image;
                                $imageUrl = Str::startsWith($imagePath, ['http://', 'https://'])
                                    ? $imagePath
                                    : route('public.media', ['path' => $imagePath]);
                            @endphp
                            <img class="h-full w-full object-cover group-hover:scale-105 transition-transform duration-500" src="{{ $imageUrl }}" alt="{{ $category->name }}">
                        @else
                            <div class="h-full w-full flex items-center justify-center text-gray-300">
                                <i class="fas fa-image text-3xl"></i>
                            </div>
                        @endif
                        @if(!$category->is_active)
                            <span class="absolute top-3 left-3 px-2.5 py-1 rounded-lg bg-gray-900/80 text-white text-[10px] font-semibold">Inactive</span>
                        @endif
                    </div>
                    <div class="p-4">
                        <div class="text-sm font-semibold text-gray-900 line-clamp-2">{{ $category->name }}</div>
                        <div class="mt-1 text-xs text-gray-500">{{ $category->slug }}</div>
                        <div class="mt-3 flex items-center justify-between">
                            <span class="text-xs text-gray-500">{{ number_format($category->products_count ?? 0) }} products</span>
                            <div class="flex items-center gap-2">
                                <a href="{{ route('admin.categories.edit', $category) }}" class="h-9 w-9 flex items-center justify-center text-gray-400 hover:text-brand hover:bg-emerald-50 rounded-xl transition-all" title="Edit">
                                    <i class="fas fa-edit text-sm"></i>
                                </a>
                                <form action="{{ route('admin.categories.destroy', $category) }}" method="POST" class="inline-block" onsubmit="return confirm('Are you sure you want to delete this category?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="h-9 w-9 flex items-center justify-center text-gray-400 hover:text-red-500 hover:bg-red-50 rounded-xl transition-all" title="Delete">
                                        <i class="fas fa-trash-alt text-sm"></i>
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                @endforeach
            </div>
            @else
            <div class="py-16 text-center">
                <div class="max-w-sm mx-auto">
                    <i class="fas fa-folder-open text-gray-300 text-5xl mb-4"></i>
                    <h3 class="text-xl font-semibold text-gray-900 mb-2">No categories</h3>
                    <p class="text-gray-500 text-sm mb-6">Create your first category to organize products.</p>
                    <a href="{{ route('admin.categories.create') }}" class="inline-flex items-center gap-2 px-5 py-3 btn-brand text-white rounded-xl text-sm font-semibold shadow-md shadow-brand/20">
                        <i class="fas fa-plus text-[12px]"></i>
                        Add First Category
                    </a>
                </div>
            </div>
            @endif
        </div>
        @if($categories->hasPages())
        <div class="px-6 py-5 border-t border-gray-100 bg-gray-50/50">
            {{ $categories->links() }}
        </div>
        @endif
    </div>
</div>
@endsection
