<x-app-layout>

    <x-module-hero
        title="Advocacy Campaigns"
        description="Manage health advocacy campaigns"
        icon="fa-bullhorn">

        <x-slot name="tabs">
            @can('view campaigns')
            <a href="{{ route('advocacy.campaigns.index') }}"
               class="module-hero-tab {{ request()->routeIs('advocacy.campaigns.*') ? '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-bullhorn text-sm"></i> Campaigns
            </a>
            @endcan

            @can('view polls')
            <a href="{{ route('advocacy.polls.index') }}"
               class="module-hero-tab {{ request()->routeIs('advocacy.polls.*') ? '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-poll text-sm"></i> Polls
            </a>
            @endcan

            @can('view stories')
            <a href="{{ route('advocacy.stories.index') }}"
               class="module-hero-tab {{ request()->routeIs('advocacy.stories.*') ? '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-newspaper text-sm"></i> Health Stories
            </a>
            @endcan
        </x-slot>

        @can('create campaigns')
        <x-slot name="actions">
            <a href="{{ route('advocacy.campaigns.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 Campaign
            </a>
        </x-slot>
        @endcan

    </x-module-hero>

    <div class="w-full">

        @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 text-sm">
            <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 text-sm">
            <i class="fas fa-exclamation-circle mr-3"></i> {{ session('error') }}
        </div>
        @endif

        {{-- Filter Bar --}}
        <div class="p-4 bg-white border-b border-gray-200">
            <form 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-teal-500">
                        <option value="">All Status</option>
                        <option value="draft"     {{ ($filters['status'] ?? '') == 'draft'     ? 'selected' : '' }}>Draft</option>
                        <option value="active"    {{ ($filters['status'] ?? '') == 'active'    ? 'selected' : '' }}>Active</option>
                        <option value="paused"    {{ ($filters['status'] ?? '') == 'paused'    ? 'selected' : '' }}>Paused</option>
                        <option value="completed" {{ ($filters['status'] ?? '') == 'completed' ? 'selected' : '' }}>Completed</option>
                    </select>
                </div>
                <div class="flex gap-2">
                    <button type="submit" class="px-4 py-2 bg-teal-700 text-white text-sm rounded-lg hover:bg-teal-800 transition">
                        <i class="fas fa-filter mr-1"></i> Filter
                    </button>
                    @if(!empty($filters['status']))
                    <a href="{{ route('advocacy.campaigns.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 --}}
        <div class="overflow-x-auto">
            <table class="w-full table-zebra">
                <thead>
                    <tr>
                        <th class="px-4 py-3 text-left th-col-1">Campaign</th>
                        <th class="px-4 py-3 text-left th-col-2">Status</th>
                        <th class="px-4 py-3 text-left th-col-3">Duration</th>
                        <th class="px-4 py-3 text-center th-col-4">Stories</th>
                        <th class="px-4 py-3 text-center th-col-5">Views</th>
                        <th class="px-4 py-3 text-center th-col-6">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($campaigns as $campaign)
                    <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($campaign->thumbnail)
                                <img src="{{ asset('storage/' . $campaign->thumbnail) }}"
                                     alt="{{ $campaign->title }}"
                                     class="w-10 h-10 rounded-lg object-cover flex-shrink-0">
                                @else
                                <div class="w-10 h-10 bg-teal-100 rounded-lg flex items-center justify-center flex-shrink-0">
                                    <i class="fas fa-bullhorn text-teal-600 text-sm"></i>
                                </div>
                                @endif
                                <div>
                                    <div class="text-sm font-medium text-gray-900">
                                        {{ Str::limit($campaign->title, 45) }}
                                        @if($campaign->is_featured)
                                        <i class="fas fa-star text-yellow-500 ml-1 text-xs"></i>
                                        @endif
                                    </div>
                                    <div class="text-xs text-gray-500">by {{ $campaign->user->name }}</div>
                                </div>
                            </div>
                        </td>
                        <td class="px-4 py-3">
                            @php
                            $statusColors = [
                                'draft'     => 'bg-gray-100 text-gray-700',
                                'active'    => 'bg-green-100 text-green-700',
                                'paused'    => 'bg-yellow-100 text-yellow-700',
                                'completed' => 'bg-blue-100 text-blue-700',
                            ];
                            @endphp
                            <span class="px-2 py-1 text-xs font-semibold rounded-full {{ $statusColors[$campaign->status] ?? 'bg-gray-100 text-gray-700' }}">
                                {{ ucfirst($campaign->status) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-700">
                            {{ $campaign->start_date->format('M d') }}
                            @if($campaign->end_date) – {{ $campaign->end_date->format('M d') }} @endif
                        </td>
                        <td class="px-4 py-3 text-center text-sm text-gray-700">{{ $campaign->stories->count() }}</td>
                        <td class="px-4 py-3 text-center text-sm text-gray-700">{{ $campaign->views_count }}</td>
                        <td class="px-4 py-3">
                            <div class="flex items-center justify-center gap-1 flex-wrap">
                                @can('view campaigns')
                                <a href="{{ route('advocacy.campaigns.show', $campaign) }}" class="btn-action-view">
                                    <i class="fas fa-eye"></i>
                                </a>
                                @endcan
                                @can('edit campaigns')
                                <a href="{{ route('advocacy.campaigns.edit', $campaign) }}" class="btn-action-edit">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <button onclick="toggleStatus({{ $campaign->id }})" class="btn-action-assign" title="Toggle Status">
                                    <i class="fas fa-power-off"></i>
                                </button>
                                @endcan
                                @can('feature campaigns')
                                <button onclick="toggleFeatured({{ $campaign->id }})"
                                        class="inline-flex items-center gap-1 px-3 py-1.5 text-xs font-medium text-white rounded {{ $campaign->is_featured ? 'bg-yellow-500 hover:bg-yellow-600' : 'bg-gray-400 hover:bg-gray-500' }} transition-colors"
                                        title="{{ $campaign->is_featured ? 'Unfeature' : 'Feature' }}">
                                    <i class="fas fa-star"></i>
                                </button>
                                @endcan
                                @can('delete campaigns')
                                <form action="{{ route('advocacy.campaigns.destroy', $campaign) }}" method="POST" class="inline"
                                      onsubmit="return confirm('Delete this campaign?')">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn-action-delete">
                                        <i class="fas fa-trash"></i>
                                    </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-6 py-12 text-center">
                            <i class="fas fa-bullhorn text-4xl text-gray-300 mb-3"></i>
                            <p class="text-gray-500 text-sm">No campaigns found.</p>
                            @can('create campaigns')
                            <a href="{{ route('advocacy.campaigns.create') }}"
                               class="mt-3 inline-flex items-center gap-2 px-4 py-2 bg-teal-700 text-white text-sm rounded-lg hover:bg-teal-800 transition">
                                <i class="fas fa-plus"></i> Create Campaign
                            </a>
                            @endcan
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($campaigns->hasPages())
        <div class="px-4 py-3 border-t border-gray-200 bg-white">
            {{ $campaigns->withQueryString()->links() }}
        </div>
        @endif

    </div>

</x-app-layout>

@push('scripts')
<script>
function toggleStatus(campaignId) {
    fetch(`/dashboard/advocacy/campaigns/${campaignId}/toggle-status`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content }
    }).then(r => r.json()).then(d => { if (d.success) window.location.reload(); });
}
function toggleFeatured(campaignId) {
    fetch(`/dashboard/advocacy/campaigns/${campaignId}/toggle-featured`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content }
    }).then(r => r.json()).then(d => { if (d.success) window.location.reload(); });
}
</script>
@endpush
