<script src="<?php echo BASE_URL_ORIG;?>js/editor/tinymce.min.js" type="text/javascript"></script>
<script src="<?php echo BASE_URL;?>js/selectlive.js" type="text/javascript"></script>
<?php if ($jakuser->getVar("files") && $jakosub['files']) { ?>
<script type="text/javascript" src="<?php echo BASE_URL_ORIG;?>js/dropzone.js"></script>
<?php } ?>

<script type="text/javascript">

// Working Flag to 0
var working = false;
<?php if ($jakuser->getVar("files") && $jakosub['files']) { ?>
Dropzone.autoDiscover = false;
<?php } ?>

$(document).ready(function() {
  tinymce.init({
      selector: '#content-editor',
      height: 300,
      menubar: false,
      paste_as_text: true,
      entity_encoding : "raw",
      plugins: [
        'advlist autolink lists link charmap print preview anchor textcolor',
        'searchreplace visualblocks code fullscreen',
        'insertdatetime media table contextmenu paste code codesample'
      ],
      toolbar: 'undo redo | styleselect | bold italic underline | alignleft aligncenter alignright alignjustify | bullist numlist | removeformat codesample code',
      language: '<?php echo $BT_LANGUAGE;?>',
      <?php if ($jkl["rtlsupport"]) { ?>
      directionality : 'rtl',
      <?php } ?>
      relative_urls: false
    });

  if ($(".datepicker").length != 0) {
      $('.datepicker').datetimepicker({
        format: '<?php echo $duedateformat[1];?>',
        icons: {
          time: "fa fa-clock",
          date: "fa fa-calendar",
          up: "fa fa-chevron-up",
          down: "fa fa-chevron-down",
          previous: 'fa fa-chevron-left',
          next: 'fa fa-chevron-right',
          today: 'fa fa-screenshot',
          clear: 'fa fa-trash',
          close: 'fa fa-remove'
        }
      });
    }
    $(".loader").hide();

    <?php if ($jakuser->getVar("files") && $jakosub['files']) { ?>

    // Now that the DOM is fully loaded, create the dropzone, and setup the
    var myDropzone = new Dropzone("#share-attachments", {dictResponseError: "SERVER ERROR",
      dictDefaultMessage: '<i class="fa fa-file-upload fa-3x"></i>',
      acceptedFiles: "<?php echo JAK_ALLOWEDO_FILES;?>",
      addRemoveLinks: true,
      autoProcessQueue: false,
      url: "<?php echo BASE_URL_ORIG;?>uploader/uploaderto.php"});

    <?php } ?>

    // JavaScript to disable send button and show loading.gif image
$('.jak_form').submit(function(e){

  e.preventDefault();

  if(working) return false;
    
  working = true;

  $(".loader").show();
  $('.submitNT').attr("disabled", "disabled");
  $(this).find('input').removeClass("is-invalid");

  // Call the server and due the necessary
  var request = $.ajax({
      async: true,
        url: '<?php echo JAK_rewrite::jakParseurl('support', 'new');?>',
        type: "POST",
        data: $(this).serialize(),
        dataType: "json",
        processData: false,
        cache: false
    });
    
    request.done(function(data) {

      working = false;
      
      if(data.status) {

        <?php if ($jakuser->getVar("files") && $jakosub['files']) { ?>

        if (myDropzone.files != "") {

          myDropzone.on("sending", function(file, xhr, formData) {
            // Will send the filesize along with the file as POST data.
            formData.append("ticketId", data.ticketid);
            formData.append("userIDU", <?php echo $jakuser->getVar("id");?>);
            formData.append("base_url", "<?php echo BASE_URL_ORIG;?>");
            formData.append("operatorNameU", "<?php echo $jakuser->getVar("name");?>");
            formData.append("operatorLanguage", "<?php echo $USER_LANGUAGE;?>");
          });

          myDropzone.processQueue();

          myDropzone.on("queuecomplete", function(a,b,c){
            // Redirect to the ticket
            window.location.replace(data.redirecturl);
          });

        } else {
          // Redirect to the ticket
          window.location.replace(data.redirecturl);
        }

        <?php } else { ?>

          // Redirect to the ticket
          window.location.replace(data.redirecturl);

        <?php } ?>

      } else {

        $.notifyClose();

        // We will need to go through each error message
        $.each(data.errors,function(k,v) {
          $.notify({icon: 'warning', message: v}, {type: 'danger', animate: {
            enter: 'animate__animated animate__fadeInDown',
            exit: 'animate__animated animate__fadeOutUp'
          }});
          // console.log(v);
          $('#'+k).addClass("is-invalid");
        });

        $(".loader").hide();
        $('.submitNT').removeAttr("disabled");

      }


    });
    
});
    
});

// Insert the standard response into the editor
$('select#supresp').on("change", function() {
  var respid = this.value;
  if (respid != 0) {

    // Get the value from the selected client
    var cvalue = $('#jak_client').val();
    // Split the value
    var cname = cvalue.split(':#:');

    $.ajax({
    type: "POST",
    url: ls.main_url + 'ajax/oprequests.php',
    data: "oprq=supportresponse&respid="+respid+"&cname="+cname[2]+"&uid="+ls.opid,
    dataType: 'json',
    success: function(msg){

      if (msg.status) {
        tinymce.activeEditor.execCommand('mceInsertContent', false, msg.response);
        md.showNotification('check', '<?php echo $jkl['g14'];?>', 'success', 'top', 'right');
      } else {
        md.showNotification('warning', '<?php echo $jkl['g116'];?>', 'danger', 'top', 'right');
      }
      
    }
  });
    $('#supresp').val(0);
  }
});

$(document).on("click", ".btn-translate", function(e) {
  e.preventDefault();

  var _this = $(this);
  var wscan = $(_this).data("scan");
  var tcontent = tinymce.get("content-editor").getContent();
  var tlang = $('#jak_tlang').find(":selected").val();

  // Check if we have something in the editor
  if (tcontent == "") return false;

  // Check if we translate something but the language is empty
  if (wscan == "translate" && tlang == "-") {

    md.showNotification('warning', '<?php echo $jkl['e47'];?>', 'danger', 'top', 'right');
    return false;

  }

  // Show the loader
  $(".tl-loader", _this).removeClass("d-none");
          
  $.ajax({
    type: "POST",
    url: ls.main_url + 'ajax/translate_ticket.php',
    data: "wsource="+wscan+"&tcontent="+tcontent+"&uid="+ls.opid+"&lang="+tlang,
    dataType: 'json',
    success: function(msg){
              
      if (msg.status == 1) {

        // Success message
        md.showNotification('check', msg.txt, 'success', 'top', 'right');

        // We will add the translation below
        if (aid == 0) {
          $('.aitranslate'+tid).html(msg.translation).fadeIn();
        } else {
          $('.aitranslate'+aid).html(msg.translation).fadeIn();
        }

        // We have a new language
        if (msg.lang) $("#jak_tlang").val(msg.lang);
        $('.selectpicker').selectpicker('refresh');

      } else if (msg.status == 2) {

        // We have a fine new text, let's add it
        tinymce.activeEditor.setContent(msg.grammar_correct);


      } else if (msg.status == 3) {

        // We have a translation, let's add it
        tinymce.activeEditor.setContent(msg.translate_correct);

      } else {
        md.showNotification('warning', msg.txt, 'danger', 'top', 'right');
      }

      // Hide the loader
      $(".tl-loader", _this).addClass("d-none");
      return true;
    }
  });
});

</script>
