@extends('layouts.app')

@section('page_title', 'Our Stories')
@section('page_subtitle', 'Read about our impact, volunteer experiences, and latest community updates.')

@php use Illuminate\Support\Str; @endphp

@section('content')
<section class="section blog-section" style="padding-top: 40px;">
    <div class="container">
        <div class="page-header" style="text-align: center; margin-bottom: 50px;">
            @auth
                <div>
                    <a class="button button-primary" href="{{ route('blog.create') }}">+ Create New Post</a>
                </div>
            @endauth
        </div>

        @if(session('success'))
            <div class="alert alert-success">{{ session('success') }}</div>
        @endif

        @if($posts->isEmpty())
            <div class="empty-state">
                <div style="font-size: 4rem; margin-bottom: 20px;">✍️</div>
                <p>No blog posts yet. Be the first to share a story from our community.</p>
            </div>
        @else
            <div class="blog-grid">
                @foreach($posts as $post)
                    <article class="post-card">
                        @if($post->image_path)
                            <div class="post-card-image">
                                <a href="{{ route('blog.show', $post) }}">
                                    <img src="{{ asset('storage/' . $post->image_path) }}" alt="{{ $post->title }}">
                                </a>
                            </div>
                        @endif
                        <div class="post-card-content">
                            <h2><a href="{{ route('blog.show', $post) }}">{{ $post->title }}</a></h2>
                            <p>{{ $post->excerpt ?? Str::limit(strip_tags($post->content), 120) }}</p>
                            <div class="post-meta">
                                <span style="font-size: 0.85rem; color: #999;">{{ $post->created_at->format('M d, Y') }}</span>
                                <div style="display: flex; gap: 15px; align-items: center;">
                                    @auth
                                        <form action="{{ route('blog.destroy', $post) }}" method="POST" onsubmit="return confirm('Are you sure you want to delete this post?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" style="background: none; border: none; color: #ff4d4d; cursor: pointer; font-weight: 700; font-size: 0.85rem; text-transform: uppercase;">Delete</button>
                                        </form>
                                    @endauth
                                    <a href="{{ route('blog.show', $post) }}" class="read-more">Read Article →</a>
                                </div>
                            </div>
                        </div>
                    </article>
                @endforeach
            </div>

            <div style="margin-top: 60px;">
                {{ $posts->links() }}
            </div>
        @endif
    </div>
</section>
@endsection
