<x-app-layout>

    <x-module-hero
        title="Research & Innovation Centre"
        description="Manage your research publications and submissions"
        icon="fa-microscope">

        <x-slot name="tabs">
            @can('view own publications')
            <a href="{{ route('research.index') }}"
               class="module-hero-tab {{ request()->routeIs('research.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> My Publications
            </a>
            @endcan

            @can('create publications')
            <a href="{{ route('research.create') }}"
               class="module-hero-tab {{ request()->routeIs('research.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> New Publication
            </a>
            @endcan

            @can('review publications')
            <a href="{{ route('research.review.index') }}"
               class="module-hero-tab {{ request()->routeIs('research.review.*') ? '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-clipboard-check text-sm"></i> Review Queue
            </a>
            @endcan
        </x-slot>

        @can('create publications')
        <x-slot name="actions">
            <a href="{{ route('research.create') }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-white text-green-800 text-sm font-semibold rounded-lg hover:bg-green-50 transition">
                <i class="fas fa-plus"></i> New Publication
            </a>
        </x-slot>
        @endcan

    </x-module-hero>

    {{-- Main Content --}}
    <div class="w-full">

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

        {{-- Search / Filter Bar --}}
        <div class="p-4 bg-white border-b border-gray-200">
            <form action="{{ route('research.index') }}" method="GET" class="flex flex-wrap gap-3 items-end">
                <div>
                    <label class="block text-xs font-medium 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-green-500">
                        <option value="">All Statuses</option>
                        <option value="draft"            {{ ($filters['status'] ?? '') === 'draft'            ? 'selected' : '' }}>Draft</option>
                        <option value="under_review"     {{ ($filters['status'] ?? '') === 'under_review'     ? 'selected' : '' }}>Under Review</option>
                        <option value="revision_required"{{ ($filters['status'] ?? '') === 'revision_required'? 'selected' : '' }}>Revision Required</option>
                        <option value="approved"         {{ ($filters['status'] ?? '') === 'approved'         ? 'selected' : '' }}>Approved</option>
                        <option value="featured"         {{ ($filters['status'] ?? '') === 'featured'         ? 'selected' : '' }}>Featured</option>
                    </select>
                </div>
                <div>
                    <label class="block text-xs font-medium text-gray-600 mb-1">Category</label>
                    <select name="category" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-green-500 focus:border-green-500">
                        <option value="">All Categories</option>
                        <option value="clinical" {{ ($filters['category'] ?? '') === 'clinical' ? 'selected' : '' }}>Clinical Research</option>
                        <option value="tech"     {{ ($filters['category'] ?? '') === 'tech'     ? 'selected' : '' }}>Technology & Innovation</option>
                        <option value="social"   {{ ($filters['category'] ?? '') === 'social'   ? 'selected' : '' }}>Social Sciences</option>
                    </select>
                </div>
                <div class="flex gap-2">
                    <button type="submit" class="px-4 py-2 bg-green-700 text-white text-sm rounded-lg hover:bg-green-800 transition">
                        <i class="fas fa-filter mr-1"></i> Filter
                    </button>
                    @if(!empty($filters['status']) || !empty($filters['category']))
                    <a href="{{ route('research.index') }}" class="px-4 py-2 bg-gray-100 text-gray-700 text-sm rounded-lg hover:bg-gray-200 transition">
                        Clear
                    </a>
                    @endif
                </div>
            </form>
        </div>

        {{-- Table --}}
        @if($publications->count() > 0)
        <div class="overflow-x-auto">
            <table class="w-full table-zebra">
                <thead>
                    <tr>
                        <th class="px-4 py-3 text-left th-col-1">Publication</th>
                        <th class="px-4 py-3 text-left th-col-2">Category</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</th>
                        <th class="px-4 py-3 text-center th-col-5">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($publications as $publication)
                    <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="flex items-center gap-3">
                                @if($publication->cover_image)
                                <img src="{{ Storage::url($publication->cover_image) }}"
                                     alt="{{ $publication->title }}"
                                     class="w-10 h-10 object-cover rounded-lg flex-shrink-0">
                                @else
                                <div class="w-10 h-10 bg-gray-100 rounded-lg flex items-center justify-center flex-shrink-0">
                                    <i class="fas fa-file-alt text-gray-400 text-sm"></i>
                                </div>
                                @endif
                                <div>
                                    <a href="{{ route('research.show', $publication) }}"
                                       class="font-medium text-gray-900 hover:text-green-700 transition text-sm">
                                        {{ Str::limit($publication->title, 55) }}
                                    </a>
                                    <p class="text-xs text-gray-500 mt-0.5">{{ Str::limit($publication->abstract, 60) }}</p>
                                </div>
                            </div>
                        </td>
                        <td class="px-4 py-3">
                            <span class="px-2 py-1 bg-gray-100 text-gray-700 text-xs font-medium rounded-full">
                                {{ $publication->category_label }}
                            </span>
                        </td>
                        <td class="px-4 py-3">
                            @php
                            $statusColors = [
                                'draft'             => 'bg-gray-100 text-gray-700',
                                'under_review'      => 'bg-blue-100 text-blue-700',
                                'revision_required' => 'bg-yellow-100 text-yellow-700',
                                'approved'          => 'bg-green-100 text-green-700',
                                'featured'          => 'bg-purple-100 text-purple-700',
                            ];
                            @endphp
                            <span class="px-2 py-1 {{ $statusColors[$publication->status] ?? 'bg-gray-100 text-gray-700' }} text-xs font-semibold rounded-full capitalize">
                                {{ str_replace('_', ' ', $publication->status) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-500">
                            {{ $publication->created_at->format('M d, Y') }}
                        </td>
                        <td class="px-4 py-3">
                            <div class="flex items-center justify-center gap-1 flex-wrap">
                                <a href="{{ route('research.show', $publication) }}" class="btn-action-view">
                                    <i class="fas fa-eye"></i> View
                                </a>
                                @if($publication->isEditable())
                                <a href="{{ route('research.edit', $publication) }}" class="btn-action-edit">
                                    <i class="fas fa-edit"></i> Edit
                                </a>
                                @endif
                                @if($publication->canSubmit())
                                <form action="{{ route('research.submit', $publication) }}" method="POST" class="inline">
                                    @csrf
                                    <button type="submit" class="btn-action-approve">
                                        <i class="fas fa-paper-plane"></i> Submit
                                    </button>
                                </form>
                                @endif
                                @if($publication->status !== 'under_review')
                                <form action="{{ route('research.destroy', $publication) }}" method="POST" class="inline"
                                      onsubmit="return confirm('Delete this publication?')">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn-action-delete">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                                @endif
                            </div>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        <div class="px-4 py-3 border-t border-gray-200 bg-white">
            {{ $publications->withQueryString()->links() }}
        </div>
        @else
        <div class="text-center py-16 bg-white">
            <i class="fas fa-folder-open text-gray-300 text-5xl mb-4"></i>
            <h3 class="text-lg font-semibold text-gray-700 mb-2">No publications found</h3>
            <p class="text-gray-500 mb-6 text-sm">
                @if(!empty($filters['status']) || !empty($filters['category']))
                    Try adjusting your filters
                @else
                    Start by creating your first research publication
                @endif
            </p>
            @can('create publications')
            <a href="{{ route('research.create') }}"
               class="inline-flex items-center gap-2 px-5 py-2.5 bg-green-700 text-white rounded-lg hover:bg-green-800 transition text-sm font-medium">
                <i class="fas fa-plus"></i> Create New Publication
            </a>
            @endcan
        </div>
        @endif

    </div>

</x-app-layout>
