@extends('layouts.admin', ['heading' => 'Homepage CMS - Blogs'])

@section('content')
    @include('admin.cms._tabs')

    @if(session('status'))
        <div class="mb-4 rounded bg-emerald-100 p-3 text-emerald-800">{{ session('status') }}</div>
    @endif
    @if($errors->any())
        <div class="mb-4 rounded bg-rose-100 p-3 text-rose-800">
            <ul class="list-disc pl-5 text-sm">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <div class="mb-6 rounded bg-white p-4 shadow">
        <h3 class="mb-3 font-semibold">Create Blog</h3>
        <form method="POST" action="{{ route('admin.cms.blogs.store') }}" enctype="multipart/form-data" class="grid grid-cols-1 gap-3 md:grid-cols-2">
            @csrf
            <input name="title" placeholder="Title" class="rounded border p-2 text-sm" required>
            <input name="slug" placeholder="Slug (optional)" class="rounded border p-2 text-sm">
            <input name="meta_title" placeholder="Meta Title" class="rounded border p-2 text-sm">
            <input name="meta_description" placeholder="Meta Description" class="rounded border p-2 text-sm">
            <input name="published_at" type="datetime-local" class="rounded border p-2 text-sm">
            <select name="status" class="rounded border p-2 text-sm" required>
                <option value="draft">Draft</option>
                <option value="published">Published</option>
            </select>
            <input name="featured_image" type="file" class="rounded border p-2 text-sm md:col-span-2">
            <textarea name="excerpt" placeholder="Excerpt" rows="2" class="rounded border p-2 text-sm md:col-span-2"></textarea>
            <textarea name="content" placeholder="Content" rows="6" class="rounded border p-2 text-sm md:col-span-2" required></textarea>
            <button class="rounded bg-emerald-600 px-4 py-2 text-sm text-white md:col-span-2">Create Blog</button>
        </form>
    </div>

    <div class="overflow-x-auto rounded bg-white shadow">
        <table class="min-w-full text-sm">
            <thead class="bg-slate-100 text-left">
                <tr>
                    <th class="p-3">Title</th>
                    <th class="p-3">Slug</th>
                    <th class="p-3">Status</th>
                    <th class="p-3">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($blogs as $blog)
                    <tr class="border-t align-top">
                        <td class="p-3">{{ $blog->title }}</td>
                        <td class="p-3">{{ $blog->slug }}</td>
                        <td class="p-3">{{ strtoupper($blog->status) }}</td>
                        <td class="p-3">
                            <form method="POST" action="{{ route('admin.cms.blogs.update', $blog->id) }}" enctype="multipart/form-data" class="grid grid-cols-1 gap-2">
                                @csrf
                                <input name="title" value="{{ $blog->title }}" class="rounded border p-1 text-xs" required>
                                <input name="slug" value="{{ $blog->slug }}" class="rounded border p-1 text-xs">
                                <input name="meta_title" value="{{ $blog->meta_title }}" class="rounded border p-1 text-xs">
                                <input name="meta_description" value="{{ $blog->meta_description }}" class="rounded border p-1 text-xs">
                                <input name="published_at" type="datetime-local" value="{{ optional($blog->published_at)->format('Y-m-d\TH:i') }}" class="rounded border p-1 text-xs">
                                <select name="status" class="rounded border p-1 text-xs">
                                    <option value="draft" @selected($blog->status === 'draft')>Draft</option>
                                    <option value="published" @selected($blog->status === 'published')>Published</option>
                                </select>
                                <input name="featured_image" type="file" class="rounded border p-1 text-xs">
                                <textarea name="excerpt" rows="2" class="rounded border p-1 text-xs">{{ $blog->excerpt }}</textarea>
                                <textarea name="content" rows="5" class="rounded border p-1 text-xs" required>{{ $blog->content }}</textarea>
                                <div class="flex gap-2">
                                    <button class="rounded bg-slate-900 px-2 py-1 text-xs text-white">Update</button>
                                    <button formaction="{{ route('admin.cms.blogs.delete', $blog->id) }}" class="rounded bg-rose-600 px-2 py-1 text-xs text-white">Delete</button>
                                </div>
                            </form>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="4" class="p-4 text-center text-slate-500">No blogs found.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
@endsection
