<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                {{ __('Admin - Manage Announcements') }}
            </h2>
            <div class="flex gap-2">
                <x-secondary-button onclick="window.location='{{ route('admin.announcements.create') }}'">
                    + New Announcement
                </x-secondary-button>
                <x-secondary-button onclick="window.location='{{ route('admin.dashboard') }}'">
                    Back to Admin
                </x-secondary-button>
            </div>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            @if(session('success'))
                <div class="mb-4 px-4 py-3 bg-gray-50 border border-gray-200 text-gray-700 rounded">
                    {{ session('success') }}
                </div>
            @endif

            <!-- Announcements Table -->
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    @if($announcements->count() > 0)
                        <div class="overflow-x-auto">
                            <table class="min-w-full divide-y divide-gray-200">
                                <thead class="bg-gray-50">
                                    <tr>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Title</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Type</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Display</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Audience</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Dates</th>
                                        <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white divide-y divide-gray-200">
                                    @foreach($announcements as $announcement)
                                        @php
                                            $colors = $announcement->type_color;
                                            $isLive = $announcement->is_active &&
                                                      (!$announcement->starts_at || $announcement->starts_at <= now()) &&
                                                      (!$announcement->expires_at || $announcement->expires_at > now());
                                            $isScheduled = $announcement->is_active && $announcement->starts_at && $announcement->starts_at > now();
                                        @endphp
                                        <tr>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                                {{ Str::limit($announcement->title, 30) }}
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                                <span class="inline-flex px-2 py-1 rounded-full text-xs font-semibold text-white {{ $colors['badge'] }}">
                                                    {{ ucfirst($announcement->type) }}
                                                </span>
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                {{ ucfirst(str_replace('_', ' ', $announcement->display_mode)) }}
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                {{ ucfirst($announcement->audience) }}
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                                @if($isLive)
                                                    <span class="inline-flex px-2 py-1 rounded-full text-xs font-semibold text-gray-700 bg-gray-100">
                                                        Live
                                                    </span>
                                                @elseif($isScheduled)
                                                    <span class="inline-flex px-2 py-1 rounded-full text-xs font-semibold text-gray-600 bg-gray-100">
                                                        Scheduled
                                                    </span>
                                                @else
                                                    <span class="inline-flex px-2 py-1 rounded-full text-xs font-semibold text-gray-700 bg-gray-100">
                                                        Inactive
                                                    </span>
                                                @endif
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                                <div>
                                                    @if($announcement->starts_at)
                                                        <div>{{ $announcement->starts_at->format('M d, Y H:i') }}</div>
                                                    @else
                                                        <div>No start</div>
                                                    @endif
                                                    @if($announcement->expires_at)
                                                        <div class="text-xs">→ {{ $announcement->expires_at->format('M d, Y H:i') }}</div>
                                                    @else
                                                        <div class="text-xs">→ Never</div>
                                                    @endif
                                                </div>
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap text-sm space-x-2">
                                                <a href="{{ route('admin.announcements.edit', $announcement) }}" class="text-xs text-gray-600 hover:opacity-60 transition-opacity">
                                                    Edit
                                                </a>

                                                <form method="POST" action="{{ route('admin.announcements.toggle-active', $announcement) }}" class="inline-block">
                                                    @csrf
                                                    <button type="submit" class="text-xs text-gray-600 hover:opacity-60 transition-opacity">
                                                        {{ $announcement->is_active ? 'Deactivate' : 'Activate' }}
                                                    </button>
                                                </form>

                                                <form method="POST" action="{{ route('admin.announcements.destroy', $announcement) }}" class="inline-block" onsubmit="return confirm('Are you sure?')">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="text-xs text-red-600 hover:opacity-60 transition-opacity">
                                                        Delete
                                                    </button>
                                                </form>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        <div class="mt-4">
                            {{ $announcements->links() }}
                        </div>
                    @else
                        <div class="text-center py-12">
                            <p class="text-gray-500 text-lg mb-4">No announcements yet</p>
                            <a href="{{ route('admin.announcements.create') }}" class="inline-flex items-center px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700">
                                Create First Announcement
                            </a>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
