<x-app-layout>

    <x-module-hero
        title="Pages Management"
        description="Create and manage dynamic publishing pages for the website"
        icon="fa-file-alt"
    >
        <x-slot name="tabs">
            @can('view pages')
            <a href="{{ route('admin.pages.index') }}"
               class="module-hero-tab {{ request()->routeIs('admin.pages.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-list text-sm"></i> All Pages
            </a>
            @endcan
            @can('create pages')
            <a href="{{ route('admin.pages.create') }}"
               class="module-hero-tab {{ request()->routeIs('admin.pages.create') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-plus text-sm"></i> Create Page
            </a>
            @endcan
            @role('Manager|Super Manager')
            <a href="{{ route('admin.sliders.index') }}"
               class="module-hero-tab {{ request()->routeIs('admin.sliders.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-images text-sm"></i> Hero Slider
            </a>
            <a href="{{ route('admin.hero-cards.index') }}"
               class="module-hero-tab {{ request()->routeIs('admin.hero-cards.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-th-large text-sm"></i> Hero Cards
            </a>
            @endrole
            @can('manage what we do images')
            <a href="{{ route('admin.what-we-do-images.index') }}"
               class="module-hero-tab {{ request()->routeIs('admin.what-we-do-images.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-image text-sm"></i> What We Do Images
            </a>
            @endcan
        </x-slot>
        <x-slot name="actions">
            @can('create pages')
            <a href="{{ route('admin.pages.create') }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-white text-[#1a4731] text-sm font-semibold rounded-lg hover:bg-white/90 transition">
                <i class="fas fa-plus"></i> Create Page
            </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="p-6">

        @if(session('success'))
            <div class="mb-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center gap-2">
                <i class="fas fa-check-circle"></i> {{ session('success') }}
            </div>
        @endif
        @if(session('error'))
            <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-2">
                <i class="fas fa-exclamation-circle"></i> {{ session('error') }}
            </div>
        @endif

        {{-- Stats row --}}
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center">
                    <i class="fas fa-file-alt text-blue-600 text-xl"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium uppercase tracking-wide">Total Pages</p>
                    <p class="text-2xl font-bold text-gray-800">{{ $stats['total'] ?? 0 }}</p>
                </div>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
                <div class="w-12 h-12 rounded-full bg-green-100 flex items-center justify-center">
                    <i class="fas fa-globe text-green-600 text-xl"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium uppercase tracking-wide">Published</p>
                    <p class="text-2xl font-bold text-gray-800">{{ $stats['published'] ?? 0 }}</p>
                </div>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
                <div class="w-12 h-12 rounded-full bg-yellow-100 flex items-center justify-center">
                    <i class="fas fa-pencil-alt text-yellow-600 text-xl"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium uppercase tracking-wide">Drafts</p>
                    <p class="text-2xl font-bold text-gray-800">{{ $stats['drafts'] ?? 0 }}</p>
                </div>
            </div>
        </div>

        {{-- Search/Filter --}}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 mb-4">
            <form method="GET" class="flex flex-wrap gap-3 items-end">
                <div class="flex-1 min-w-48">
                    <label class="block text-xs font-semibold text-gray-600 mb-1">Search</label>
                    <input type="text" name="search" value="{{ request('search') }}"
                           placeholder="Search pages by title or slug..."
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-gray-600 mb-1">Status</label>
                    <select name="status" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-transparent">
                        <option value="">All Statuses</option>
                        <option value="published" {{ request('status') == 'published' ? 'selected' : '' }}>Published</option>
                        <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>Drafts</option>
                    </select>
                </div>
                <button type="submit" class="px-4 py-2 bg-[#1a4731] text-white text-sm font-medium rounded-lg hover:bg-[#276749] transition">
                    <i class="fas fa-search mr-1"></i> Filter
                </button>
                @if(request('search') || request('status'))
                <a href="{{ route('admin.pages.index') }}" class="px-4 py-2 bg-gray-100 text-gray-700 text-sm font-medium rounded-lg hover:bg-gray-200 transition">
                    <i class="fas fa-times mr-1"></i> Clear
                </a>
                @endif
            </form>
        </div>

        {{-- Table --}}
        <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full table-zebra">
                    <thead>
                        <tr>
                            <th class="px-4 py-3 text-left th-col-1">Page Title</th>
                            <th class="px-4 py-3 text-left th-col-2">Slug</th>
                            <th class="px-4 py-3 text-left th-col-3">Status</th>
                            <th class="px-4 py-3 text-left th-col-4">Created By</th>
                            <th class="px-4 py-3 text-left th-col-5">Updated</th>
                            <th class="px-4 py-3 text-center th-col-6">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($pages as $page)
                            <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                                <td class="px-4 py-3">
                                    <div class="font-medium text-gray-800">{{ $page->title }}</div>
                                </td>
                                <td class="px-4 py-3">
                                    <code class="text-xs bg-gray-100 px-2 py-1 rounded">{{ $page->slug }}</code>
                                </td>
                                <td class="px-4 py-3">
                                    @if($page->is_published)
                                        <span class="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-semibold bg-green-100 text-green-800">
                                            <i class="fas fa-circle text-green-500" style="font-size:6px"></i> Published
                                        </span>
                                    @else
                                        <span class="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-semibold bg-gray-100 text-gray-600">
                                            <i class="fas fa-circle text-gray-400" style="font-size:6px"></i> Draft
                                        </span>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-sm text-gray-600">
                                    {{ $page->creator->name ?? 'System' }}
                                </td>
                                <td class="px-4 py-3 text-sm text-gray-500">
                                    {{ $page->updated_at->diffForHumans() }}
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex items-center justify-center gap-1 flex-wrap">
                                        @if($page->is_published)
                                        <a href="{{ route('pages.show', $page->slug) }}" target="_blank"
                                           class="btn-action-view" title="View Public">
                                            <i class="fas fa-external-link-alt"></i>
                                        </a>
                                        @endif
                                        <a href="{{ route('admin.pages.preview', $page) }}" target="_blank"
                                           class="btn-action-view" title="Preview">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        @can('edit pages')
                                        <a href="{{ route('admin.pages.edit', $page) }}"
                                           class="btn-action-edit">
                                            <i class="fas fa-edit"></i> Edit
                                        </a>
                                        <form action="{{ route('admin.pages.toggle-publish', $page) }}" method="POST" class="inline">
                                            @csrf
                                            <button type="submit"
                                                    class="{{ $page->is_published ? 'btn-action-reject' : 'btn-action-approve' }}"
                                                    title="{{ $page->is_published ? 'Unpublish' : 'Publish' }}">
                                                <i class="fas fa-{{ $page->is_published ? 'toggle-on' : 'toggle-off' }}"></i>
                                                {{ $page->is_published ? 'Unpublish' : 'Publish' }}
                                            </button>
                                        </form>
                                        @endcan
                                        @can('delete pages')
                                        <form action="{{ route('admin.pages.destroy', $page) }}" method="POST" class="inline"
                                              onsubmit="return confirm('Delete this page? This cannot be undone.')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn-action-delete">
                                                <i class="fas fa-trash"></i> Delete
                                            </button>
                                        </form>
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr class="odd:bg-white even:bg-gray-50 hover:bg-gray-100 transition-colors border-b border-gray-100 last:border-0">
                                <td colspan="6" class="px-4 py-12 text-center">
                                    <i class="fas fa-file-alt text-4xl text-gray-300 mb-3 block"></i>
                                    <p class="text-gray-500">No pages found.</p>
                                    @can('create pages')
                                    <a href="{{ route('admin.pages.create') }}" class="mt-3 inline-flex items-center gap-2 px-4 py-2 bg-[#1a4731] text-white text-sm rounded-lg hover:bg-[#276749] transition">
                                        <i class="fas fa-plus"></i> Create First Page
                                    </a>
                                    @endcan
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="px-4 py-3 border-t border-gray-200">
                {{ $pages->withQueryString()->links() }}
            </div>
        </div>

    </div>
</x-app-layout>
