<?php $inputId = isset($select_id) ? $select_id : null; ?>
<script>
  //var z= <?php echo $defaultValue; ?>;
  console.log('defaultValue '+typeof z);
  var unitHandler = <?php echo isset($handler)?1:0; ?>

    $select = $('#<?php echo e($inputId); ?>').selectize({
        preload: true,
        valueField: '<?php echo $valueField; ?>',
        labelField: '<?php echo $labelField; ?>',
        delimiter: ',',
        openOnFocus : true,
        hideSelected:true,
        <?php if(isset($maxItem)): ?>
        maxItems : '<?php echo $maxItem; ?>',
        <?php endif; ?>
        searchField: '<?php echo $searchField; ?>',
        onChange: function(value) {
          var contact = this.options[value];
          if(unitHandler){
            $( "#<?php echo e($inputId); ?>" ).trigger( "unit:change", contact.alias );
          }
          
          },
        load: function(query, callback) {
          console.log('Load');
          $.ajax({
            url: "<?php echo e(env('APP_URL')); ?>" + "<?php echo $url; ?>"+ encodeURIComponent(query),
            type: 'GET',
            error: function(err) {
                console.log('error occuered '+err.responseText);
              callback();
            },
            success: function(res) {
                //var r = JSON.parse(res);
                console.log('data loaded '+res);
                //$select.options = res; 
                callback(res);
                
              //callback(res.matches.slice(0, 10));
            }
          });
        },
        onLoad: function() {
            console.log('onLoad');
            <?php if(isset($defaultValue)): ?>
                preloadedItems = [ <?php echo $defaultValue; ?> ];
                //console.log('populating item '+<?php echo $defaultValue; ?>);
                this.setValue(preloadedItems);
            <?php endif; ?>   
        
    }
      });

    

    </script>
<?php /**PATH /var/www/amartools/resources/views/partials/selectize.blade.php ENDPATH**/ ?>