@extends('layouts.admin')

@section('title', 'Blog CMS – Admin Console')
@section('page_title', 'Blog & Knowledge CMS')

@section('content')
<div class="space-y-8 max-w-7xl">
    
    <!-- New Article Creation Form -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl p-6 sm:p-8 shadow-xl space-y-5" x-data="{ open: false }">
        <div class="flex items-center justify-between border-b border-white/10 pb-4">
            <div>
                <h3 class="text-base font-bold text-white font-heading">Publish Automotive Article</h3>
                <p class="text-xs text-slate-400">Write high-ranking guides on customs changes, hybrid care, or auction strategies.</p>
            </div>
            <button type="button" @click="open = !open" class="px-3.5 py-1.5 rounded-xl bg-red-600 hover:bg-red-700 text-white font-bold text-xs transition flex items-center gap-1.5">
                <i class="fa-solid" :class="open ? 'fa-minus' : 'fa-plus'"></i>
                <span x-text="open ? 'Close Editor' : 'Write New Article'"></span>
            </button>
        </div>

        <form method="POST" action="{{ route('admin.cms.blog.store') }}" x-show="open" class="space-y-4 pt-2">
            @csrf

            <div class="grid grid-cols-1 sm:grid-cols-3 gap-4">
                <div class="sm:col-span-2">
                    <label class="block text-xs font-bold uppercase tracking-wider text-slate-300 mb-1">Article Title *</label>
                    <input type="text" name="title" required class="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-2.5 text-white text-xs font-semibold focus:border-red-500 focus:outline-none" placeholder="e.g. Bangladesh Budget 2026 NBR Customs Duties on Japanese Hybrids">
                </div>
                <div>
                    <label class="block text-xs font-bold uppercase tracking-wider text-slate-300 mb-1">Category *</label>
                    <select name="blog_category_id" required class="w-full bg-slate-950 border border-white/10 rounded-xl px-3 py-2.5 text-white text-xs font-semibold">
                        @foreach($categories as $cat)
                            <option value="{{ $cat->id }}">{{ $cat->name }}</option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-bold uppercase tracking-wider text-slate-300 mb-1">Featured Image URL *</label>
                    <input type="url" name="featured_image" required class="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-2 text-white text-xs font-semibold" placeholder="https://images.unsplash.com/photo-...">
                </div>
                <div>
                    <label class="block text-xs font-bold uppercase tracking-wider text-slate-300 mb-1">Short Excerpt / Summary</label>
                    <input type="text" name="excerpt" class="w-full bg-slate-950 border border-white/10 rounded-xl px-4 py-2 text-white text-xs font-semibold" placeholder="Brief 1-line teaser for search cards...">
                </div>
            </div>

            <div>
                <label class="block text-xs font-bold uppercase tracking-wider text-slate-300 mb-1">Full Article Content *</label>
                <textarea name="content" rows="8" required class="w-full bg-slate-950 border border-white/10 rounded-xl p-4 text-white text-xs font-normal leading-relaxed focus:border-red-500 focus:outline-none" placeholder="Detailed body paragraphs, bullet points, guidance..."></textarea>
            </div>

            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 pt-2 border-t border-white/10">
                <div>
                    <label class="block text-xs font-bold uppercase tracking-wider text-slate-300 mb-1">SEO Meta Title</label>
                    <input type="text" name="meta_title" class="w-full bg-slate-950 border border-white/10 rounded-xl px-3 py-1.5 text-white text-xs">
                </div>
                <div>
                    <label class="block text-xs font-bold uppercase tracking-wider text-slate-300 mb-1">SEO Meta Description</label>
                    <input type="text" name="meta_description" class="w-full bg-slate-950 border border-white/10 rounded-xl px-3 py-1.5 text-white text-xs">
                </div>
            </div>

            <button type="submit" class="px-6 py-2.5 rounded-xl bg-red-600 hover:bg-red-700 text-white font-bold text-xs shadow transition">
                Publish Article Now
            </button>
        </form>
    </div>

    <!-- Published Articles Table -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl overflow-hidden shadow-xl">
        <div class="p-4 border-b border-white/10">
            <h3 class="text-sm font-bold text-white font-heading">Published Articles & Guides</h3>
        </div>
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs">
                <thead class="bg-slate-950 text-slate-400 uppercase tracking-wider text-[10px] border-b border-white/10">
                    <tr>
                        <th class="p-4">Article Title</th>
                        <th class="p-4">Category</th>
                        <th class="p-4">Author</th>
                        <th class="p-4">Views</th>
                        <th class="p-4">Date</th>
                        <th class="p-4 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/5 text-slate-300">
                    @forelse($posts as $post)
                        <tr class="hover:bg-white/5 transition">
                            <td class="p-4">
                                <a href="{{ route('blog.show', $post->slug) }}" target="_blank" class="font-bold text-white hover:text-red-400 block text-xs">
                                    {{ $post->title }}
                                </a>
                                <span class="text-[10px] text-slate-500 font-mono">/blog/{{ $post->slug }}</span>
                            </td>
                            <td class="p-4">
                                <span class="px-2 py-0.5 rounded text-[10px] font-bold uppercase bg-red-600/20 text-red-400">
                                    {{ $post->category->name ?? 'Article' }}
                                </span>
                            </td>
                            <td class="p-4 text-slate-300">
                                {{ $post->author->name ?? 'Admin' }}
                            </td>
                            <td class="p-4 text-slate-400">
                                {{ $post->views_count }} views
                            </td>
                            <td class="p-4 text-slate-400">
                                {{ $post->created_at->format('M d, Y') }}
                            </td>
                            <td class="p-4 text-right">
                                <form method="POST" action="{{ route('admin.cms.blog.destroy', $post->id) }}" onsubmit="return confirm('Delete this blog post?')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="px-2.5 py-1.5 rounded-lg bg-rose-500/20 hover:bg-rose-500/40 text-rose-300 transition">
                                        <i class="fa-solid fa-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="p-8 text-center text-slate-400">
                                No articles published yet.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="p-4 border-t border-white/10">
            {{ $posts->links() }}
        </div>
    </div>

</div>
@endsection
