<!-- BEGIN: Vendor JS-->

@vite([
  'resources/assets/vendor/libs/jquery/jquery.js',
  'resources/assets/vendor/libs/popper/popper.js',
  'resources/assets/vendor/js/bootstrap.js',
  'resources/assets/vendor/libs/@algolia/autocomplete-js.js',
   'resources/assets/vendor/libs/sweetalert2/sweetalert2.js',
   'resources/assets/vendor/libs/select2/select2.js',
   'resources/assets/vendor/libs/notyf/notyf.js',
   'resources/js/app.js',
  ])

@if ($configData['hasCustomizer'])
    @vite('resources/assets/vendor/libs/pickr/pickr.js')
@endif

@vite([
  'resources/assets/vendor/libs/perfect-scrollbar/perfect-scrollbar.js',
  'resources/assets/vendor/libs/hammer/hammer.js',
  'resources/assets/vendor/js/menu.js',
  'resources/assets/vendor/libs/select2/select2.js',
])

@yield('vendor-script')

<script type="module">
  Dropzone.autoDiscover = false;
  console.log('Dropzone.autoDiscover', Dropzone.autoDiscover);
(function( factory ){
  if ( typeof define === 'function' && define.amd ) {
    // AMD
    define( ['jquery', 'datatables.net', 'datatables.net-editor'], factory );
  }
  else if ( typeof exports === 'object' ) {
    // Node / CommonJS
    module.exports = function ($, dt) {
      if ( ! $ ) { $ = require('jquery'); }
      factory( $, dt || $.fn.dataTable || require('datatables') );
    };
  }
  else if ( jQuery ) {
    // Browser standard
    factory( jQuery, jQuery.fn.dataTable );
  }
}(function( $, DataTable ) {
  'use strict';

  if ( ! DataTable.ext.editorFields ) {
    DataTable.ext.editorFields = {};
  }

  var _fieldTypes = DataTable.Editor ?
  DataTable.Editor.fieldTypes :
  DataTable.ext.editorFields;

  _fieldTypes.select2 = {
    // Select2 nell'Editor DataTables: non usare $(".modal") — prende il primo modal
    // della pagina (es. #basicModal su prima_nota) e il dropdown si disallinea con
    // zoom/scroll, soprattutto sui campi a metà form (modal-dialog-scrollable).
    _dropdownParent: function ( $input ) {
      var $parent = $input.closest('.modal');
      if ( !$parent.length ) {
        $parent = $('.modal.DTED.show, .modal.show').last();
      }
      return $parent.length ? $parent : $(document.body);
    },

    _addOptions: function ( conf, opts ) {
      var elOpts = conf._input[0].options;

      elOpts.length = 0;

      if ( opts ) {
        DataTable.Editor.pairs( opts, conf.optionsPair, function ( val, label, i ) {
          elOpts[i] = new Option( label, val );
        } );
      }
    },

    create: function ( conf ) {
      conf._input = $('<select/>')
      .attr( $.extend( {
        id: DataTable.Editor.safeId( conf.id )
      }, conf.attr || {} ) );

      this.one("open", function(){

        var options = $.extend( {
          width: '100%',
          dropdownParent: _fieldTypes.select2._dropdownParent( conf._input ),
        }, conf.opts );

        _fieldTypes.select2._addOptions( conf, conf.options || conf.ipOpts );
        conf._input.select2( options );

        var open;
        conf._input
        .on( 'select2:open', function () {
          open = true;
        } )
        .on( 'select2:close', function () {
          open = false;
        } );

        // Prima apertura: applica l'ultimo valore richiesto da Editor.set()
        if ( conf._pendingVal !== undefined ) {
          conf._input
          .val( conf._pendingVal )
          .trigger( 'change', { editor: true } );
        }

        // On open, need to have the instance update now that it is in the DOM
        this.one( 'open.select2-'+DataTable.Editor.safeId( conf.id ), function () {
          options.dropdownParent = _fieldTypes.select2._dropdownParent( conf._input );
          conf._input.select2( options );

          if ( open ) {
            conf._input.select2( 'open' );
          }
        });
      });

      return conf._input[0];
    },

    get: function ( conf ) {
      var val = conf._input.val();
      val =  conf._input.prop('multiple') && val === null ?
      [] :
      val;

      return conf.separator ?
      val.join( conf.separator ) :
      val;
    },

    set: function ( conf, val ) {
      var field = this.field(conf.name);
      conf._pendingVal = val;

      if ( conf.separator && ! Array.isArray( val ) ) {
        val = val === null
        ? []
        : val.split(conf.separator);
      }

      // Clear out any existing tags
      if ( conf.opts && conf.opts.tags ) {
        conf._input.val([]);
      }

      // The value isn't present in the current options list, so we need to add it
      // in order to be able to select it. Note that this makes the set action async!
      // It doesn't appear to be possible to add an option to select2, then change
      // its label and update the display
      var needAjax = false;

      if ( conf.opts && conf.opts.ajax ) {
        if ( Array.isArray( val ) ) {
          for ( var i=0, ien=val.length ; i<ien ; i++ ) {
            if ( conf._input.find('option[value="'+val[i]+'"]').length === 0 ) {
              needAjax = true;
              break;
            }
          }
        }
        else {
          if ( conf._input.find('option[value="'+val+'"]').length === 0 ) {
            needAjax = true;
          }
        }
      }

      if ( needAjax && val ) {
        field.processing(true);

        var functionData = {};
        if ( typeof conf.opts.ajax.data === 'function' ) {
          functionData = conf.opts.ajax.data({
            term: val,
            page: 0
          });
        }

        $.ajax( $.extend( {
          beforeSend: function ( jqXhr, settings ) {
            // Add an initial data request to the server, but don't
            // override `data` since the dev might be using that
            var initData = conf.urlDataType === undefined || conf.urlDataType === 'json'
            ? 'initialValue=true&value=' + JSON.stringify(val) + '&' + $.param(functionData)
            : $.param($.extend(functionData, {initialValue: true, value: val}));

            if ( typeof conf.opts.ajax.url === 'function' ) {
              settings.url = conf.opts.ajax.url();
            }

            if ( settings.type === 'GET' ) {
              settings.url += settings.url.indexOf('?') === -1 ?
              '?'+initData :
              '&'+initData;
            }
            else {
              settings.data = settings.data ?
              settings.data +'&'+ initData :
              initData;
            }
          },
          success: function ( json ) {
            var addOption = function ( option ) {
              if ( conf._input.find('option[value="'+option.id+'"]').length === 0 ) {
                $('<option/>')
                .attr('value', option.id)
                .text( option.text )
                .appendTo( conf._input );
              }
            }

            if ( Array.isArray( json ) ) {
              for ( var i=0, ien=json.length ; i<ien ; i++ ) {
                addOption( json[i] );
              }
            }
            else if ( json.results && Array.isArray( json.results ) ) {
              for ( var i=0, ien=json.results.length ; i<ien ; i++ ) {
                addOption( json.results[i] );
              }
            }
            else {
              addOption( json );
            }

            conf._input
            .val( val )
            .trigger( 'change', {editor: true} );
            field.processing(false);
          }
        }, conf.opts.ajax ) );
      }
      else {
        conf._input
        .val( val )
        .trigger( 'change', {editor: true} );
      }
    },

    enable: function ( conf ) {
      $(conf._input).removeAttr( 'disabled' );
    },

    disable: function ( conf ) {
      $(conf._input).attr( 'disabled', 'disabled' );
    },

    // Non-standard Editor methods - custom to this plug-in
    inst: function ( conf ) {
      var args = Array.prototype.slice.call( arguments );
      args.shift();

      return conf._input.select2.apply( conf._input, args );
    },

    update: function ( conf, data ) {
      var val = _fieldTypes.select2.get( conf );

      _fieldTypes.select2._addOptions( conf, data );

      // Restore null value if it was, to let the placeholder show
      if ( val === null ) {
        _fieldTypes.select2.set.call( this, conf, null );
      }

      $(conf._input).trigger('change', {editor: true} );
    },

    focus: function ( conf ) {
      if ( conf._input.is(':visible') && conf.onFocus === 'focus' ) {
        conf._input.select2('open');
      }
    },

    owns: function ( conf, node ) {
      if ( $(node).closest('.select2-container').length || $(node).closest('.select2').length || $(node).hasClass('select2-selection__choice__remove') ) {
        return true;
      }
      return false;
    },

    canReturnSubmit: function() {
      return false;
    }
  };

}));

