@extends('layouts.backend')

@section('title', $product->exists ? 'Editando '.$product->post->title : 'Crear nuevo producto')

@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:;">eCommerce</a></li>
                        <li class="breadcrumb-item"><a class="breadcrumb-link" href="{{ route('backend.products.index') }}">Productos</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($product, [
        'method' => $product->exists ? 'put' : 'post',
        'route' => $product->exists ? ['backend.products.update', $product->id] : 'backend.products.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 producto</h4>
                </div>
                <!-- End Header -->

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

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

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

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

                                <div class="input-group input-group-merge">
                                    {!! Form::number('weight', null, ['class' => 'form-control', 'id' => 'weightLabel', 'step' => 0.001, 'placeholder' => '0.000', 'min' => 0]) !!}
                                    <div class="input-group-append">
                                        <!-- Select -->
                                        <div id="weightSelect" class="select2-custom select2-custom-right">
                                            <select class="js-select2-custom custom-select" size="1" style="opacity: 0;"
                                                    data-hs-select2-options='{
                                      "dropdownParent": "#weightSelect",
                                      "dropdownAutoWidth": true,
                                      "width": true
                                    }'>
                                                <option value="kg" selected>kg</option>
                                            </select>
                                        </div>
                                        <!-- End Select -->
                                    </div>
                                </div>
                            </div>
                            <!-- End Form Group -->
                        </div>
                    </div>
                    <!-- End Row -->

                    {{--<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 breve</label>

                        {!! Form::textarea('post[excerpt]', null, ['class' => 'form-control', 'id' => 'textLabel', 'rows' => 3]) !!}
                    </div>
                    <!-- End Form Group -->
                <!-- Form Group -->
                    <div class="form-group">
                        <label for="contentLabel" class="input-label">Descripción</label>

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

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

                <!-- Body -->
                <div class="card-body">
                    <div class="form-group">
                        <label for="sizesLabel" class="input-label">Tamaños</label>
                        {!! Form::text('sizes', null, ['class' => 'js-tagify tagify-form-control form-control', 'id' => 'sizesLabel', 'placeholder' => 'Enter para separar']) !!}
                    </div>
                    <div class="form-group">
                        <label for="colorsLabel" class="input-label">Colores</label>
                        {!! Form::text('colors', null, ['class' => 'js-tagify tagify-form-control form-control', 'id' => 'colorsLabel', 'placeholder' => 'Enter para separar']) !!}
                    </div>
                    <div class="form-group">
                        <label for="materialsLabel" class="input-label">Material</label>
                        {!! Form::text('materials', null, ['class' => 'js-tagify tagify-form-control form-control', 'id' => 'materialsLabel', 'placeholder' => 'Enter para separar']) !!}
                    </div>
                </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">Disponibilidad</h4>
                </div>
                <!-- End Header -->

                <!-- Body -->
                <div class="card-body">
                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="priceNameLabel" class="input-label">Precio</label>

                        <div class="input-group">
                            {!! Form::text('price', null, ['class' => 'form-control number', 'id' => 'priceNameLabel', 'min' => 0]) !!}

                            <div class="input-group-append">
                                <!-- Select -->
                                <div id="priceSelect" class="select2-custom select2-custom-right">
                                    <select class="js-select2-custom custom-select" size="1" style="opacity: 0;"
                                            data-hs-select2-options='{
                                    "dropdownParent": "#priceSelect",
                                    "dropdownAutoWidth": true,
                                    "width": true
                                  }'>
                                        <option value="PYG" selected>PYG</option>
                                    </select>
                                </div>
                                <!-- End Select -->
                            </div>
                        </div>
                    </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">Destacado</span>
                          </span>
                        <span class="col-4 col-sm-3">
                            {!! Form::hidden('post[featured]', 0) !!}
                            {!! Form::checkbox('post[featured]', 1, $product->exists ? $product->post->featured : 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 -->
                    <!-- 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, $product->exists ? $product->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 -->

            <!-- Card -->
            <div class="card mb-3 mb-lg-5">
                <!-- Header -->
                <div class="card-header">
                    <h4 class="card-header-title">Características</h4>
                </div>
                <!-- End Header -->

                <!-- Body -->
                <div class="card-body">
                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="vendorLabel" class="input-label">Marca</label>

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

                    <!-- Form Group -->
                    <div class="form-group">
                        <label for="categoryLabel" class="input-label">Categorías</label>

                        <!-- Select -->
                    {!! Form::select('category_id[]', $categories, $product->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 -->
                    <div class="form-group mb-0">
                        <label for="tagsLabel" class="input-label">Etiquetas</label>
                        {!! Form::text('tags', null, ['class' => 'js-tagify tagify-form-control form-control', 'id' => 'tagsLabel', 'placeholder' => 'Enter para separar']) !!}
                    </div>
                </div>
                <!-- End 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">{{ $product->exists ? 'Actualizar' : 'Crear' }} producto</button>
                </div>
            </div>
        </div>
        @if($product->exists)
            <div class="col-12">
                @include('backend.partials.images', ['model' => $product])
            </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
