jQuery(document).ready(function($) {
  $("#external_user").click(function() {
   if($('#external_user').prop('checked')) {
    $('.row-api_source').show();
    $("#corsair_apisource").prop('required',true);
    $("#default_email_id").prop('required',true);
    
    
  } else {
    $('.row-api_source').hide();
    $("#corsair_apisource").prop('required',false);
    $("#default_email_id").prop('required',false);
    
  }
});
  
  $("#local_custom_user").click(function() {
   if($('#local_custom_user').prop('checked')) {

    $('.row-table_source').show();
    $("#local_custom_user_table").prop('required',true);
    $("#local_custom_user_field").prop('required',true);
    $("#local_custom_email_id").prop('required',true);
  } else {

    $('.row-table_source').hide();
    $("#local_custom_user_table").prop('required',false);
    $("#local_custom_user_field").prop('required',false);
    $("#local_custom_email_id").prop('required',false);
  }
});

$("#custom_login_field_label_toggle").click(function() {
   if($('#custom_login_field_label_toggle').prop('checked')){
    $('.row-table_customlogfield').show();
  }else{
    $('.row-table_customlogfield').hide();
  }
});
$("#corsair_white_list_toggle").click(function() {
   if($('#corsair_white_list_toggle').prop('checked')){
    $('.row-table_white_list_section').show();
  }else{
    $('.row-table_white_list_section').hide();
  }
});
  

  $(document).on("change", ".url_source" , function() {
   var user_role =$('.user_role').val();
   var url_source =$('.url_source').val();
   if (user_role === "" && url_source !== "") {

     $('.user-roll-error').html('Please select user role');
     get_popup_html(url_source);
   }
   else if (url_source === "") {
    $('.url-source-error').html('Please select url source');
    return false;
  }
  else if (user_role === "" && url_source === "" ){
    $('.url-source-error').html('Please select url source');
    $('.user-roll-error').html('Please select user role');
    return false;
  }
  else{
   $('.url-source-error').html('');
   $('.user-roll-error').html('');
   get_popup_html(url_source);
 }
});

  $(document).on("change", ".user_role" , function() {
   var user_role =$('.user_role').val();
   var url_source =$('.url_source').val();
   if (user_role === "") {
    $('.user-roll-error').html('Please select user role');
    return false;
  }
  else if (url_source === "") {
    $('.url-source-error').html('Please select url source');
    return false;
  }
  else if (user_role === "" && url_source === "" ){
    $('.url-source-error').html('Please select url source');
    $('.user-roll-error').html('Please select user role');
    return false;
  }
  else{
   $('.url-source-error').html('');
   $('.user-roll-error').html('');
   return false;
 }
});

  $(document).on("click", ".redirection-model .exit" , function() {
    $('html').removeClass('redirection-model-active'); 
  });
// Saving redirection link in table 
$(document).on("click", ".btn-redirection" , function() {
  var redirection_link=$('.redirection_link').val();
  var redirection_source=$('.url_source').val();
  var user_role=$('.user_role').val();
  if (user_role === "") {
   $('.user-roll-error').html('Please select user role');
   return false;
 }
 else if (redirection_source === "") {
  $('.url-source-error').html('Please select url source');
  return false;
}
else if (redirection_link === "") {

  $('.redirection-link-error').html('Redirection link should not be blank'); 
  return false;
}
else if (user_role === "" && redirection_source === "" && redirection_link === ""){
  $('.url-source-error').html('Please select url source');
  $('.user-roll-error').html('Please select user role');
  $('.redirection-link-error').html('Redirection link should not be blank'); 
  return false;
}


else{
 $('.url-source-error').html('');
 $('.user-roll-error').html('');
 $('.redirection-link-error').html('');
 add_redirection_rule(user_role,redirection_source,redirection_link);
  // return false;
}
});
$(document).on("change", ".redirection_link" , function() {
 var redirection_link=$('.redirection_link').val();
 if (redirection_link === "") {
   $('.redirection-link-error').html('Redirection link should not be blank'); 
   return false;
 }
 else{
  $('.redirection-link-error').html(''); 
  return false;
}
});


$(document).on("change", ".redirection_source_update" , function() {
 var redirection_source=$('.redirection_source_update').val();
 if (redirection_source === "") {
  $('.redirection_source_update').addClass('redirection_link_error');
  $('.redirection_source_update-error').show(); 
  $('.redirection_source_update-error').html('Please select the source'); 
  return false;
}
else{
 $('.redirection_source_update').removeClass('redirection_link_error');
 $('.redirection_source_update-error').hide(); 
 $('.redirection_source_update-error').html(''); 
 get_link_html_by_source(redirection_source);
}
});

$(document).on("change", ".redirection_link_update" , function() {
 var redirection_link=$('.redirection_link_update').val();
 if (redirection_link === "") {
  $('.redirection_link_update').addClass('redirection_link_error');
  $('.redirection_link_update_error').show(); 
  $('.redirection_link_update_error').html('Redirection link should not be blank'); 
  return false;
}
else{
 $('.redirection_link_update').removeClass('redirection_link_error');
 $('.redirection_link_update_error').hide(); 
 $('.redirection_link_update_error').html(''); 
 return false;
}
});


// updating redirection link in table 
$(document).on("click", ".btn-redirection-update" , function() {
  var redirection_link=$('.redirection_link_update').val();
  var redirection_source=$('.redirection_source_update').val();
  var user_role=$('#user_role_redirect_update').val();
  var redirection_id=$('#redirection_id').val();
  if (redirection_link === "") {
    $('.redirection_link_update').addClass('redirection_link_error');
    $('.redirection_link_update_error').show(); 
    $('.redirection_link_update_error').html('Redirection link should not be blank'); 
    return false;
  }
  else if (redirection_source === "") {
    $('.redirection_source_update').addClass('redirection_link_error');
    $('.redirection_source_update-error').show(); 
    $('.redirection_source_update-error').html('Please select the source'); 
    return false;
  }
  else if (redirection_source === "" && redirection_link === "") {
    $('.redirection_link_update').addClass('redirection_link_error');
    $('.redirection_link_update_error').show(); 
    $('.redirection_link_update_error').html('Redirection link should not be blank'); 
    $('.redirection_source_update').addClass('redirection_link_error');
    $('.redirection_source_update-error').show(); 
    $('.redirection_source_update-error').html('Please select the source'); 

    return false;
  }
  else{
    $('.redirection_link_update').removeClass('redirection_link_error');
    $('.redirection_link_update_error').hide(); 
    $('.redirection_link_update_error').html(''); 
    $('.redirection_source_update').removeClass('redirection_link_error');
    $('.redirection_source_update-error').hide(); 
    $('.redirection_source_update-error').html('');
    uppdate_redirection_rule(redirection_source,redirection_link,redirection_id); 

  }
});

if( $('#whitelist_emails').length > 0 ){
  $("#whitelist_emails").select2({
      tags: true,
      tokenSeparators: [',', ' '],
  });
}

});