//QUILL
(function( factory ){
  if ( typeof define === 'function' && define.amd ) {
    // AMD
    define( ['jquery', 'datatables', 'datatables-editor'], factory );
  }
  else if ( typeof exports === 'object' ) {
    // Node / CommonJS
    module.exports = function ($, dt) {
      if ( ! $ ) { $ = require('jquery'); }
      factory( $, dt || $.fn.dataTable || require('datatables') );
    };
  }
  else if ( jQuery ) {
    // Browser standard
    factory( jQuery, jQuery.fn.dataTable );
  }
}(function( $, DataTable ) {
  'use strict';


  if ( ! DataTable.ext.editorFields ) {
    DataTable.ext.editorFields = {};
  }

  var _fieldTypes = DataTable.Editor ?
  DataTable.Editor.fieldTypes :
  DataTable.ext.editorFields;


  // Default toolbar, as Quill doesn't provide one
  var defaultToolbar =
  '<div id="toolbar-toolbar" class="toolbar">'+
  '<span class="ql-formats">'+
  '<select class="ql-font">'+
  '<option selected=""></option>'+
  '<option value="serif"></option>'+
  '<option value="monospace"></option>'+
  '</select>'+
  '<select class="ql-size">'+
  '<option value="small"></option>'+
  '<option selected=""></option>'+
  '<option value="large"></option>'+
  '<option value="huge"></option>'+
  '</select>'+
  '</span>'+
  '<span class="ql-formats">'+
  '<button class="ql-bold"></button>'+
  '<button class="ql-italic"></button>'+
  '<button class="ql-underline"></button>'+
  '<button class="ql-strike"></button>'+
  '</span>'+
  '<span class="ql-formats">'+
  '<select class="ql-color"></select>'+
  '<select class="ql-background"></select>'+
  '</span>'+
  '<span class="ql-formats">'+
  '<button class="ql-list" value="ordered"></button>'+
  '<button class="ql-list" value="bullet"></button>'+
  '<select class="ql-align">'+
  '<option selected=""></option>'+
  '<option value="center"></option>'+
  '<option value="right"></option>'+
  '<option value="justify"></option>'+
  '</select>'+
  '</span>'+
  '<span class="ql-formats">'+
  '<button class="ql-link"></button>'+
  '</span>'+
  '</div>';


  _fieldTypes.quill = {
    create: function ( conf ) {
      var safeId = DataTable.Editor.safeId( conf.id );
      var input = $(
        '<div id="'+safeId+'" class="quill-wrapper">'+
        (conf.toolbarHtml || defaultToolbar)+
        '<div class="editor"></div>'+
        '</div>'
      );

      conf._quill = new Quill( input.find('.editor')[0], $.extend( true, {
        theme: 'snow',
        modules: {
          toolbar: {
            container: $('div.toolbar', input)[0]
          }
        }
      }, conf.opts ) );

      return input;
    },

    get: function ( conf ) {
      return conf._quill.root.innerHTML;
    },

    set: function ( conf, val ) {
      conf._quill.root.innerHTML = val !== null ? val : '';
    },

    enable: function ( conf ) {}, // not supported by Quill

    disable: function ( conf ) {}, // not supported by Quill

    // Get the Quill instance
    quill: function ( conf ) {
      return conf._quill;
    }
  };


}));
//END QUILL
</script>


<!-- END: Page Vendor JS-->
<!-- BEGIN: Theme JS-->
@vite(['resources/assets/js/main.js'])

<!-- END: Theme JS-->
<!-- Pricing Modal JS-->
@stack('pricing-script')
<!-- END: Pricing Modal JS-->
<!-- BEGIN: Page JS-->
@yield('page-script')
<!-- END: Page JS-->

<!-- app JS -->
@vite(['resources/js/app.js'])
<!-- END: app JS-->
