@extends('layouts.master')

@section('title', isset($category) ? $category->name . ' - Blog' : 'Blog')

@section('content')
<div class="bg-light py-5 mb-5">
    <div class="container">
        <h1 class="fw-bold text-center">{{ isset($category) ? $category->name : 'Latest Updates' }}</h1>
        <p class="text-center text-muted">Industry news, tips, and platform updates.</p>
    </div>
</div>

<div class="container mb-5">
    <div class="row">
        <!-- Sidebar -->
        <div class="col-lg-3 mb-4">
            <div class="card shadow-sm border-0">
                <div class="card-body">
                    <h5 class="fw-bold mb-3">Categories</h5>
                    <ul class="list-unstyled mb-0">
                        <li><a href="{{ route('blog.index') }}" class="text-decoration-none {{ !isset($category) ? 'fw-bold' : '' }}">All Posts</a></li>
                        @foreach($categories as $cat)
                            <li><a href="{{ route('blog.category', $cat->slug) }}" class="text-decoration-none {{ isset($category) && $category->id == $cat->id ? 'fw-bold' : '' }}">{{ $cat->name }}</a></li>
                        @endforeach
                    </ul>
                </div>
            </div>
        </div>

        <!-- Posts Grid -->
        <div class="col-lg-9">
            <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4">
                @forelse($posts as $post)
                    <div class="col">
                        <div class="card h-100 shadow-sm border-0 hover-shadow transition">
                            @if($post->image)
                                <img src="{{ asset('storage/' . $post->image) }}" class="card-img-top" alt="{{ $post->title }}" style="height: 200px; object-fit: cover;">
                            @else
                                <div class="bg-secondary card-img-top" style="height: 200px;"></div>
                            @endif
                            <div class="card-body">
                                <small class="text-muted">{{ $post->published_at->format('M d, Y') }}</small>
                                <h5 class="card-title fw-bold mt-2">
                                    <a href="{{ route('blog.show', $post->slug) }}" class="text-dark text-decoration-none">{{ $post->title }}</a>
                                </h5>
                                <p class="card-text text-muted small">{{ Str::limit($post->excerpt, 100) }}</p>
                            </div>
                        </div>
                    </div>
                @empty
                    <div class="col-12">
                        <p class="text-center text-muted">No posts found.</p>
                    </div>
                @endforelse
            </div>
            <div class="mt-4">
                {{ $posts->links() }}
            </div>
        </div>
    </div>
</div>
@endsection
