<x-app-layout>

    <x-module-hero
        title="Advocacy Polls"
        description="Create and manage community polls"
        icon="fa-poll">

        <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 polls')
        <x-slot name="actions">
            <a href="{{ route('advocacy.polls.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 Poll
            </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

        {{-- 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">Question</th>
                        <th class="px-4 py-3 text-left th-col-2">Status</th>
                        <th class="px-4 py-3 text-center th-col-3">Votes</th>
                        <th class="px-4 py-3 text-left th-col-4">Display</th>
                        <th class="px-4 py-3 text-left th-col-5">Created</th>
                        <th class="px-4 py-3 text-center th-col-6">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($polls as $poll)
                    <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>
                                <p class="font-medium text-gray-900 text-sm line-clamp-1">{{ $poll->question }}</p>
                                @if($poll->campaign)
                                <p class="text-xs text-gray-500 mt-0.5">
                                    <i class="fas fa-bullhorn mr-1"></i> {{ $poll->campaign->title }}
                                </p>
                                @endif
                            </div>
                        </td>
                        <td class="px-4 py-3">
                            @php
                            $statusColors = [
                                'draft'  => 'bg-gray-100 text-gray-700',
                                'active' => 'bg-green-100 text-green-700',
                                'closed' => 'bg-blue-100 text-blue-700',
                            ];
                            @endphp
                            <span class="px-2 py-1 text-xs font-semibold rounded-full {{ $statusColors[$poll->status] ?? 'bg-gray-100' }}">
                                {{ ucfirst($poll->status) }}
                            </span>
                        </td>
                        <td class="px-4 py-3 text-center">
                            <span class="text-lg font-bold text-purple-600">{{ $poll->total_responses }}</span>
                        </td>
                        <td class="px-4 py-3 text-sm text-gray-700 capitalize">{{ $poll->display_position }}</td>
                        <td class="px-4 py-3 text-sm text-gray-500">{{ $poll->created_at->diffForHumans() }}</td>
                        <td class="px-4 py-3">
                            <div class="flex items-center justify-center gap-1 flex-wrap">
                                @can('view polls')
                                <a href="{{ route('advocacy.polls.show', $poll) }}" class="btn-action-view">
                                    <i class="fas fa-eye"></i>
                                </a>
                                @endcan
                                @can('edit polls')
                                @if($poll->status === 'draft')
                                <a href="{{ route('advocacy.polls.edit', $poll) }}" class="btn-action-edit">
                                    <i class="fas fa-edit"></i>
                                </a>
                                @endif
                                @if($poll->status === 'active')
                                <button onclick="closePoll({{ $poll->id }})" class="btn-action-reject" title="Close Poll">
                                    <i class="fas fa-stop"></i>
                                </button>
                                @endif
                                @endcan
                                @can('delete polls')
                                <form action="{{ route('advocacy.polls.destroy', $poll) }}" method="POST" class="inline"
                                      onsubmit="return confirm('Delete this poll?')">
                                    @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-poll text-4xl text-gray-300 mb-3"></i>
                            <p class="text-gray-500 text-sm">No polls found.</p>
                            @can('create polls')
                            <a href="{{ route('advocacy.polls.create') }}"
                               class="mt-3 inline-flex items-center gap-2 px-4 py-2 bg-purple-700 text-white text-sm rounded-lg hover:bg-purple-800 transition">
                                <i class="fas fa-plus"></i> Create Poll
                            </a>
                            @endcan
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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

    </div>

</x-app-layout>

@push('scripts')
<script>
function closePoll(pollId) {
    if (!confirm('Close this poll? No more votes will be accepted.')) return;
    fetch(`/dashboard/advocacy/polls/${pollId}/close`, {
        method: 'POST',
        headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content }
    }).then(r => r.json()).then(d => { if (d.success) window.location.reload(); });
}
</script>
@endpush