function get_popup_html(url_source){
  jQuery.ajax({
    url: object.admin_ajax_url,
    method:'post',
    data: {
      'action':'get_redirection_popup',
      'url_source':url_source
    },
    beforeSend: function(){
      jQuery('#lodingimages').css('visibility','visible'); 
    }, 

    success:function(data) {
      console.log(data);
      if(data.status==1){
        jQuery('.source-content').html(data.html);
        jQuery('#lodingimages').css('visibility','hidden'); 

      }

    },

    error: function(errorThrown){

      console.log(errorThrown);
    }

  });
}

function add_redirection_rule(user_role,url_source,link){
  jQuery.ajax({
    url: object.admin_ajax_url,
    method:'post',
    data: {
      'action':'add_redirection',
      'user_role':user_role,
      'url_source':url_source,
      'link':link
    },
    beforeSend: function(){
      jQuery('#lodingimages').css('visibility','visible'); 
    }, 

    success:function(data) {
      if(data.status==false && data.message=='aleady_exist'){
        jQuery('.redirection_message').show();
        jQuery('.redirection_notice').html('Redirection for the selected user role has been aleady addred.');
        jQuery('#lodingimages').css('visibility','hidden'); 
        jQuery('.redirection-rule').html(data.html);
        jQuery('.redirection_message').delay(5000).fadeOut('slow');
      }
      else if(data.status==false && data.message=='error'){
       jQuery('.redirection_message').show();
       jQuery('.redirection_notice').html('Something went wrong. Please try again later');
       jQuery('#lodingimages').css('visibility','hidden');
       jQuery('.redirection-rule').html(data.html); 
       jQuery('.redirection_message').delay(5000).fadeOut('slow');


     }
     else{
       jQuery('.redirection_message').show();
       jQuery('.redirection_notice').addClass('success');
       jQuery('.redirection_notice').html('Redirection rule added successfully');
       jQuery('#lodingimages').css('visibility','hidden'); 
       jQuery('.redirection-rule').html(data.html);
       jQuery('.redirection_message').delay(5000).fadeOut('slow');

     }

   },

   error: function(errorThrown){

    console.log(errorThrown);
  }

});
}

