<x-app-layout>
<x-module-hero title="Topbar Notifications" description="Manage scrolling announcements displayed in the site topbar" icon="fa-bell">
    <x-slot name="tabs">
        @can('view topbar notifications')
        <a href="{{ route('topbar-notifications.index') }}"
           class="module-hero-tab {{ request()->routeIs('topbar-notifications.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 Notifications
        </a>
        @endcan
        @can('create topbar notifications')
        <a href="{{ route('topbar-notifications.create') }}"
           class="module-hero-tab {{ request()->routeIs('topbar-notifications.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 New
        </a>
        @endcan
    </x-slot>
    @can('create topbar notifications')
    <x-slot name="actions">
        <a href="{{ route('topbar-notifications.create') }}"
           class="inline-flex items-center gap-2 px-4 py-2 bg-white text-[#1a4731] text-sm font-semibold rounded-lg hover:bg-green-50 transition">
            <i class="fas fa-plus"></i> Add Notification
        </a>
    </x-slot>
    @endcan
</x-module-hero>

<div class="w-full">
    @if(session('success'))
        <div class="mx-4 mt-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="mx-4 mt-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

    @if($notifications->isEmpty())
        <div class="p-12 text-center text-gray-500">
            <i class="fas fa-bell-slash text-6xl mb-4 block opacity-30"></i>
            <h3 class="text-xl font-semibold text-gray-700 mb-2">No notifications yet</h3>
            <p class="text-gray-500 mb-6">Create your first topbar notification to get started.</p>
            @can('create topbar notifications')
                <a href="{{ route('topbar-notifications.create') }}"
                   class="inline-flex items-center gap-2 px-5 py-2.5 bg-[#1a4731] text-white font-semibold rounded-lg hover:bg-[#276749] transition">
                    <i class="fas fa-plus"></i> Add Notification
                </a>
            @endcan
        </div>
    @else
        <div class="overflow-x-auto">
            <table class="w-full table-zebra">
                <thead>
                    <tr>
                        <th class="px-4 py-3 text-left th-col-1">Content</th>
                        <th class="px-4 py-3 text-left th-col-2">Button</th>
                        <th class="px-4 py-3 text-left th-col-3">Start Date</th>
                        <th class="px-4 py-3 text-left th-col-4">End Date</th>
                        <th class="px-4 py-3 text-left th-col-5">Status</th>
                        <th class="px-4 py-3 text-left th-col-6">Order</th>
                        <th class="px-4 py-3 text-center th-col-7">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($notifications as $notification)
                        <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 max-w-xs">
                                <p class="text-sm font-medium text-gray-800 truncate">{{ $notification->content }}</p>
                                @if($notification->button_url)
                                    <p class="text-xs text-gray-400 truncate mt-0.5">{{ $notification->button_url }}</p>
                                @endif
                            </td>
                            <td class="px-4 py-3">
                                @if($notification->button_label)
                                    <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-700">
                                        {{ $notification->button_label }}
                                    </span>
                                @else
                                    <span class="text-xs text-gray-400">—</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-sm text-gray-600">
                                {{ $notification->start_date?->format('d M Y') ?? '—' }}
                            </td>
                            <td class="px-4 py-3 text-sm">
                                @if($notification->end_date)
                                    <span class="{{ $notification->end_date->isPast() ? 'text-red-500 font-medium' : 'text-gray-600' }}">
                                        {{ $notification->end_date->format('d M Y') }}
                                    </span>
                                @else
                                    <span class="text-gray-400">—</span>
                                @endif
                            </td>
                            <td class="px-4 py-3">
                                @if($notification->is_active && (!$notification->end_date || $notification->end_date->isFuture()))
                                    <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-semibold bg-green-100 text-green-800">
                                        <i class="fas fa-circle text-green-500 mr-1" style="font-size:6px"></i> Active
                                    </span>
                                @elseif($notification->end_date && $notification->end_date->isPast())
                                    <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-semibold bg-gray-100 text-gray-600">
                                        <i class="fas fa-circle text-gray-400 mr-1" style="font-size:6px"></i> Expired
                                    </span>
                                @else
                                    <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-semibold bg-yellow-100 text-yellow-800">
                                        <i class="fas fa-circle text-yellow-500 mr-1" style="font-size:6px"></i> Inactive
                                    </span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-sm text-gray-600 font-mono">{{ $notification->order }}</td>
                            <td class="px-4 py-3 text-center">
                                <div class="flex items-center justify-center gap-2">
                                    <a href="{{ route('topbar-notifications.show', $notification) }}"
                                       class="btn-action-view"
                                       target="_blank" rel="noopener noreferrer"
                                       title="View on public website">
                                        <i class="fas fa-eye"></i> View
                                    </a>
                                    @can('edit topbar notifications')
                                        <a href="{{ route('topbar-notifications.edit', $notification) }}"
                                           class="btn-action-edit">
                                            <i class="fas fa-edit"></i> Edit
                                        </a>
                                    @endcan
                                    @can('delete topbar notifications')
                                        <form action="{{ route('topbar-notifications.destroy', $notification) }}" method="POST" class="inline"
                                              onsubmit="return confirm('Delete this notification?')">
                                            @csrf @method('DELETE')
                                            <button type="submit" class="btn-action-delete">
                                                <i class="fas fa-trash"></i> Delete
                                            </button>
                                        </form>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>

        @if($notifications->hasPages())
            <div class="px-4 py-3 border-t">{{ $notifications->links() }}</div>
        @endif
    @endif
</div>
</x-app-layout>
