@extends('admin.layouts.app')

@section('title', 'Blog Articles')

@section('content')
<div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6">
    <div>
        <h2 class="text-xl font-bold text-gray-800">Blog Articles</h2>
        <p class="text-sm text-gray-500 mt-0.5">{{ $articles->total() }} total</p>
    </div>
    <a href="{{ route('admin.blog-articles.create') }}" class="inline-flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white text-sm font-semibold px-4 py-2.5 rounded-lg transition">
        <span>+</span> Add Article
    </a>
</div>

<div class="bg-white rounded-xl shadow-sm border border-gray-100 p-4 mb-4">
    <form method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-3">
        <input type="text" name="search" value="{{ request('search') }}" placeholder="Search articles..." class="px-4 py-2 border border-gray-300 rounded-lg text-sm">
        <select name="status" class="px-4 py-2 border border-gray-300 rounded-lg text-sm">
            <option value="">All statuses</option>
            @foreach(['draft', 'pending', 'approved', 'rejected'] as $status)
                <option value="{{ $status }}" @selected(request('status') === $status)>{{ ucfirst($status) }}</option>
            @endforeach
        </select>
        <select name="category_id" class="px-4 py-2 border border-gray-300 rounded-lg text-sm">
            <option value="">All categories</option>
            @foreach($categories as $category)
                <option value="{{ $category->id }}" @selected((string) request('category_id') === (string) $category->id)>{{ $category->name }}</option>
            @endforeach
        </select>
        <button type="submit" class="px-4 py-2 bg-blue-600 text-white text-sm rounded-lg">Filter</button>
    </form>
</div>

<div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
    @if($articles->isEmpty())
        <div class="text-center py-16 text-sm text-gray-500">No blog articles found.</div>
    @else
        <table class="min-w-full divide-y divide-gray-100">
            <thead class="bg-gray-50">
                <tr>
                    <th class="px-5 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Article</th>
                    <th class="px-5 py-3 text-left text-xs font-semibold text-gray-500 uppercase hidden lg:table-cell">Author</th>
                    <th class="px-5 py-3 text-left text-xs font-semibold text-gray-500 uppercase hidden md:table-cell">Category</th>
                    <th class="px-5 py-3 text-left text-xs font-semibold text-gray-500 uppercase">Status</th>
                    <th class="px-5 py-3 text-right text-xs font-semibold text-gray-500 uppercase">Actions</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                @foreach($articles as $article)
                    <tr>
                        <td class="px-5 py-4">
                            <p class="text-sm font-semibold text-gray-800">{{ $article->title }}</p>
                            <p class="text-xs text-gray-400">{{ $article->slug }}</p>
                        </td>
                        <td class="px-5 py-4 hidden lg:table-cell text-sm text-gray-600">{{ $article->author?->name ?: '-' }}</td>
                        <td class="px-5 py-4 hidden md:table-cell text-sm text-gray-600">{{ $article->category?->name ?: '-' }}</td>
                        <td class="px-5 py-4">
                            <span class="text-xs px-2 py-1 rounded-full {{ $article->statusBadge() }}">{{ $article->statusLabel() }}</span>
                        </td>
                        <td class="px-5 py-4">
                            <div class="flex justify-end flex-wrap gap-2">
                                <a href="{{ route('admin.blog-articles.preview', $article) }}" target="_blank" class="px-2 py-1 text-xs bg-gray-100 rounded">Preview</a>
                                @if($article->status === 'approved')
                                    <a href="{{ route('blogs.show', $article->slug) }}" target="_blank" class="px-2 py-1 text-xs bg-gray-100 rounded">View</a>
                                @endif
                                <a href="{{ route('admin.blog-articles.edit', $article) }}" class="px-2 py-1 text-xs bg-blue-100 text-blue-700 rounded">Edit</a>
                                @if($article->status !== 'approved')
                                    <form method="POST" action="{{ route('admin.blog-articles.approve', $article) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button type="submit" class="px-2 py-1 text-xs bg-green-100 text-green-700 rounded">Approve</button>
                                    </form>
                                @endif
                                @if($article->status !== 'rejected')
                                    <button type="button" data-reject-action="{{ route('admin.blog-articles.reject', $article) }}" class="px-2 py-1 text-xs bg-yellow-100 text-yellow-700 rounded">Reject</button>
                                @endif
                                @if($article->status === 'approved')
                                    <form method="POST" action="{{ route('admin.blog-articles.unpublish', $article) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button type="submit" class="px-2 py-1 text-xs bg-gray-100 text-gray-700 rounded">Unpublish</button>
                                    </form>
                                @endif
                                <form method="POST" action="{{ route('admin.blog-articles.destroy', $article) }}" onsubmit="return confirm('Delete this article?')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="px-2 py-1 text-xs bg-red-100 text-red-700 rounded">Delete</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
        <div class="px-5 py-4 border-t border-gray-100">
            {{ $articles->links() }}
        </div>
    @endif
</div>

@include('admin.blog-articles._reject_modal')
@endsection
