(function($) {
    "use strict";

  $(document).ready(function() {


function disablekey()
{
 document.onkeydown = function (e)
 {
  return false;
 }
}

function enablekey()
{
 document.onkeydown = function (e)
 {
  return true;
 }
}

// **************************************  AJAX REQUESTS SECTION *****************************************

  // Status Start
      $(document).on('click','.status',function () {
        var link = $(this).attr('data-href');
            $.get( link, function(data) {
              }).done(function(data) {
                $('#geniustable').DataTable().ajax.reload();
                  toastr.success(data);
            })
          });
  // Status Ends



// ADD OPERATION

$(document).on('click','#add-data',function(e){
  e.preventDefault();
  var url = $(this).attr('data-href');
  var modal1El = document.getElementById('modal1');
  if(!modal1El){ console.error('modal1 element not found'); return; }
  $('#modal1').find('.modal-title').html('ADD NEW '+$('#headerdata').val());
  $('#modal1 .modal-content .modal-body').html('<div class="text-center p-4"><div class="spinner-border" role="status"></div></div>');
  bootstrap.Modal.getOrCreateInstance(modal1El).show();
  $.get(url).done(function(response){
    $('#modal1 .modal-content .modal-body').html(response);
  }).fail(function(xhr, status, error){
    $('#modal1 .modal-content .modal-body').html('<div class="alert alert-danger">Error loading form: '+error+'</div>');
  });
});

// ADD OPERATION END


// Attribute Modal

$(document).on('click','.attribute',function(){
if(admin_loader == 1)
  {
  $('.submit-loader').show();
}
  $('#attribute').find('.modal-title').html($('#attribute_data').val());
  $('#attribute .modal-content .modal-body').html('').load($(this).attr('data-href'),function(response, status, xhr){
      if(status == "success")
      {
        if(admin_loader == 1)
          {
            $('.submit-loader').hide();
          }
      }

    });
});



// Attribute Modal Ends


// EDIT OPERATION

$(document).on('click','.edit',function(e){
  e.preventDefault();
  var modal1El = document.getElementById('modal1');
  $('#modal1').find('.modal-title').html('EDIT '+$('#headerdata').val());
  $('#modal1 .modal-content .modal-body').html('').load($(this).attr('data-href'),function(response, status, xhr){
      if(status == "success")
      {
        bootstrap.Modal.getOrCreateInstance(modal1El).show();
      }
    });
});


// EDIT OPERATION END




// EDIT OPERATION END





// ADD / EDIT FORM SUBMIT FOR DATA TABLE


$(document).on('submit','#geniusformdata',function(e){
  e.preventDefault();
  disablekey();
  // alert('ok');
  $('.gocover').show();
      $.ajax({
       method:"POST",
       url:$(this).prop('action'),
       data:new FormData(this),
       dataType:'JSON',
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
            $('button.addProductSubmit-btn').prop('disabled',false);
          }
          else
          {
            $('#geniustable').DataTable().ajax.reload();
            toastr.success(data);
            $('button.addProductSubmit-btn').prop('disabled',false);
            ['modal1','modal2','verify-modal'].forEach(function(id){ var el=document.getElementById(id); if(el){ var inst=bootstrap.Modal.getInstance(el); if(inst) inst.hide(); }});
           }
           $('.gocover').hide();
          enablekey();
       }

      });

});



$(document).on('submit','#geniusformdataedit',function(e){
  e.preventDefault();
  disablekey();
  $('.gocover').show();
      $.ajax({
       method:"POST",
       url:$(this).prop('action'),
       data:new FormData(this),
       dataType:'JSON',
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
            $('button.addProductSubmit-btn').prop('disabled',false);
          }
          else
          {
            $('#geniustable').DataTable().ajax.reload();
            toastr.success(data);
            $('button.addProductSubmit-btn').prop('disabled',false);
            ['modal1','modal2','verify-modal'].forEach(function(id){ var el=document.getElementById(id); if(el){ var inst=bootstrap.Modal.getInstance(el); if(inst) inst.hide(); }});
           }
           $('.gocover').hide();
          enablekey();
       }

      });

});



// ADD / EDIT FORM SUBMIT FOR DATA TABLE ENDS

// ADD / EDIT article


