@extends('front.layout.pages-layout')
@section('pageTitle', isset($pageTitle) ? $pageTitle : 'Document Title')
@section('meta_tags')
{!! SEO::generate() !!}
@endsection
@section('content')
<section class="section department-single">
    <div class="container">
        <div class="row">
            <div class="col-12">
                <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap">
                    <!-- Title -->
                    <h3 class="mb-0">Bulletin Room</h3>
                    <!-- Breadcrumb -->
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb bg-transparent mb-0 px-0">
                            <li class="breadcrumb-item"><a href="{{ url('/') }}">Home</a></li>
                            <li class="breadcrumb-item active" aria-current="page">Bulletin</li>
                        </ol>
                    </nav>
                </div>
                <hr>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-8">
                <!-- Blog Posts Section -->
                <section id="blog-posts" class="blog-posts">
                    <div class="container">
                        <div class="row gy-4">
                            <div class="col-12">
                                @if (!empty(latest_posts(0, 1)))
                                @foreach (latest_posts(0,1) as $latest_post)
                                @php
                                $duration = readDuration($latest_post->title, $latest_post->content);
                                @endphp
                                <article>
                                    <div class="post-img">
                                        <img src="/images/posts/{{ $latest_post->featured_image }}" alt="" class="img-fluid">
                                    </div>

                                    <h2 class="title">
                                        <a href="{{ route('read_post',$latest_post->slug) }}">{{$latest_post->title}}</a>
                                    </h2>
                                    <div class="meta-top">
                                        <ul>
                                            <li class="d-flex align-items-center"><i class="icofont-ui-user"></i> <a href="{{route('author_posts',$latest_post->author->username)}}">{{ $latest_post->author->name }}</a></li>
                                            <li class="d-flex align-items-center"><i class="mdi mdi-calendar-badge"></i>{{ date_formatter($latest_post->created_at) }}</li>
                                            <li class="d-flex align-items-center"><i class="icofont-clock-time"></i>{{ readDuration($duration) }}{{Str::plural('min', $duration)}}</li>
                                        </ul>
                                    </div>

                                    <div class="content">
                                        <p class="text-justify">
                                            {!! Str::ucfirst(words($latest_post->content, 50)) !!}
                                        </p>

                                        <div class="read-more">
                                            <a href="{{ route('read_post', $latest_post->slug) }}">Read More</a>
                                        </div>
                                    </div>

                                </article>
                                @endforeach
                                @endif
                            </div><!-- End post list item -->


                            <div class="card shadow border-0 p-3">
                                @if (!empty(latest_posts(1, 3)))
                                @foreach (latest_posts(1,3) as $post)
                                <div class="row mb-4 align-items-stretch">
                                    <!-- Image Section -->
                                    <div class="col-md-4 d-flex p-3">
                                        <img
                                            src="{{ asset('/images/posts/resized/thumb_' . $post->featured_image) }}"
                                            class="img-fluid card-radius w-100 object-fit-cover"
                                            style="height: 100%; max-height: 250px; object-fit: cover;"
                                            alt="{{ $post->title ?? 'Post Image' }}">
                                    </div>

                                    <!-- Content Section -->
                                    <div class="col-md-8">
                                        <div class="card-body h-100 d-flex flex-column justify-content-between">
                                            <div>
                                                <h5 class="card-title fw-bold">
                                                    <a href="{{ route('read_post',$post->slug) }}">{{ $post->title }}</a>
                                                </h5>

                                                <div class="text-muted small mb-2 card-details">
                                                    <i class="icofont-ui-user"></i>
                                                    <a href="{{ route('author_posts',$post->author->username) }}">{{ $post->author->name }}</a> &nbsp;
                                                    <i class="mdi mdi-calendar-badge"></i> {{ date_formatter($post->created_at) }} &nbsp;
                                                    <i class="icofont-category"></i>
                                                    <a href="{{ route('category_post', $post->post_category->slug) }}" class="ml-1">{{ $post->post_category->name }}</a>
                                                </div>

                                                <p class="card-text">
                                                    {!! Str::ucfirst(words($post->content, 25)) !!}
                                                </p>
                                            </div>

                                            <div>
                                                <a href="{{ route('read_post',$post->slug) }}">Read More...</a>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- End post list item -->
                                @endforeach
                                @endif
                            </div><!-- End blog posts list -->

                        </div>
                </div>
                </section><!-- /Blog Posts Section -->

                <!-- Blog Pagination Section -->
                <section id="blog-pagination" class="blog-pagination section">

                    <div class="container">
                        <div class="d-flex justify-content-center">
                            <ul>
                                <li><a href="#"><i class="bi bi-chevron-left"></i></a></li>
                                <li><a href="#">1</a></li>
                                <li><a href="#" class="active">2</a></li>
                                <li><a href="#">3</a></li>
                                <li><a href="#">4</a></li>
                                <li>...</li>
                                <li><a href="#">10</a></li>
                                <li><a href="#"><i class="bi bi-chevron-right"></i></a></li>
                            </ul>
                        </div>
                    </div>

                </section><!-- /Blog Pagination Section -->
            </div>
            <div class="col-lg-4 sidebar">

                <div class="widgets-container">

                    <!-- Search Widget -->
                     <x-sidebar-search></x-sidebar-search>
                    <!--/Search Widget -->

                    <!-- Categories Widget -->
                    @php
                    $grouped = sidebar_categories()->groupBy(fn($cat) => $cat->parent_category->name ?? 'Others');
                    @endphp

                    <div class="categories-widget widget-item">
                        <h3 class="widget-title">Categories</h3>

                        @foreach ($grouped as $parentName => $categories)
                        <h6 class="mt-3"><strong>{{ $parentName }}</strong></h6>
                        <ul class="ms-3">
                            @foreach ($categories as $item)
                            <li>
                                <a href="{{ route('category_post', $item->slug) }}">
                                    {{ $item->name }} <span>({{ $item->posts_count }})</span>
                                </a>
                            </li>
                            @endforeach
                        </ul>
                        @endforeach
                    </div>

                    <!--/Categories Widget -->

                    <!-- Recent Posts Widget -->
                    <div class="recent-posts-widget widget-item">

                        <h3 class="widget-title">Recent Posts</h3>
                        @if (!empty(latest_posts(0, 5)))
                        @foreach (latest_posts(0,5) as $post)
                        <div class="post-item">
                            <img src="/images/posts/resized/thumb_{{ $post->featured_image }}" alt="{{ $post->title }}" class="img-fluid" style="max-height: 100px; object-fit: cover;">
                            <div>
                                <h4 style="line-height: 1.5rem;"><a href="{{ route('read_post', $post->slug) }}">{{ $post->title }}</a></h4>
                                <time>{{ date_formatter($latest_post->created_at) }}</li></time>
                            </div>
                        </div>
                        @endforeach
                        @endif
                        <!-- End recent post item-->
                    </div><!--/Recent Posts Widget -->

                    <!-- Tags Widget -->
                    <div class="tags-widget widget-item">

                        <h3 class="widget-title">Tags</h3>
                        <ul>
                            @foreach (getTags() as $tags )
                            <li><a href="{{ route('tag_posts', urlencode($tags)) }}">{{ $tags }}</a></li>
                            @endforeach
                        </ul>

                    </div><!--/Tags Widget -->

                </div>

            </div>
        </div>
    </div>
</section>
@endsection