function update_redirecion(id){
  jQuery.ajax({
    url: object.admin_ajax_url,
    method:'post',
    data: {
      'action':'update_redirection_popup',
      'id':id,
    },
    beforeSend: function(){
     // jQuery('#lodingimages').css('visibility','visible'); 
   }, 

   success:function(data) {
    console.log(data);
    if(data.status==1){
      jQuery('.redirection-form').html(data.html);
        //jQuery('#lodingimages').css('visibility','hidden'); 
        jQuery('html').addClass('redirection-model-active');

      }

    },

    error: function(errorThrown){

      console.log(errorThrown);
    }

  });
}
function get_link_html_by_source(url_source){
 jQuery.ajax({
  url: object.admin_ajax_url,
  method:'post',
  data: {
    'action':'link_by_source',
    'url_source':url_source
  },
  beforeSend: function(){
   jQuery('#lodingimages_popup_update').css('visibility','visible'); 
 }, 

 success:function(data) {
   if(data.status==1){
    jQuery('#lodingimages_popup_update').css('visibility','hidden'); 
    jQuery('.redirection_section').html(data.html);
      //jQuery('html').removeClass('redirection-model-active'); 
    }

  },

  error: function(errorThrown){

    console.log(errorThrown);
  }

});
}

function uppdate_redirection_rule(url_source,link,id){
  jQuery.ajax({
    url: object.admin_ajax_url,
    method:'post',
    data: {
      'action':'uppdate_redirection_rules',
      'url_source':url_source,
      'link':link,
      'id':id
    },
    beforeSend: function(){
     jQuery('#lodingimages_popup_update').css('visibility','visible'); 
   }, 

   success:function(data) {
    if(data.status==true && data.message=='success'){
      jQuery('.redirection_message').show();
      jQuery('.redirection_notice').addClass('success');
      jQuery('.redirection_notice').html('Url has been updated successfully');
      jQuery('#lodingimages_popup_update').css('visibility','hidden'); 
      jQuery('.redirection-rule').html(data.html);
      jQuery('html').removeClass('redirection-model-active'); 
      jQuery('.redirection_message').delay(5000).fadeOut('slow');
    }
    else{
     jQuery('.redirection_notice').removeClass('success');
     jQuery('.redirection_notice').show();
     jQuery('.redirection_notice').html('Something went wrong, Please try again later');
     jQuery('#lodingimages_popup_update').css('visibility','hidden'); 
     jQuery('.redirection-rule').html(data.html);
     jQuery('html').removeClass('redirection-model-active'); 
     jQuery('.redirection_message').delay(5000).fadeOut('slow');
   }

 },

 error: function(errorThrown){

  console.log(errorThrown);
}

});
}

function delete_redirecion(id) {
  if( !confirm('Are you sure, you want to delete this redirectionn rule?')) {
    return false;
  }
  else{
    jQuery.ajax({
      url: object.admin_ajax_url,
      method:'post',
      data: {
        'action':'delete_redirection_rule',
        'id':id
      },
      beforeSend: function(){

      }, 

      success:function(data) {
        console.log(data);
        if(data.status==true && data.message=='success'){
          jQuery('.redirection-rule').html(data.html);
        }

      },

      error: function(errorThrown){

        console.log(errorThrown);
      }

    });
  }
}

function change_status(id,status){
 jQuery.ajax({
  url: object.admin_ajax_url,
  method:'post',
  data: {
    'action':'update_status_redirection',
    'status':status,
    'id':id
  },
  beforeSend: function(){

  }, 

  success:function(data) {
    if(data.status==true && data.message=='success'){
      jQuery('.redirection-rule').html(data.html);
    }

  },

  error: function(errorThrown){

    console.log(errorThrown);
  }

});
}