@extends('layouts.admin.theme')
@section("title", env("APP_NAME") . " :: Lista de Páginas HTML")
@section('content')

    <div class="card shadow-sm">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h5 class="my-2">{{ __('Lista de Páginas HTML') }}</h5>
        </div>
        <div class="card-body">
            @if (session('success'))
                <div class="alert alert-success">{{ session('success') }}</div>
            @endif

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

            <h6 class="card-subtitle mb-2 text-body-secondary">
                @if (auth()->user()->getPermissaoDetalhes("GERENCIADOR_DE_PAGINAS_HTML")['adicionar'])
                    @include('components.buttons._newButton', ['url' => 'admin.htmls.create', 'text' => __('Nova Página HTML')])
                @endif
            </h6>

            <div class="mb-3 alert alert-light">
                <div class="mb-3 bg-gray border-0 rounded-4 shadow-sm p-3">
                    <div class="tab-pane fade show active" id="termos" role="tabpanel" aria-labelledby="termos-tab">
                        <form class="d-flex" method="get" action="{{ route('admin.htmls.index') }}">
                            <input
                                type="text"
                                class="form-control me-2 border-0"
                                name="q"
                                value="{{ request()->input('q') }}"
                                placeholder="{{ __('Buscar na publicação') }}"
                                aria-label="{{ __('Pesquisar') }}"
                            >
                            <button class="btn btn-success text-secondary fw-bold px-4 py-2 rounded-4" type="submit">
                                {{ __('Buscar') }}
                            </button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table table-bordered table-hover table-striped align-middle text-center">
                    @include('components.tables._thead', ['thead' => [__('Data de criação'), __('Título'), __('Pertence a'), __('Exibição'), __('Link'), __('Menu Personalizado'), __('Visível na Lista de Serviços'), __('Status')]])
                    @php
                        $index = 0;
                    @endphp
                    @forelse($htmls as $html)
                        <tr>
                            <td> {{ date("d/m/Y H:i:s", strtotime($html->created_at)) }} </td>
                            <td>
                                {{ $html->title }}
                                <div class="d-flex gap-2 justify-content-center mt-2">
                                    @include('components.buttons._editButton', [
                                        "route" => "admin.htmls.edit",
                                        "id" => $html->id,
                                        "title" => __('Editar'),
                                        "fontawesome" => null,
                                        "permission" => "GERENCIADOR_DE_PAGINAS_HTML",
                                    ])
                                    @include('components.buttons._deleteButton', [
                                        "message" => __("* Esta página HTML será excluída e esta ação não poderá ser desfeita, deseja continuar?"),
                                        "route" => "admin.htmls.destroy",
                                        "id" => $html->id,
                                        "title" => __('Excluir'),
                                        "fontawesome" => null,
                                        "permission" => "GERENCIADOR_DE_PAGINAS_HTML",
                                    ])
                                </div>
                            </td>
                            <td>
                                @if(empty($html->structure_id))
                                    ---
                                @else
                                    {{ optional($html->pertence)->name }}
                                @endif
                            </td>
                            <td>
                                @if(($html->display_context ?? 'BOTH') === 'ORG_PAGE')
                                    {{ __('Somente no órgão') }}
                                @elseif(($html->display_context ?? 'BOTH') === 'INDEPENDENT')
                                    {{ __('Somente independente') }}
                                @else
                                    {{ __('Órgão + independente') }}
                                @endif
                            </td>
                            <td>
                                @if(($html->display_context ?? 'BOTH') !== 'ORG_PAGE')
                                    <a class="text-dark text-decoration-none d-inline fs-5"
                                        href='{{ url("servicos/" . $html->slug) }}' target="_blank" alt="{{ $html->title }}"
                                        title="{{ $html->title }}">
                                        <i class="fa-solid fa-link"></i>
                                    </a>
                                @else
                                    ---
                                @endif
                            </td>
                            <td>
                                @if (auth()->user()->getPermissaoDetalhes("GERENCIADOR_DE_PAGINAS_HTML")['editar'])
                                    <form id="{{ 'checkCustomMenu_' . $index }}" class="fix d-flex flex-column align-items-center gap-2"
                                        action="{{ route("admin.htmls.customMenu", $html->id) }}" method="post">
                                        @csrf
                                        @if(!empty($filterParams))
                                            @foreach($filterParams as $key => $value)
                                                @if($value !== null && $value !== '')
                                                    <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                                @endif
                                            @endforeach
                                        @endif
                                        <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                        <div class="form-check form-switch">
                                            <input type="hidden" name="custom_menu_enabled" value="0">
                                            <input class="form-check-input" type="checkbox" {{ $html->custom_menu_enabled ? 'checked' : '' }}
                                                onclick="var f=this.form; f.querySelector('input[name=custom_menu_enabled]').value=this.checked?'1':'0'; f.submit();">
                                            <label class="form-check-label small">{{ $html->custom_menu_enabled ? __('Ativado') : __('Desativado') }}</label>
                                        </div>
                                        <a href="{{ route('admin.htmls.menu.index', $html->id) }}" class="btn btn-sm btn-outline-primary">
                                            <i class="fa-solid fa-list"></i> {{ __('Editar Menu Personalizado') }}
                                        </a>
                                    </form>
                                @else
                                    {{ $html->custom_menu_enabled ? __('Ativado') : __('Desativado') }}
                                @endif
                            </td>
                            <td>
                                @if (auth()->user()->getPermissaoDetalhes("GERENCIADOR_DE_PAGINAS_HTML")['editar'])
                                    <form id="{{ 'checkVisibility_' . $index }}" class="fix d-flex justify-content-center"
                                        action="{{ route("admin.htmls.visibility", $html->id)}}" method="post">
                                        @csrf
                                        @if(!empty($filterParams))
                                            @foreach($filterParams as $key => $value)
                                                @if($value !== null && $value !== '')
                                                    <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                                @endif
                                            @endforeach
                                        @endif
                                        <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                        @if($html->visibility == "ATIVADO")
                                            <div class="form-check form-switch">
                                                <input type="hidden" name="checkVisibility" value="DESATIVADO">
                                                <input class="form-check-input" type="checkbox" checked
                                                    onclick="document.getElementById('checkVisibility_{{ $index }}').submit()">
                                            </div>
                                        @else
                                            <div class="form-check form-switch">
                                                <input type="hidden" name="checkVisibility" value="ATIVADO">
                                                <input class="form-check-input" type="checkbox"
                                                    onclick="document.getElementById('checkVisibility_{{ $index }}').submit()">
                                            </div>
                                        @endif
                                    </form>
                                @else
                                    {{ $html->visibility }}
                                @endif
                            </td>
                            <td>
                                @if (auth()->user()->getPermissaoDetalhes("GERENCIADOR_DE_PAGINAS_HTML")['editar'])
                                    <form id="{{ 'checkStatus_' . $index }}" class="fix d-flex justify-content-center"
                                        action="{{ route("admin.htmls.status", $html->id)}}" method="post">
                                        @csrf
                                        @if(!empty($filterParams))
                                            @foreach($filterParams as $key => $value)
                                                @if($value !== null && $value !== '')
                                                    <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                                @endif
                                            @endforeach
                                        @endif
                                        <input type="hidden" name="page" value="{{ request()->input('page', 1) }}">
                                        @if(($html->status ?? 'ATIVADO') == "ATIVADO")
                                            <div class="form-check form-switch">
                                                <input type="hidden" name="checkStatus" value="DESATIVADO">
                                                <input class="form-check-input" type="checkbox" checked
                                                    onclick="document.getElementById('checkStatus_{{ $index }}').submit()">
                                            </div>
                                        @else
                                            <div class="form-check form-switch">
                                                <input type="hidden" name="checkStatus" value="ATIVADO">
                                                <input class="form-check-input" type="checkbox"
                                                    onclick="document.getElementById('checkStatus_{{ $index }}').submit()">
                                            </div>
                                        @endif
                                    </form>
                                @else
                                    {{ $html->status ?? 'ATIVADO' }}
                                @endif
                            </td>
                        </tr>
                        @php
                            $index++;
                        @endphp
                    @empty
                        <tr>
                            <td colspan="8">
                                <p>{{ __('Nenhuma Página HTML encontrada.') }}</p>
                            </td>
                        </tr>
                    @endforelse
                </table>
            </div>
            <div class="row">
                <div class="col-12 my-3">
                    <div class="d-flex justify-content-center">{!! $htmls->links() !!} </div>
                </div>
            </div>
        </div>
    </div>
@endsection
