@extends('layouts.backend')

@section('title', $page->exists ? 'Editando '.$page->post->title : 'Crear nueva página')

@section('content')
    <!-- Page Header -->
    <div class="page-header">
        <div class="row align-items-center">
            <div class="col-sm mb-2 mb-sm-0">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb breadcrumb-no-gutter">
                        <li class="breadcrumb-item"><a class="breadcrumb-link" href="javascript:;">Contenido</a></li>
                        <li class="breadcrumb-item"><a class="breadcrumb-link" href="{{ route('backend.pages.index') }}">Páginas</a></li>
                        <li class="breadcrumb-item active" aria-current="page">@yield('title')</li>
                    </ol>
                </nav>

                <h1 class="page-header-title">@yield('title')</h1>
            </div>
        </div>
        <!-- End Row -->
    </div>
    <!-- End Page Header -->

    {!! Form::model($page, [
        'method' => $page->exists ? 'put' : 'post',
        'route' => $page->exists ? ['backend.pages.update', $page->id] : 'backend.pages.store',
        'files' => true
    ]) !!}
    <div class="row">
        <div class="col-lg-8">

            <!-- Card -->
            <div class="card mb-3 mb-lg-5">
                <!-- Header -->
                <div class="card-header">
                    <h4 class="card-header-title">Información de página</h4>
                </div>
                <!-- End Header -->

                <!-- Body -->
                <div class="card-body">
                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="pageNameLabel" class="input-label">Nombre</label>
                        {!! Form::text('post[title]', null, ['class' => 'form-control', 'id' => 'pageNameLabel']) !!}
                    </div>
                    <!-- End Form Group -->

                    <div class="row">
                        <div class="col-sm-6">
                            <!-- Form Group -->
                            <div class="form-group">
                                <label for="urlLabel" class="input-label">Enlace <i class="tio-help-outlined text-body ml-1" data-toggle="tooltip" data-placement="top" title="Código del página"></i></label>

                                {!! Form::text('post[slug]', null, ['class' => 'form-control', 'id' => 'urlLabel']) !!}
                            </div>
                            <!-- End Form Group -->
                        </div>

                        <div class="col-sm-6">
                            <!-- Form Group -->
                            <div class="form-group">
                                <label for="weightLabel" class="input-label">Plantilla</label>

                                {!! Form::select('template', ['' => 'Predeterminada'] + $templates, null, ['class' => 'js-select2-custom custom-select', 'size' => '1', 'style' => 'opacity: 0;', 'id' => 'categoryLabel', 'data-hs-select2-options' => '{
    "minimumResultsForSearch": "Infinity"
  }']) !!}
                            </div>
                            <!-- End Form Group -->
                        </div>
                    </div>
                    <!-- End Row -->
                </div>
                <!-- Body -->
            </div>
            <!-- End Card -->
            @if($page->sections->count())
                <div class="card mb-3 mb-lg-5">
                    <!-- Header -->
                    <div class="card-header">
                        <h4 class="card-header-title">Secciones</h4>
                    </div>
                    <!-- End Header -->

                    <!-- Body -->
                    <div class="card-body">
                        <div class="row">
                            @foreach($page->sections as $section)
                                <div class="col-md-6 mt-3"><a href="{{ route('backend.sections.edit', $section->id) }}">{{ $section->post->title ?? 'Sección #'.$section->id }} ({{ $section->selector_name }})</a></div>
                            @endforeach
                        </div>
                    </div>
                </div>
            @endif

        </div>

        <div class="col-lg-4">
            <!-- Card -->
            <div class="card mb-3 mb-lg-5">
                <!-- Header -->
                <div class="card-header">
                    <h4 class="card-header-title">Visibilidad</h4>
                </div>
                <!-- End Header -->

                <!-- Body -->
                <div class="card-body">
                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="parentLabel" class="input-label">Página superior</label>
                        <!-- Select -->
                    {!! Form::select('post[parent_id]', ['' => 'Ninguna'] + $pages, null, ['class' => 'js-select2-custom custom-select', 'size' => '1', 'style' => 'opacity: 0;', 'id' => 'parentLabel', 'data-hs-select2-options' => '{
                        "minimumResultsForSearch": "Infinity"
                      }']) !!}
                    <!-- End Select -->
                    </div>
                    <!-- End Form Group -->
                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="positionLabel" class="input-label">Posición en el menú</label>
                        <!-- Select -->
                    {!! Form::select('position_id[]', config('cms.positions'), $page->position_id ?? [1], ['class' => 'js-select2-custom custom-select', 'size' => '1', 'multiple', 'style' => 'opacity: 0;', 'id' => 'positionLabel', 'data-hs-select2-options' => '{
                        "minimumResultsForSearch": "Infinity"
                      }']) !!}
                    <!-- End Select -->
                    </div>
                    <!-- End Form Group -->
                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="targetLabel" class="input-label">Comportamiento</label>
                        <!-- Select -->
                    {!! Form::select('target', config('cms.targets'), null, ['class' => 'js-select2-custom custom-select', 'size' => '1', 'style' => 'opacity: 0;', 'id' => 'targetLabel', 'data-hs-select2-options' => '{
                        "minimumResultsForSearch": "Infinity"
                      }']) !!}
                    <!-- End Select -->
                    </div>
                    <!-- End Form Group -->


                    <hr class="my-4">

                    <!-- Toggle Switch -->
                    <label class="row toggle-switch" for="activeSwitch1">
                          <span class="col-8 col-sm-9 toggle-switch-content ml-0">
                            <span class="text-dark">Activo</span>
                          </span>
                        <span class="col-4 col-sm-3">
                            {!! Form::hidden('post[active]', 0) !!}
                            {!! Form::checkbox('post[active]', 1, $page->exists ? $page->post->active : 1, ['class' => 'toggle-switch-input', 'id' => 'activeSwitch1']) !!}
                            <span class="toggle-switch-label ml-auto">
                              <span class="toggle-switch-indicator"></span>
                            </span>
                          </span>
                    </label>
                    <!-- End Toggle Switch -->
                </div>
                <!-- Body -->
            </div>
            <!-- End Card -->

            <div class="card">
                <div class="card-body d-sm-flex align-items-sm-center">
                    <button type="submit" class="btn btn-primary btn-block">{{ $page->exists ? 'Actualizar' : 'Crear' }} página</button>
                </div>
            </div>
        </div>
        @if($page->exists)
        <div class="col-12">
            @include('backend.partials.images', ['model' => $page])
        </div>
        @endif
    </div>
    <!-- End Row -->
    {!! Form::close() !!}
@endsection

@push('scripts')
    <script>
      $(function() {
        // INITIALIZATION OF ADD INPUT FILED
        // =======================================================
        $('.js-add-field').each(function () {
          new HSAddField($(this), {
            addedField: function() {
              $('.js-add-field .js-select2-custom-dynamic').each(function () {
                var select2dynamic = $.HSCore.components.HSSelect2.init($(this));
              });
            }
          }).init();
        });


        // INITIALIZATION OF TAGIFY
        // =======================================================
        $('.js-tagify').each(function () {
          var tagify = $.HSCore.components.HSTagify.init($(this));
        });


        // INITIALIZATION OF DROPZONE FILE ATTACH MODULE
        // =======================================================
        $('.js-dropzone').each(function () {
          var dropzone = $.HSCore.components.HSDropzone.init('#' + $(this).attr('id'));
        });
      });
    </script>
@endpush
