@extends('layouts.admin')

@section('title', 'Announcements')

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Marketing</span>
            <h1>Announcements</h1>
        </div>
        <a class="button primary" href="{{ route('admin.announcements.create') }}">Add announcement</a>
    </section>

    @if (session('status'))
        <p class="form-status">{{ session('status') }}</p>
    @endif

    <section class="panel">
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>Schedule</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($announcements as $announcement)
                        <tr>
                            <td>
                                <strong>{{ $announcement->title }}</strong>
                                @if ($announcement->body)
                                    <small>{{ \Illuminate\Support\Str::limit($announcement->body, 90) }}</small>
                                @endif
                            </td>
                            <td>
                                {{ $announcement->starts_at?->format('M d, Y H:i') ?: 'Now' }}
                                <small>to {{ $announcement->ends_at?->format('M d, Y H:i') ?: 'No end' }}</small>
                            </td>
                            <td><span class="status {{ $announcement->is_active ? 'paid' : 'refunded' }}">{{ $announcement->is_active ? 'Active' : 'Inactive' }}</span></td>
                            <td>
                                <div class="table-actions">
                                    <a class="text-button" href="{{ route('admin.announcements.edit', $announcement) }}">Edit</a>
                                    <form method="POST" action="{{ route('admin.announcements.toggle', $announcement) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button class="text-button" type="submit">{{ $announcement->is_active ? 'Disable' : 'Enable' }}</button>
                                    </form>
                                    <form method="POST" action="{{ route('admin.announcements.destroy', $announcement) }}">
                                        @csrf
                                        @method('DELETE')
                                        <button class="text-button danger" type="submit">Delete</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="4">No announcements yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        {{ $announcements->links() }}
    </section>
@endsection