$(document).on('click','.tquiz-btn1',function(e){
  e.preventDefault();
  disablekey();
  $('.gocover').show();

  var draft =  $(this).data("draft");
  var description = $(".description div.nicEdit-main").html(); 

  var question_description = $(".question_description div.nicEdit-main").map(function() {
    return $(this).html();
 }).get();
 

  var result_description= $(".result_description div.nicEdit-main").map(function() {
    return $(this).html();
 }).get();
 
  
  let fd = new FormData(document.getElementById("tquizformdata"));
  fd.append('draft',draft);
  fd.append('description',description);

  let QueDesLength = question_description.length;
  for (let i=0; i<question_description.length; i++) {
    fd.append('question_description[]', question_description[i]);
    }

  let ResDesLength = result_description.length;
  for (let i=0; i<result_description.length; i++) {
    fd.append('result_description[]', result_description[i]);
    }
  
      $.ajax({
       method:"POST",
       url:$("#tquizformdata").prop('action'),
       data:fd,
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
            $('button.addProductSubmit-btn').prop('disabled',false);
          }
          else
          {
            toastr.success(data);
            $('button.addProductSubmit-btn').prop('disabled',false);
            ['modal1','modal2','verify-modal'].forEach(function(id){ var el=document.getElementById(id); if(el){ var inst=bootstrap.Modal.getInstance(el); if(inst) inst.hide(); }});
           }
          $('.gocover').hide();
          enablekey();
       }

      });
});

$(document).on('click','.sort-btn1',function(e){
  e.preventDefault();
  disablekey();
  $('.gocover').show();

  var draft =  $(this).data("draft");
  var description = $(".description div.nicEdit-main").html(); 

  var item_description= $(".item_description div.nicEdit-main").map(function() {
    return $(this).html();
 }).get();
 
 
  
  let fd = new FormData(document.getElementById("sortformdata"));
  fd.append('draft',draft);
  fd.append('description',description);

  for (let i=0; i<item_description.length; i++) {
    fd.append('item_description[]', item_description[i]);
    }

      $.ajax({
       method:"POST",
       url:$("#sortformdata").prop('action'),
       data:fd,
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
            $('button.addProductSubmit-btn').prop('disabled',false);
          }
          else
          {
            toastr.success(data);
            $('button.addProductSubmit-btn').prop('disabled',false);
            ['modal1','modal2','verify-modal'].forEach(function(id){ var el=document.getElementById(id); if(el){ var inst=bootstrap.Modal.getInstance(el); if(inst) inst.hide(); }});
           }
           $('.gocover').hide();
          enablekey();
       }

      });
});

$(document).on('click','.personality-btn1',function(e){
  e.preventDefault();
  disablekey();
  $('.gocover').show();

  var draft =  $(this).data("draft");
  var description = $(".description div.nicEdit-main").html(); 

  var question_description = $(".question_description div.nicEdit-main").map(function() {
    return $(this).html();
 }).get();
 

  var result_description= $(".result_description div.nicEdit-main").map(function() {
    return $(this).html();
 }).get();
 
 
  
  let fd = new FormData(document.getElementById("personalityformdata"));
  fd.append('draft',draft);
  fd.append('description',description);

  for (let i=0; i<question_description.length; i++) {
    fd.append('question_description[]', question_description[i]);
    }

  for (let i=0; i<result_description.length; i++) {
    fd.append('result_description[]', result_description[i]);
    }

      $.ajax({
       method:"POST",
       url:$("#personalityformdata").prop('action'),
       data:fd,
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
            $('button.addProductSubmit-btn').prop('disabled',false);
          }
          else
          {
            toastr.success(data);
            $('button.addProductSubmit-btn').prop('disabled',false);
            ['modal1','modal2','verify-modal'].forEach(function(id){ var el=document.getElementById(id); if(el){ var inst=bootstrap.Modal.getInstance(el); if(inst) inst.hide(); }});
           }
           $('.gocover').hide();
          enablekey();
       }

      });
});

$(document).on('click','.submit-btn1',function(e){
  e.preventDefault();
  disablekey();
  $('.gocover').show();
  var draft =  $(this).data("draft");
  var description = $(".description div.nicEdit-main").html(); 
  
  let fd = new FormData(document.getElementById("geniusformdata2"));
  fd.append('draft',draft);
  fd.append('description',description);

      $.ajax({
       method:"POST",
       url:$("#geniusformdata2").prop('action'),
       data:fd,
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
            $('button.addProductSubmit-btn').prop('disabled',false);
          }
          else
          {
            toastr.success(data);
            $('button.addProductSubmit-btn').prop('disabled',false);
            ['modal1','modal2','verify-modal'].forEach(function(id){ var el=document.getElementById(id); if(el){ var inst=bootstrap.Modal.getInstance(el); if(inst) inst.hide(); }});
           }
           $('.gocover').hide();
          enablekey();
       }

      });
});


