@extends('layouts.pmar.theme')
@section('title', env('APP_NAME') . ' :: Notícias')
@section('content')
<section>
    <div class="container py-5">
        <div class="row">
            <div class="col-12">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item">
                            <a href="{{ route('paginas.home') }}">Portal</a>
                        </li>
                        <li class="breadcrumb-item active" aria-current="page">Notícias</li>
                    </ol>
                </nav>
            </div>
            <div class="col-12">
                <h1 class="text-secondary text-center">Notícias</h1>
                @php
                    $pathSystem = $_SERVER['DOCUMENT_ROOT'] . "/";
                    $angraDefault = asset("/pmar/assets/img/angra-default.png");
                    $noticiasFiltrosPainelAberto = request()->filled('secretaria_id')
                        || request()->filled('ano')
                        || request()->filled('categoria');
                @endphp
            </div>
            <div class="col-12">
                <div id="searchForm" class="bg-light d-flex justify-content-center align-items-center py-5">
                    <div class="container">
                        <form method="get" action="{{ route('paginas.noticias') }}" class="text-center">
                            @php
                                $noticiasFiltroBtnClass = 'btn btn-light border-0 rounded-5 shadow-sm text-secondary flex-shrink-0 px-3 py-3 d-flex align-items-center justify-content-center';
                            @endphp
                            <div class="row justify-content-center align-items-center g-3 mb-3">
                                {{-- Desktop: filtro à esquerda do campo de busca --}}
                                <div class="col-auto d-none d-md-flex align-items-center order-md-1">
                                    <button type="button" class="{{ $noticiasFiltroBtnClass }}"
                                        data-bs-toggle="collapse" data-bs-target="#noticias-filtros-collapse"
                                        aria-controls="noticias-filtros-collapse"
                                        aria-expanded="{{ $noticiasFiltrosPainelAberto ? 'true' : 'false' }}"
                                        title="{{ __('Mostrar ou ocultar filtros') }}">
                                        <i class="fa-solid fa-filter fa-lg" aria-hidden="true"></i>
                                        <span class="visually-hidden">{{ __('Mostrar ou ocultar filtros') }}</span>
                                    </button>
                                </div>
                                {{-- Campo de busca (único name="q") --}}
                                <div class="col-12 col-md-6 col-xl-6 order-1 order-md-2">
                                    <label for="filtro-noticias-q" class="visually-hidden">{{ __('Buscar notícias') }}</label>
                                    <input id="filtro-noticias-q" class="shadow-sm p-3 border-0 rounded-5 form-control w-100"
                                        type="search" name="q" value="{{ request('q') }}"
                                        placeholder="{{ __('Procure uma notícia...') }}"
                                        aria-label="{{ __('Buscar notícias') }}" maxlength="255" autocomplete="off">
                                </div>
                                {{-- Mobile: filtro à esquerda de Buscar; Desktop: só Buscar e Limpar --}}
                                <div
                                    class="col-12 col-md-auto order-3 order-md-3 d-flex flex-wrap justify-content-center align-items-center gap-2">
                                    <button type="button" class="{{ $noticiasFiltroBtnClass }} d-md-none"
                                        data-bs-toggle="collapse" data-bs-target="#noticias-filtros-collapse"
                                        aria-controls="noticias-filtros-collapse"
                                        aria-expanded="{{ $noticiasFiltrosPainelAberto ? 'true' : 'false' }}"
                                        title="{{ __('Mostrar ou ocultar filtros') }}">
                                        <i class="fa-solid fa-filter fa-lg" aria-hidden="true"></i>
                                        <span class="visually-hidden">{{ __('Mostrar ou ocultar filtros') }}</span>
                                    </button>
                                    <button class="px-4 rounded-5 fw-bolder btn btn-success text-secondary" type="submit"
                                        id="searchNewsButton">{{ __('Buscar') }}</button>
                                    <a href="{{ route('paginas.noticias') }}"
                                        class="px-4 rounded-5 fw-bolder btn btn-outline-secondary text-secondary">{{ __('Limpar') }}</a>
                                </div>
                            </div>
                            <div id="noticias-filtros-collapse"
                                class="row justify-content-center g-3 collapse w-100 @if ($noticiasFiltrosPainelAberto) show @endif">
                                <div class="col-12 col-sm-6 col-lg-4">
                                    <label for="filtro-noticias-secretaria" class="visually-hidden">{{ __('Secretaria') }}</label>
                                    <select id="filtro-noticias-secretaria" name="secretaria_id"
                                        class="form-select shadow-sm border-0 rounded-5 py-3"
                                        aria-label="{{ __('Filtrar por secretaria') }}">
                                        <option value="">{{ __('Todas as secretarias') }}</option>
                                        @foreach ($secretariasFiltro as $secretaria)
                                            <option value="{{ $secretaria->id }}" @selected(request('secretaria_id') === $secretaria->id)>
                                                {{ $secretaria->name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="col-12 col-sm-6 col-lg-4">
                                    <label for="filtro-noticias-ano" class="visually-hidden">{{ __('Ano') }}</label>
                                    <select id="filtro-noticias-ano" name="ano"
                                        class="form-select shadow-sm border-0 rounded-5 py-3"
                                        aria-label="{{ __('Filtrar por ano') }}">
                                        <option value="">{{ __('Todos os anos') }}</option>
                                        @foreach ($anos as $ano)
                                            <option value="{{ $ano }}" @selected((string) request('ano') === (string) $ano)>{{ $ano }}
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="col-12 col-sm-6 col-lg-4">
                                    <label for="filtro-noticias-categoria" class="visually-hidden">{{ __('Categoria') }}</label>
                                    <select id="filtro-noticias-categoria" name="categoria"
                                        class="form-select shadow-sm border-0 rounded-5 py-3"
                                        aria-label="{{ __('Filtrar por categoria') }}">
                                        <option value="">{{ __('Todas as categorias') }}</option>
                                        @foreach ($categorias as $categoriaEnum)
                                            <option value="{{ $categoriaEnum->name }}" @selected(request('categoria') === $categoriaEnum->name)>
                                                {{ __($categoriaEnum->value) }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            @php $i = 0; @endphp
            <!-- Exibir as 2 primeiras notícias -->
            @foreach ($noticias->slice(0, 2) as $noticia)
                <div class="col-md-6 bottom20">
                    <div onclick="window.location='{{ route('paginas.slug', [date('d-m-Y', strtotime($noticia->created_at)), $noticia->slug]) }}';"
                        class="card news-card h-100">
                        <img src="{{ file_exists( $pathSystem . $noticia->featuredPhoto ) ? asset($noticia->featuredPhoto) : asset($angraDefault) }}" class="card-img-top news-image-large h-100"
                            alt="Notícia">
                        <div class="news-content p-3">
                            <h5 class="news-title">{{ html_entity_decode($noticia->title) }}</h5>
                            <p class="card-text">{{ Str::limit(html_entity_decode($noticia->summary), 100) }}</p>
                            <p class="news-date">
                                {{ app(App\Http\Controllers\Controller::class)->formatDateToPortuguese($noticia->created_at) }}
                            </p>
                        </div>
                    </div>
                </div>
            @endforeach

            <!-- Exibir as próximas 3 notícias -->
            @foreach ($noticias->slice(2, 3) as $noticia)
                <div class="col-md-4 bottom20">
                    <div onclick="window.location='{{ route('paginas.slug', [date('d-m-Y', strtotime($noticia->created_at)), $noticia->slug]) }}';"
                        class="card news-card h-100">
                        <img src="{{ file_exists( $pathSystem . $noticia->featuredPhoto ) ? asset($noticia->featuredPhoto) : asset($angraDefault) }}" class="card-img-top news-image-small" alt="Notícia">
                        <div class="news-content p-3">
                            <h5 class="news-title">{{ html_entity_decode($noticia->title) }}</h5>
                            <p class="card-text">{{ Str::limit(html_entity_decode($noticia->summary), 100) }}</p>
                            <p class="news-date">
                                {{ app(App\Http\Controllers\Controller::class)->formatDateToPortuguese($noticia->created_at) }}
                            </p>
                        </div>
                    </div>
                </div>
            @endforeach

            <div id="mainNews"></div>
            <div id="noticiasMenoresDesktop" class="row">
                <!-- Exibir as notícias restantes -->
                <div class="col-md-9">
                        @foreach ($noticias->slice(5, 10) as $noticia)
                        <div onclick="window.location='{{ route('paginas.slug', [date('d-m-Y', strtotime($noticia->created_at)), $noticia->slug]) }}';"
                            class="card news-card mb-5 d-flex flex-row align-items-center">
                            <img src="{{ file_exists( $pathSystem . $noticia->featuredPhoto ) ? asset($noticia->featuredPhoto) : asset($angraDefault) }}" class="news-image-menor" alt="Notícia">
                            <div class="news-content p-4">
                                <h5 class="news-title mt-3">{{ html_entity_decode($noticia->title) }}</h5>
                                <p class="news-summary">{{ Str::limit(html_entity_decode($noticia->summary), 150) }}</p>
                                <p class="news-date">
                                    {{ app(App\Http\Controllers\Controller::class)->formatDateToPortuguese($noticia->created_at) }}
                                </p>
                            </div>
                        </div>
                        @endforeach
                    </div>

                <div class="col-md-3 d-flex flex-column align-items-end">
                    @forelse($banners as $banner)
                        <div class="d-flex justify-content-center align-items-center my-2">
                            <a href='{{ $banner->url }}' target="{{ $banner->target }}" alt="{{ $banner->title }}"
                                title="{{ $banner->title }}">
                                <img src="{{ asset($banner->photo) }}" alt="{{ $banner->title }}"
                                    title="{{ $banner->title }}" class="img-fluid rounded shadow-sm">
                            </a>
                        </div>
                    @empty
                    @endforelse
                </div>
            </div>

            <!-- MOBILE - Cards menores empilhados e banners (restantes) -->
            <div id="noticiasMenoresMobile" class="row m-auto">
                @php $i = 5; @endphp
                @foreach ($noticias as $noticia)
                    @if($i >= 5 && $i < 15)
                        <div class="col-12 bottom20">
                            <div onclick="window.location='{{ route('paginas.slug', [date('d-m-Y', strtotime($noticia->created_at)), $noticia->slug]) }}';"
                                class="card news-card h-100" style="height: 100% !important;">
                                <img src="{{ asset($noticia->featuredPhoto) }}" class="card-img-top news-image-large h-100"
                                    alt="Notícia">
                                <div class="news-content p-3">
                                    <h5 class="news-title">
                                        {{ html_entity_decode($noticia->title) }}
                                    </h5>
                                    <p class="card-text">{{ Str::limit(html_entity_decode($noticia->summary), 100) }}</p>
                                    <p class="news-date">
                                        {{ app(App\Http\Controllers\Controller::class)->formatDateToPortuguese($noticia->created_at) }}
                                    </p>
                                </div>
                            </div>
                        </div>
                        @php $i++; @endphp
                    @else
                        @php break; @endphp
                    @endif
                @endforeach
                <div class="col-12 d-flex flex-column justify-content-center align-items-center">
                    @forelse($banners as $banner)
                        <a class="my-2" href='{{ $banner->url }}' target="{{ $banner->target }}" alt="{{ $banner->title }}"
                            title="{{ $banner->title }}">
                            <img src="{{ asset($banner->photo) }}" alt="{{ $banner->title }}" title="{{ $banner->title }}"
                                class="img-fluid rounded shadow-sm">
                        </a>
                    @empty

                    @endforelse
                </div>
            </div>

            <div class="row">
                <div class="col-12 my-3">
                    <div class="d-flex justify-content-center">
                        {!! $noticias->appends(request()->query())->links() !!}
                    </div>
                </div>
            </div>

        </div>
    </div>
</section>

<script>
    document.addEventListener("DOMContentLoaded", function () {
        if (window.location.hash) {
            var element = document.querySelector(window.location.hash);
            if (element) {
                element.scrollIntoView({
                    behavior: "smooth",
                    block: "start"
                });
            }
        }
    });

</script>

@endsection
