@extends('layouts.app')

@section('title', 'Announcements')
@section('heading', 'Announcements')

@section('content')
@include('announcements.top-submenu')
@include('settings.partials.back-to-home')
      <div class="mb-5 flex flex-wrap items-center justify-between gap-3">
        <div >
            <h1 class="font-semibold uppercase tracking-[0.18em] text-primary">Announcements</h1>
            <p class="mt-1 text-sm text-slate-500">Company news, updates, and shared files.</p>
        </div>

        <div class="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
        
        <div class="flex flex-wrap items-center gap-2">
            @if(auth()->user()->hasPermission('announcements_view'))
                <a href="{{ route('announcements.create') }}" class="inline-flex items-center rounded-lg bg-teal-700 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-teal-600">
                    <i class="bi bi-plus-lg me-1.5"></i> New announcement
                </a>
            @endif
        </div>
    </div>
      </div>

<div class="space-y-6">

    <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-5">
        <x-kpi-card title="Total" :value="$kpis['total']" accent="slate" subtitle="All announcements" />
        <x-kpi-card title="Active" :value="$kpis['active']" accent="emerald" subtitle="Visible to team" />
        <x-kpi-card title="Inactive" :value="$kpis['inactive']" tone="warning" accent="amber" subtitle="Draft or archived" />
        <x-kpi-card title="Pinned" :value="$kpis['pinned']" accent="violet" subtitle="Highlighted items" />
        <x-kpi-card title="This month" :value="$kpis['this_month']" accent="sky" subtitle="Created in {{ now()->format('F') }}" />
    </div>

    <x-card>
        <x-slot name="header">
            <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
                <div>
                    <h2 class="text-sm font-semibold text-slate-800">All announcements</h2>
                    <p class="text-xs text-slate-500">List view — newest and pinned first.</p>
                </div>
                @if(auth()->user()->hasPermission('announcements_view'))
                    <a href="{{ route('announcements.create') }}" class="inline-flex w-full items-center justify-center rounded-lg border border-teal-700 bg-white px-4 py-2 text-sm font-medium text-teal-800 hover:bg-teal-50 sm:w-auto">
                        <i class="bi bi-megaphone me-2"></i> Create announcement
                    </a>
                @endif
            </div>
        </x-slot>

        <x-table>
            <x-slot name="head">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Title</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Summary</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Files</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Pinned</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Author</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Created</th>
                    <th class="px-4 py-3 text-right text-xs font-medium uppercase text-slate-500">Actions</th>
                </tr>
            </x-slot>
            @forelse($announcements as $a)
                <tr class="{{ $a->is_pinned ? 'bg-amber-50/40' : '' }}">
                    <td class="px-4 py-3 text-sm font-medium text-slate-900">
                        <div class="flex items-center gap-2">
                            @if($a->is_pinned)
                                <i class="bi bi-pin-angle-fill text-amber-600" title="Pinned"></i>
                            @endif
                            {{ $a->title }}
                        </div>
                    </td>
                    <td class="max-w-xs px-4 py-3 text-sm text-slate-600">
                        @if(! empty($a->body))
                            <span class="line-clamp-2">{{ \Illuminate\Support\Str::limit(strip_tags($a->body), 140) }}</span>
                        @else
                            <span class="text-slate-400">—</span>
                        @endif
                    </td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-700">
                        @if(($a->attachments_count ?? 0) > 0)
                            <span class="inline-flex items-center rounded-full bg-slate-100 px-2 py-0.5 text-xs font-medium text-slate-700">{{ $a->attachments_count }} file(s)</span>
                        @else
                            <span class="text-slate-400">—</span>
                        @endif
                    </td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm">
                        @if($a->is_active)
                            <x-badge variant="success">Active</x-badge>
                        @else
                            <x-badge>Inactive</x-badge>
                        @endif
                    </td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-700">{{ $a->is_pinned ? 'Yes' : 'No' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-700">{{ $a->creator?->name ?? '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $a->created_at?->format('M j, Y') ?? '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-right">
                        @if(auth()->user()->hasPermission('announcements_edit'))
                            <a href="{{ route('announcements.edit', $a) }}" class="btn-icon-action btn-icon-action--edit" title="Edit" aria-label="Edit"><i class="bi bi-pencil-square"></i></a>
                        @endif
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="8" class="px-4 py-12 text-center">
                        <p class="text-sm text-slate-500">No announcements yet.</p>
                        @if(auth()->user()->hasPermission('announcements_view'))
                            <a href="{{ route('announcements.create') }}" class="mt-4 inline-flex items-center rounded-lg bg-teal-700 px-4 py-2 text-sm font-medium text-white hover:bg-teal-600"><i class="bi bi-plus-lg me-1"></i>Create your first announcement</a>
                        @endif
                    </td>
                </tr>
            @endforelse
        </x-table>
        @if($announcements->hasPages())
            <div class="mt-4">{{ $announcements->links() }}</div>
        @endif
    </x-card>
</div>
@endsection