// ADD / EDIT article ENDS

  $(document).on('click','.delete[data-href]',function(e){
    e.preventDefault();
    var href = $(this).attr('data-href');
    var target = $(this).attr('data-target') || '#confirm-delete';
    var modalEl = document.querySelector(target);
    if(modalEl){
      $(modalEl).find('.btn-ok').attr('href', href);
      bootstrap.Modal.getOrCreateInstance(modalEl).show();
    }
  });

  $('#confirm-delete .btn-ok').on('click', function(e) {

    $.ajax({
      type:"GET",
      url:$(this).attr('href'),
      success:function(data)
      {
        console.log(data);
          var cdEl = document.getElementById('confirm-delete'); if(cdEl) bootstrap.Modal.getInstance(cdEl).hide();
          $('#geniustable').DataTable().ajax.reload();
          toastr.success(data);
      }
    });
    return false;
  });

  $('#confirm-delete1 .btn-ok').on('click', function(e) {

  if(admin_loader == 1)
    {
    $('.submit-loader').show();
  }

    $.ajax({
      type:"GET",
      url:$(this).attr('href'),
      success:function(data)
      {
          var cd1El = document.getElementById('confirm-delete1'); if(cd1El) bootstrap.Modal.getInstance(cd1El).hide();
          $('#geniustable').DataTable().ajax.reload();
        if(admin == 3)
        {
      toastr.success(data[0]);
        }
        else{
      toastr.success(data);
        }


    if(admin_loader == 1)
      {
        $('.submit-loader').hide();
      }


      }
    });

    if($('#t-txt').length > 0)
{

  var tdata =  $('#t-txt').val();

  if(tdata.length > 0) {

    var id = $('#t-id').val();
    var title = $('#t-title').val();
    var text = $('#t-txt').val();
    $.ajax({
      url: $('#t-add').val(),
      method: "GET",
      data: { id : id, title: title, text : text }
    });

  }

}

    return false;
  });


  $('#confirm-delete2 .btn-ok').on('click', function(e) {

    $('.submit-loader').show();


    $.ajax({
      type:"GET",
      url:$(this).attr('href'),
      success:function(data)
      {
          var cd2El = document.getElementById('confirm-delete2'); if(cd2El) bootstrap.Modal.getInstance(cd2El).hide();
          if(admin == 3)
          {
        toastr.success(data);
          }
          else{
        toastr.success(data[0]);
        $(".nice-select.process.select.order-droplinks").attr('class','nice-select process select order-droplinks '+data[1]);
          }

        $(".nice-select.process.select.order-droplinks").attr('class','nice-select process select order-droplinks '+data[1]);
      }
    });

    return false;
  });

// DELETE OPERATION END

  });



// NORMAL FORM

$(document).on('submit','#geniusform',function(e){
e.preventDefault();
  var fd = new FormData(this);
  $('.gocover').show();
 
  if ($('.attr-checkbox').length > 0) {
    $('.attr-checkbox').each(function() {

      // if checkbox checked then take the value of corresponsig price input (if price input exists)
      if($(this).prop('checked') == true) {

        if ($("#"+$(this).attr('id')+'_price').val().length > 0) {
          // if price value is given
          fd.append($("#"+$(this).attr('id')+'_price').data('name'), $("#"+$(this).attr('id')+'_price').val());
        } else {
          // if price value is not given then take 0
          fd.append($("#"+$(this).attr('id')+'_price').data('name'), 0.00);
        }

        // $("#"+$(this).attr('id')+'_price').val(0.00);
      }
    });
  }

var geniusform = $(this);
$('button.addProductSubmit-btn').prop('disabled',true);
    $.ajax({
     method:"POST",
     url:$(this).prop('action'),
     data:fd,
     contentType: false,
     cache: false,
     processData: false,
     success:function(data)
     {
       
        if ((data.errors)) {
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
          geniusform.find('input , select , textarea').eq(1).focus();
        }
        else
        {
          toastr.success(data);
          geniusform.find('input , select , textarea').eq(1).focus();
        }

        $('button.addProductSubmit-btn').prop('disabled',false);
      
        $('.gocover').hide();
      
     }

    });

});

// NORMAL FORM ENDS



// NORMAL FORM

$(document).on('submit','#trackform',function(e){
  e.preventDefault();

  $('.gocover').show();


  $('button.addProductSubmit-btn').prop('disabled',true);
      $.ajax({
       method:"POST",
       url:$(this).prop('action'),
       data:new FormData(this),
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
            $('#trackform input , #trackform select , #trackform textarea').eq(1).focus();
          }
          else
          {
            toastr.success(data);
            $('#trackform input , #trackform select , #trackform textarea').eq(1).focus();
            $('#track-load').load($('#track-load').data('href'));
          }
          $('.gocover').hide();
          $('button.addProductSubmit-btn').prop('disabled',false);
       }

      });

});

// NORMAL FORM ENDS

// MESSAGE FORM

