@extends('layouts.backend')

@section('title', $section->exists ? 'Editando '.$section->post->title : 'Crear nueva sección')

@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.sections.index') }}">Secciones</a></li>
                        @if($section->page)
                            <li class="breadcrumb-item"><a class="breadcrumb-link" href="{{ route('backend.pages.edit', $section->page->id) }}">{{ $section->page->post->title }}</a></li>
                        @endif
                        <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($section, [
        'method' => $section->exists ? 'put' : 'post',
        'route' => $section->exists ? ['backend.sections.update', $section->id] : 'backend.sections.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 sección</h4>
                </div>
                <!-- End Header -->

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

                {{--<label class="input-label">Descripción</label>--}}

                {{--<!-- Quill -->--}}
                {{--<div class="quill-custom">--}}
                {{--<div class="js-quill" style="min-height: 15rem;">--}}
                {{--</div>--}}
                {{--</div>--}}
                {{--<!-- End Quill -->--}}

                <!-- Form Group -->
                    <div class="form-group">
                        <label for="textLabel" class="input-label">Texto</label>

                        {!! Form::textarea('post[excerpt]', null, ['class' => 'form-control', 'id' => 'textLabel', 'rows' => 5]) !!}
                    </div>
                    <!-- End Form Group -->

                <!-- Form Group -->
                    <div class="form-group">
                        <label for="contentLabel" class="input-label">Contenido</label>

                        {!! Form::textarea('post[content]', null, ['class' => 'form-control ckeditor', 'id' => 'contentLabel', 'rows' => 5]) !!}
                    </div>
                    <!-- End Form Group -->

                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="urlLabel" class="input-label">Enlace</label>

                        {!! Form::text('url', null, ['class' => 'form-control', 'id' => 'urlLabel']) !!}
                    </div>
                    <!-- End Form Group -->
                </div>
                <!-- Body -->
            </div>
            <!-- End Card -->
        </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="selectorLabel" class="input-label">Plantilla</label>

                        <!-- Select -->
                    {!! Form::select('selector', ['' => 'Ninguna'] + $selectors, $section->selector, ['class' => 'js-select2-custom custom-select', 'size' => '1', 'style' => 'opacity: 0;', 'id' => 'selectorLabel', 'data-hs-select2-options' => '{
                        "minimumResultsForSearch": "Infinity"
                      }']) !!}
                    <!-- End Select -->
                    </div>
                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="categoryLabel" class="input-label">Páginas</label>

                        <!-- Select -->
                    {!! Form::select('page_id',  ['' => 'Ninguna'] + $pages, $section->page_id, ['class' => 'js-select2-custom custom-select', 'size' => '1', 'style' => 'opacity: 0;', 'id' => 'categoryLabel', 'data-hs-select2-options' => '{
                        "minimumResultsForSearch": "Infinity"
                      }']) !!}
                    <!-- End Select -->
                    </div>
                    <!-- Form Group -->

                    <!-- 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, $section->exists ? $section->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">{{ $section->exists ? 'Actualizar' : 'Crear' }} sección</button>
                </div>
            </div>
        </div>
        @if($section->exists)
        <div class="col-12">
            @include('backend.partials.images', ['model' => $section])
        </div>
        @endif
    </div>
    <!-- End Row -->
    {!! Form::close() !!}
@endsection

@push('scripts')
    <!-- Modal -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                {!! Form::open([
                    'method' => 'delete'
                ]) !!}
                <div class="modal-header">
                    <h5 class="modal-title" id="deleteModalLabel">Eliminar imagen</h5>
                    <button type="button" class="btn btn-xs btn-icon btn-ghost-secondary" data-dismiss="modal" aria-label="Close">
                        <i class="tio-clear tio-lg"></i>
                    </button>
                </div>
                <div class="modal-body">
                    <span></span>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">Cancelar</button>
                    <button class="btn btn-danger" type="submit">Eliminar</button>
                </div>
                {!! Form::close() !!}
            </div>
        </div>
    </div>
    <!-- End Modal -->
    <script>
      $(function() {
        $('.modal-toggle-delete').on('click', function() {
          $('#deleteModal form').attr('action', $(this).attr('data-href'));
          $('#deleteModal .modal-body span').text($(this).attr('data-title'));
          $('#deleteModal').modal('show');
          return false;
        });
      });
    </script>
    <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), {
            originalInputValueFormat: valuesArr => valuesArr.map(item => item.value).join(',')
          });
        });


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