@extends('layouts.admin.theme')
@section("title", env("APP_NAME") . " :: Lista de Notícias")
@section('content')

    <div class="card shadow-sm">

        @php $headLine = "<i class='fa-solid fa-bullhorn'></i> Lista de Notícias"; @endphp

        @include('components.buttons._headLineButtonsBackAdd', [
                'headLine' => __($headLine),
                'routeAdd' => 'admin.noticias.create',
                'routeAddParams' => [],
                'nameAdd' => __('Nova notícia'),
                "permission" => "GERENCIADOR_DE_NOTICIAS",
            ])

        <div class="card-body pt-3">
            <div class="mb-4">
                <span class="badge bg-primary fs-6">
                    <i class="fa-solid fa-newspaper me-1"></i>
                    {{ __('Total de notícias publicadas') }}: <strong>{{ number_format($totalNoticiasPublicadas, 0, ',', '.') }}</strong>
                </span>
            </div>

            @include("errors.messageFlash")

            @php
                $noticiasListQueryParams = ['page' => request()->input('page', 1)];
                foreach (['q', 'dta', 'secretaria_id', 'ano', 'categoria'] as $noticiasListKey) {
                    if (request()->filled($noticiasListKey)) {
                        $noticiasListQueryParams[$noticiasListKey] = request($noticiasListKey);
                    }
                }
            @endphp

            <form method="get" action="{{ route('admin.noticias.index') }}" class="mb-4 mt-3">
                <div class="row g-3 mb-3">
                    <div class="col-12 col-md-6 col-lg-5">
                        <label for="admin-noticias-q" class="form-label mb-2">{{ __('Termos') }}</label>
                        <input id="admin-noticias-q" type="text" name="q" class="form-control"
                            value="{{ request('q') }}" placeholder="{{ __('Título, resumo ou corpo') }}"
                            maxlength="255" autocomplete="off">
                    </div>
                    <div class="col-12 col-md-3 col-lg-3">
                        <label for="admin-noticias-dta" class="form-label mb-2">{{ __('Data de publicação') }}</label>
                        <input id="admin-noticias-dta" type="date" name="dta" class="form-control"
                            value="{{ request('dta') }}">
                    </div>
                    <div class="col-12 col-md-3 col-lg-4">
                        <label for="admin-noticias-secretaria" class="form-label mb-2">{{ __('Secretaria') }}</label>
                        <select id="admin-noticias-secretaria" name="secretaria_id" class="form-select">
                            <option value="">{{ __('Todas') }}</option>
                            @foreach ($secretariasFiltro as $secretaria)
                                <option value="{{ $secretaria->id }}" @selected(request('secretaria_id') === $secretaria->id)>
                                    {{ $secretaria->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="row g-3 align-items-end">
                    <div class="col-12 col-sm-4 col-md-3 col-lg-2">
                        <label for="admin-noticias-ano" class="form-label mb-2">{{ __('Ano') }}</label>
                        <select id="admin-noticias-ano" name="ano" class="form-select">
                            <option value="">{{ __('Todos') }}</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-8 col-md-5 col-lg-5">
                        <label for="admin-noticias-categoria" class="form-label mb-2">{{ __('Categoria') }}</label>
                        <select id="admin-noticias-categoria" name="categoria" class="form-select">
                            <option value="">{{ __('Todas') }}</option>
                            @foreach ($categorias as $categoriaEnum)
                                <option value="{{ $categoriaEnum->name }}" @selected(request('categoria') === $categoriaEnum->name)>
                                    {{ __($categoriaEnum->value) }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-12 col-md-4 col-lg-5 d-flex flex-wrap gap-2">
                        <button type="submit" class="btn btn-outline-success">
                            <i class="fa-solid fa-magnifying-glass me-1" aria-hidden="true"></i>{{ __('Filtrar') }}
                        </button>
                        <a href="{{ route('admin.noticias.index') }}" class="btn btn-outline-secondary">{{ __('Limpar') }}</a>
                    </div>
                </div>
            </form>

            @php
                $pathSystem = $_SERVER['DOCUMENT_ROOT'] . "/";
                $angraDefault = asset("/pmar/assets/img/angra-default.png");
            @endphp

            <x-tables._table :thead="['#', 'Publicada em', 'Título', 'Descrição', 'Fixar', 'Status', 'Ações']" :pagination="$noticias->links()">
                <x-slot name="body">

                    @php $index = 0; @endphp
                        @foreach($noticias as $noticia)
                        <tr>
                            <td style="width:150px">
                                @include('components.tables._openPhoto', [
                                        'asset' => file_exists( $pathSystem . $noticia->featuredPhoto ) ? $noticia->featuredPhoto : $angraDefault,
                                        'alt' => $noticia->title,
                                        'title' => $noticia->title,
                                        'text' => 'Visualizar foto',
                                    ])
                            </td>
                            <td>{{ date("d/m/Y H:m:s", strtotime($noticia->created_at)) }} </td>
                            <td> {{ $noticia->title }} </td>
                            <td>{!! Str::limit($noticia->summary, 50) !!} </td>
                            <td class="text-center align-middle">
                                @include('components.buttons._status', [
                                                "index" => $index,
                                                "route" => "admin.noticias.fix",
                                                "id" => $noticia->id,
                                                "status" => $noticia->fix,
                                                "permission" => "GERENCIADOR_DE_NOTICIAS",
                                                "queryParams" => $noticiasListQueryParams,
                                            ])
                            </td>
                            <td class="text-center align-middle">
                                @include('components.buttons._status', [
                                                "index" => $index,
                                                "route" => "admin.noticias.status",
                                                "id" => $noticia->id,
                                                "status" => $noticia->status,
                                                "permission" => "GERENCIADOR_DE_NOTICIAS",
                                                "queryParams" => $noticiasListQueryParams,
                                            ])
                            </td>
                            <td>
                                <div class="d-flex gap-2 justify-content-center">
                                    @include('components.buttons._editButton', [
                                                    "route" => "admin.noticias.edit",
                                                    "id" => $noticia->id,
                                                    "title" => "Editar",
                                                    "fontawesome" => null,
                                                    "permission" => "GERENCIADOR_DE_NOTICIAS",
                                                ])

                                    @include('components.buttons._deleteButton', [
                                                    "message" => __("* Esta notícia será excluída e esta ação não poderá ser desfeita, deseja continuar??"),
                                                    "route" => "admin.noticias.destroy",
                                                    "id" => $noticia->id,
                                                    "title" => "Excluir",
                                                    "fontawesome" => null,
                                                    "permission" => "GERENCIADOR_DE_NOTICIAS",
                                                    "queryParams" => $noticiasListQueryParams,
                                                ])
                                </div>
                            </td>
                        </tr>
                        @php $index++; @endphp
                    @endforeach

                </x-slot>
            </x-tables._table>
        </div>
    </div>
@endsection