$(document).on('submit','#messageform',function(e){
  e.preventDefault();
  var href = $(this).data('href');
    $('.gocover').show();
  $('button.mybtn1').prop('disabled',true);
      $.ajax({
       method:"POST",
       url:$(this).prop('action'),
       data:new FormData(this),
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
            $('#messageform textarea').val('');
          }
          else
          {
            toastr.success(data);
            $('#messageform textarea').val('');
            $('#messages').load(href);
          }
            $('.gocover').hide();
          $('button.mybtn1').prop('disabled',false);
       }
      });
});

// MESSAGE FORM ENDS


// LOGIN FORM

$("#loginform").on('submit',function(e){
  e.preventDefault();
  $('button.submit-btn').prop('disabled',true);
  $('.alert-info').show();
  $('.alert-info p').html($('#authdata').val());
      $.ajax({
       method:"POST",
       url:$(this).prop('action'),
       data:new FormData(this),
       dataType:'JSON',
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
          $('.alert-info').hide();
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
          }
          else
          {
            $('.alert-info').hide();
            toastr.success('Success !');
            window.location = data;
          }
          $('button.submit-btn').prop('disabled',false);
       }

      });

});


// LOGIN FORM ENDS


// FORGOT FORM

$("#forgotform").on('submit',function(e){
  e.preventDefault();
  $('button.submit-btn').prop('disabled',true);
  $('.alert-info').show();
  $('.alert-info p').html($('#authdata').val());
      $.ajax({
       method:"POST",
       url:$(this).prop('action'),
       data:new FormData(this),
       dataType:'JSON',
       contentType: false,
       cache: false,
       processData: false,
       success:function(data)
       {
          if ((data.errors)) {
          $('.alert-info').hide();
            for(var error in data.errors)
            {
              toastr.error(data.errors[error]);
            }
          }
          else
          {
            $('.alert-info').hide();
            toastr.success(data);
            $('input[type=email]').val('');
          }
          $('button.submit-btn').prop('disabled',false);
       }

      });

});


// FORGOT FORM ENDS





// SEND MESSAGE SECTION
$(document).on('click','.send',function(){
  $('.eml-val').val($(this).data('email'));
});

          $(document).on("submit", "#emailreply1" , function(){
          var token = $(this).find('input[name=_token]').val();
          var subject = $(this).find('input[name=subject]').val();
          var message =  $(this).find('textarea[name=message]').val();
          var to = $(this).find('input[name=to]').val();
          $('#eml1').prop('disabled', true);
          $('#subj1').prop('disabled', true);
          $('#msg1').prop('disabled', true);
          $('#emlsub1').prop('disabled', true);
            $.ajax({
            type: 'post',
            url: mainurl+'/admin/user/send/message',
            data: {
                '_token': token,
                'subject'   : subject,
                'message'  : message,
                'to'   : to
                  },
                 success: function( data) {
                  $('#eml1').prop('disabled', false);
                  $('#subj1').prop('disabled', false);
                  $('#msg1').prop('disabled', false);
                  $('#subj1').val('');
                  $('#msg1').val('');
                  $('#emlsub1').prop('disabled', false);
                  if(data == 0)
                    toastr.error('Oops Something Goes Wrong !!');
                  else
                    toastr.success('Message Sent !!');
                  $('.close').click();
            }
        });
          return false;
        });

// SEND MESSAGE SECTION ENDS



// SEND EMAIL SECTION

          $(document).on("submit", "#emailreply" , function(){
          var token = $(this).find('input[name=_token]').val();
          var subject = $(this).find('input[name=subject]').val();
          var message =  $(this).find('textarea[name=message]').val();
          var to = $(this).find('input[name=to]').val();
          $('#eml').prop('disabled', true);
          $('#subj').prop('disabled', true);
          $('#msg').prop('disabled', true);
          $('#emlsub').prop('disabled', true);
     $.ajax({
            type: 'post',
            url: mainurl+'/admin/order/email',
            data: {
                '_token': token,
                'subject'   : subject,
                'message'  : message,
                'to'   : to
                  },
            success: function( data) {
          $('#eml').prop('disabled', false);
          $('#subj').prop('disabled', false);
          $('#msg').prop('disabled', false);
          $('#subj').val('');
          $('#msg').val('');
        $('#emlsub').prop('disabled', false);
        if(data == 0)
        toastr.error('Oops Something Goes Wrong !!');
        else
        toastr.success('Email Sent !!');
        $('.close').click();
            }

        });
          return false;
        });
// SEND EMAIL SECTION ENDS



$(document).on('click','.godropdown .go-dropdown-toggle', function(){
  $('.godropdown .action-list').hide();
  var $this = $(this);
  $this.next('.action-list').toggle();
});


$(document).on('click', function(e)
{
    var container = $(".godropdown");

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0)
    {
      container.find('.action-list').hide();
    }
});



// **************************************  AJAX REQUESTS SECTION ENDS *****************************************


})(jQuery);
