@extends('layouts.backend')

@section('title', $article->exists ? 'Editando '.$article->post->title : 'Crear nuevo artículo')

@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.articles.index') }}">Artículos</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($article, [
        'method' => $article->exists ? 'put' : 'post',
        'route' => $article->exists ? ['backend.articles.update', $article->id] : 'backend.articles.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 artículo</h4>
                </div>
                <!-- End Header -->

                <!-- Body -->
                <div class="card-body">
                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="articleNameLabel" class="input-label">Nombre</label>
                        {!! Form::text('post[title]', null, ['class' => 'form-control', 'id' => 'articleNameLabel']) !!}
                    </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">Descripción</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="publishedLabel" class="input-label">Fecha de publicación</label>

                        <div id="projectDeadlineNewProjectFlatpickr" class="js-flatpickr flatpickr-custom input-group input-group-merge"
                             data-hs-flatpickr-options='{
        "appendTo": "#projectDeadlineNewProjectFlatpickr",
        "dateFormat": "d/m/Y",
        "wrap": true
      }'>
                            <div class="input-group-prepend" data-toggle>
                                <div class="input-group-text">
                                    <i class="tio-date-range"></i>
                                </div>
                            </div>

                            {!! Form::text('published_at', null, ['class' => 'flatpickr-custom-form-control form-control', 'id' => 'publishedLabel', 'placeholder' => 'Seleccionar fecha', 'data-input']) !!}
                        </div>
                    </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="categoryLabel" class="input-label">Categorías</label>

                        <!-- Select -->
                    {!! Form::select('category_id[]', $categories, $article->post->categories, ['class' => 'js-select2-custom custom-select', 'size' => '1', 'style' => 'opacity: 0;', 'multiple', 'id' => 'categoryLabel', 'data-hs-select2-options' => '{
                        "minimumResultsForSearch": "Infinity",
                        "placeholder": "Seleccionar categoría"
                      }']) !!}
                    <!-- 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, $article->exists ? $article->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">{{ $article->exists ? 'Actualizar' : 'Crear' }} artículo</button>
                </div>
            </div>
        </div>
        @if($article->exists)
            <div class="col-12">
                @include('backend.partials.images', ['model' => $article])
            </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
