@extends('layouts.admin.theme')
@section("title", env("APP_NAME") . " :: Atualizar Página HTML")
@section('content')
    <form action="{{ route('admin.htmls.update', $html->id) }}" method="post" enctype="multipart/form-data">
        <div class="container mt-5">
            <div class="row">
                <div class="col-12">
                    <div class="card shadow">
                        @php
                            $headLine = __('Atualizar Página HTML');
                        @endphp
                        @include('components.buttons._headLineButtonsBackAdd', [
                            'headLine' => $headLine,
                            'routeBack' => 'admin.htmls.index',
                            'routeBackParams' => $filterParams ?? [],
                            'nameBack' => __('Voltar'),
                            'permission' => 'GERENCIADOR_DE_PAGINAS_HTML',
                        ])
                        <div class="card-body">
                            @csrf
                            @method('patch')
                            <input type="hidden" name="return_url" value="{{ old('return_url', $returnUrl ?? null) }}">
                            @if(!empty($filterParams))
                                @foreach($filterParams as $key => $value)
                                    @if($value !== null && $value !== '')
                                        <input type="hidden" name="{{ $key }}" value="{{ $value }}">
                                    @endif
                                @endforeach
                            @endif
                            @if ($errors->any())
                                @foreach ($errors->all() as $error)
                                    <div class="alert alert-danger">
                                        {!! $error !!}
                                    </div>
                                @endforeach
                            @endif

                            @if (session('error'))
                                <div class="alert alert-danger">
                                    {{ session('error') }}
                                </div>
                            @endif
                            <div class="mb-3">
                                <input type="hidden" name="id" value="{{ old("id", $html->id) }}">
                                <input type="hidden" name="slug" value="{{ old("slug", $html->slug) }}">
                                <input type="hidden" name="order" value="{{ old("order", $html->order) }}">
                                <input type="hidden" name="structure_id" value="{{ old("structure_id", $html->structure_id) }}">
                                <label for="type" class="form-label text-bold">{{ __('Pertence a:') }}</label>
                                <select class="form-control" name="structure_id" id="structure_id">
                                    <option value="">---</option>
                                    @foreach($secretarias as $secretaria)
                                        @if($secretaria->id == $html->structure_id)
                                            <option value="{{ $secretaria->id }}" selected="true">{{ $secretaria->name }}</option>
                                        @else
                                            <option value="{{ $secretaria->id }}">{{ $secretaria->name }}</option>
                                        @endif
                                    @endforeach
                                </select>
                            </div>
                            <div class="mb-3">
                                <label for="reusable_menu_id" class="form-label text-bold">
                                    {{ __('Menu reutilizável (opcional)') }}
                                </label>
                                <select class="form-control" name="reusable_menu_id" id="reusable_menu_id">
                                    <option value="">{{ __('Sem menu reutilizável (usar menu personalizado, se ativado)') }}</option>
                                    @foreach($reusableMenus as $menu)
                                        @php
                                            $selected = old('reusable_menu_id', $html->reusable_menu_id) == $menu->id;
                                        @endphp
                                        <option value="{{ $menu->id }}" {{ $selected ? 'selected' : '' }}>
                                            {{ $menu->title }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('reusable_menu_id')
                                    <small class="text-danger text-bold">{{ $message }}</small>
                                @enderror
                                @if(auth()->user()->getPermissaoDetalhes("GERENCIADOR_DE_PAGINAS_HTML")['editar'] ?? false)
                                    <a href="{{ route('admin.htmls.menu.index', $html->id) }}" class="btn btn-sm btn-outline-primary mt-2">
                                        <i class="fa-solid fa-list"></i> {{ __('Editar Menu Personalizado') }}
                                    </a>
                                @endif
                            </div>
                            <div class="mb-3">
                                <label for="title" class="form-label text-bold">{{ __('Título da Página:') }} <span
                                        class="span-admin-create">*</span></label>
                                <input type="text" class="form-control @error('title') is-invalid @enderror" name="title"
                                    id="title" autocomplete="off" value="{{ old('title', $html->title) }}">
                                @error('title')
                                    <small class="text-danger text-bold">{{ $message }}</small><br>
                                @enderror
                                <small id="charCounterTitleNews" class="text-muted">0/85</small>
                            </div>
                            <div class="mb-3 text-wrap">
                                <label for="summary" class="form-label text-bold">{{ __('Descrição sumária:') }} <span
                                        class="span-admin-create">*</span></label>
                                <textarea class="form-control @error('summary') is-invalid @enderror" name="summary"
                                    id="summary">{{ old('summary', $html->summary) }}</textarea>
                                @error('summary')
                                    <small class="text-danger text-bold">{{ $message }}</small>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label for="display_context" class="form-label text-bold">
                                    {{ __('Onde esta página deve aparecer?') }} <span class="span-admin-create">*</span>
                                </label>
                                @php $currentDisplayContext = old('display_context', $html->display_context ?? 'BOTH'); @endphp
                                <select class="form-control @error('display_context') is-invalid @enderror" name="display_context" id="display_context">
                                    <option value="ORG_PAGE" {{ $currentDisplayContext === 'ORG_PAGE' ? 'selected' : '' }}>
                                        {{ __('Somente na página do órgão') }}
                                    </option>
                                    <option value="INDEPENDENT" {{ $currentDisplayContext === 'INDEPENDENT' ? 'selected' : '' }}>
                                        {{ __('Somente como página independente') }}
                                    </option>
                                    <option value="BOTH" {{ $currentDisplayContext === 'BOTH' ? 'selected' : '' }}>
                                        {{ __('Na página do órgão e como página independente') }}
                                    </option>
                                </select>
                                <small class="text-muted">
                                    {{ __('Use "Somente na página do órgão" para evitar duplicidade com a URL independente.') }}
                                </small>
                                @error('display_context')
                                    <small class="text-danger text-bold">{{ $message }}</small>
                                @enderror
                            </div>

                            <ul class="nav nav-tabs m-0 p-0" id="myTab" role="tablist">
                                @include('components/lists/_liLinkEditHtml', [
                                    'array_tab' => [
                                        ['hash' => 'html-tab-pane', 'label' => __('HTML'), 'class' => 'active', 'nome' => 'html', 'elements' => 'html-tab-pane', 'selecionar' => 'true'],
                                        ['hash' => 'css-tab-pane', 'label' => __('CSS'), 'class' => '', 'nome' => 'css', 'elements' => 'css-tab-pane', 'selecionar' => 'false'],
                                        ['hash' => 'js-tab-pane', 'label' => __('JS'), 'class' => '', 'nome' => 'js', 'elements' => 'js-tab-pane' , 'selecionar' => 'false'],
                                    ]
                                ])
                            </ul>
                            <div class="tab-content mt-0">
                                <div class="tab-pane fade show active" id="html-tab-pane" role="tabpanel"
                                    aria-labelledby="html-tab" tabindex="0">
                                    <div class="mb-3 text-wrap">
                                        <textarea class="form-control @error('code') is-invalid @enderror" name="code"
                                            id="code" style="height: 600px">{{ old('code', $html->code) }}</textarea>
                                        @error('code')
                                            <small class="text-danger text-bold">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                                <div class="tab-pane fade" id="css-tab-pane" role="tabpanel" aria-labelledby="css-tab"
                                    tabindex="0">
                                    <div class="mb-3 text-wrap">
                                        <textarea class="form-control @error('css') is-invalid @enderror" name="css"
                                            id="css" style="height: 600px">{{ old('css', $html->css) }}</textarea>
                                        @error('css')
                                            <small class="text-danger text-bold">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                                <div class="tab-pane fade" id="js-tab-pane" role="tabpanel" aria-labelledby="js-tab"
                                    tabindex="0">
                                    <div class="mb-3 text-wrap">

                                        <textarea class="form-control @error('js') is-invalid @enderror" name="js" id="js"
                                            style="height: 600px">{{ old('js', $html->js) }}</textarea>
                                        @error('js')
                                            <small class="text-danger text-bold">{{ $message }}</small>
                                        @enderror
                                    </div>
                                </div>
                            </div>
                            <div class="mb-3">
                                <label for="visibility" class="form-label text-bold">{{ __('* Visível na Lista de Serviços') }}</label>
                                <select class="form-control @error('visibility') is-invalid @enderror" name="visibility"
                                    id="visibility">
                                    @foreach(App\Enums\StatusEnum::cases() as $status)
                                        @if($html->visibility == $status->name)
                                            <option value="{{ $status->name }}" selected="true">{{ $status->value }}</option>
                                        @else
                                            <option value="{{ $status->name }}">{{ $status->value }}</option>
                                        @endif
                                    @endforeach
                                </select>
                                @error('visibility')
                                    <small class="text-danger text-bold">{{ $message }}</small>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label for="status" class="form-label text-bold">{{ __('* Status') }}</label>
                                <select class="form-control @error('status') is-invalid @enderror" name="status"
                                    id="status">
                                    @foreach(App\Enums\StatusEnum::cases() as $status)
                                        @if(($html->status ?? 'ATIVADO') == $status->name)
                                            <option value="{{ $status->name }}" selected="true">{{ $status->value }}</option>
                                        @else
                                            <option value="{{ $status->name }}">{{ $status->value }}</option>
                                        @endif
                                    @endforeach
                                </select>
                                @error('status')
                                    <small class="text-danger text-bold">{{ $message }}</small>
                                @enderror
                            </div>
                            <div class="mb-3">
                                <div class="mb-3">
                                <a href="{{ route('admin.htmls.index', $filterParams ?? []) }}" class="btn btn-lg btn-secondary d-inline">{{ __('Voltar') }}</a>

                                <button type="submit" class="btn btn-lg btn-info d-inline text-white" formaction="{{ route('admin.htmls.preview') }}" formtarget="_blank">
                                    <i class="fa-solid fa-eye"></i> {{ __('Pré-visualizar') }}
                                </button>

                                <button class="btn btn-lg btn-dark d-inline" type="submit">{{ __('Enviar') }}</button>
                            </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
@endsection
