jQuery(function($){
	// Start Display Notes using jquery
	var message = `
        <div class="iflair_admin_notes">
            <p><strong>Note:</strong> Please select the figure according to your theme settings (Appearance -> Customize -> WooCommerce -> Product Catalog: "Products per row"). Ensure the values for "On page load" & "On load more" are multiples of the selected figure. For example, if you choose 3, set values like 3, 6, 9, 12, 15, and so on.</p>
        </div>
    `;    
    jQuery(message).insertAfter("#number_products_4_display");
    // End Display Notes using jquery

	window.ifqpf_editors = {};
	var ifqpf_default_settings = ifqpf_admin_obj.ifqpf_default_settings;
	var select2_object = ifqpf_admin_obj.select2_object;
	var select2_objects = ifqpf_admin_obj.select2_objects;
	var select2_placeholder = ifqpf_admin_obj.select2_placeholder;
	var select2_objects_keys = ( !ifqpf_is_empty(select2_objects) ) ? Object.keys(select2_objects) : [];
	var default_sorting_option = ifqpf_admin_obj.default_sorting_option;
	var editor_error_messages = ifqpf_admin_obj.editor_error_messages;

	$('.ifqpf-select2').each(function(key, val){

		var select_id = $(this).attr('id');
		var settings = ( select2_objects_keys.length > 0 && select2_objects.hasOwnProperty(select_id) ) ? select2_objects[select_id] : select2_object;
		if( settings.length < 1 ){
			settings = {};
		}

		$(this).select2(settings);

	});

	/* Show setting popup */
	$(".ifqpf-table-modal-open").click(function(e){

		e.preventDefault();
		var id = $(this).data('id');
		$('#ifqpf_popup_'+id).addClass('ifqpf-popup-show');		

	});

	/* Hide setting popup */
	$(".ifqpf-popup-close").click(function(e){

		e.preventDefault();
		var popup = $(this).closest('.ifqpf-popup-main');
		popup.removeClass('ifqpf-popup-show');

	});

	/* Close popup on escape click */	
	document.addEventListener('keydown', function(event) {
    const key = event.key;
    	if (key === "Escape") {
        	var popup1 = jQuery('.ifqpf-popup-main');
			popup1.removeClass('ifqpf-popup-show');
    	}
	});

	// terms include and exclude selection code
	jQuery('.select2-selection--multiple').click(function(){
		var selfclass = jQuery(this).closest('tr').next().find('.ifqpf-include-terms option:selected');
		var include_products = [];
		  jQuery(selfclass).each(function() {
		   include_products.push(this.text);
		});
		
    	var selfclass2 = jQuery(this).closest('tr').prev().find('.ifqpf-exclude-terms option:selected');
		var exclude_products = [];
		jQuery(selfclass2).each(function() {
		    exclude_products.push(this.text);        
		});
    	
		var exclude_terms=[]; var include_terms=[];
        var exclude_clas1s = jQuery(this).parent().parent().prev();       
        var all_exclude_terms = [];
        if (jQuery(exclude_clas1s).hasClass('ifqpf-exclude-terms')){
      		jQuery(exclude_clas1s).find('option').each(function() {
        		all_exclude_terms.push(this.text);
       		});
        }
        var include_clas2s = jQuery(this).parent().parent().prev();
        var all_include_terms = [];
        if (jQuery(include_clas2s).hasClass('ifqpf-include-terms')){       
	        jQuery(include_clas2s).find('option').each(function() {
	        	all_include_terms.push(this.text);       
	    	});
        }
		
        jQuery.grep(include_products, function(el) {
		   if (jQuery.inArray(el, all_exclude_terms) != -1) {		 
				jQuery(exclude_clas1s).find('option').filter(function(){
				   return jQuery.trim(jQuery(this).text()) ==  el
				}).hide();
			}
		});

		jQuery.grep(exclude_products, function(el) {
	   		if (jQuery.inArray(el, all_include_terms) != -1) {	 
				jQuery(include_clas2s).find('option').filter(function(){
				   return jQuery.trim(jQuery(this).text()) ==  el
				}).hide();
	   		}
		});

		var select2_results__options = jQuery('.select2-results__options');
		var results_li_products = [];
		jQuery(select2_results__options).find("li").each(function()
	    {
	       results_li_products.push(jQuery(this).text());         
	    }); 
	    
		jQuery.grep(include_products, function(el) {
	   		if (jQuery.inArray(el, results_li_products) != -1) {	 
				jQuery(select2_results__options).find('li').filter(function(){
				   return jQuery.trim(jQuery(this).text()) ==  el
				}).hide();
	   		}
		});

		jQuery.grep(exclude_products, function(el) {
	   		if (jQuery.inArray(el, results_li_products) != -1) {	 
				jQuery(select2_results__options).find('li').filter(function(){
				   return jQuery.trim(jQuery(this).text()) ==  el
				}).hide();
	   		}
		});		

    });

	// Remove terms in include and exclude x click
    jQuery('.select2-selection__choice__remove').click(function(){
    	var removed_val = jQuery(this).next('.select2-selection__choice__display').text();
		var select2_results__options = jQuery('.select2-results__options');
		var results_li_products = [];
		jQuery(select2_results__options).find("li").each(function()
	    {
	       results_li_products.push(jQuery(this).text());         
	    }); 
	    
		jQuery.grep(removed_val, function(el) {
	   		if (jQuery.inArray(el, results_li_products) != -1) {	 
				jQuery(select2_results__options).find('li').filter(function(){
				   return jQuery.trim(jQuery(this).text()) ==  el
				}).show();
	   		}
		});
    });

	/* Hide show taxonomy heading text */
	if( $('.ifqpf-display-heading').length > 0 ){

		$('.ifqpf-display-heading').each(function(index, value){

			var parent = $(this).closest('.ifqpf-popup-content');
			var toggle = parent.find('.ifqpf-field-toggle-check');

			if( !$(this).is(":checked") ){
				parent.find(".ifqpf-heading-text").closest('tr').hide();
				parent.find(".ifqpf-field-toggle-check").closest('tr').hide();
				parent.find(".ifqpf-field-defalut-toggle").closest('tr').hide();
			}else{
				parent.find(".ifqpf-heading-text").closest('tr').show();
				parent.find(".ifqpf-field-toggle-check").closest('tr').show();
				if( toggle.is(":checked") ){
					parent.find(".ifqpf-field-defalut-toggle").closest('tr').show();
				}
			}

		});

	}

	$('.ifqpf-display-heading').change(function(){

		var parent = $(this).closest('.ifqpf-popup-content');
		var toggle = parent.find('.ifqpf-field-toggle-check');

		if( !$(this).is(":checked") ){
			parent.find(".ifqpf-heading-text").closest('tr').hide();
			parent.find(".ifqpf-field-toggle-check").closest('tr').hide();
			parent.find(".ifqpf-field-defalut-toggle").closest('tr').hide();
		}else{
			parent.find(".ifqpf-heading-text").closest('tr').show();
			parent.find(".ifqpf-field-toggle-check").closest('tr').show();
			if( toggle.is(":checked") ){
				parent.find(".ifqpf-field-defalut-toggle").closest('tr').show();
			}
		}

	});

	/* Hide show all heading text */
	if( $('.ifqpf-display-heading-check').length > 0 ){

		$('.ifqpf-display-heading-check').each(function(index, value){

			var text_clss = $(this).data('text-class');

			if( !$(this).is(":checked") ){
				$("."+text_clss).closest('tr').hide();
			}else{
				$("."+text_clss).closest('tr').show();
			}

		});

	}

	$('.ifqpf-display-heading-check').change(function(){

		var text_clss = $(this).data('text-class');

		if( !$(this).is(":checked") ){
			$("."+text_clss).closest('tr').hide();
		}else{
			$("."+text_clss).closest('tr').show();
		}

	});

	/* Enable/disable search in title checkbox */
	$('.ifqpf-check-search-display').change(function(){

		if( $(this).is(":checked") ){
			$('.ifqpf-check-search-in-title').prop("checked", true);
		}else{
			$('.ifqpf-check-search-in-title').prop("checked", false);
		}

	});

	/* Hide show taxonomy dropdown placeholder */
	$('.selection-type').each(function(key, val){

		var parent = $(this).closest('.ifqpf-popup-content');

		if( $(this).val() == 'dropdown' ){
			parent.find(".ifqpf-dropdown-placeholder").closest('tr').show();
		}else{
			parent.find(".ifqpf-dropdown-placeholder").closest('tr').hide();
		}

	});

	/* Hide show dropdown first option text field */
	$('.selection-type').change(function(){

		var parent = $(this).closest('.ifqpf-popup-content');

		if( $(this).val() == 'dropdown' ){
			parent.find(".ifqpf-dropdown-placeholder").closest('tr').show();
		}else{
			parent.find(".ifqpf-dropdown-placeholder").closest('tr').hide();
		}

	});

	/* Initialize sortable fields */
	$( ".ifqpf-sorting-field-parent" ).sortable({
	  cursor: "move",
	  handle: ".sorting-handle",
	});

	/* Reset sorting of sortable fields */
	$(".ifqpf-reset-sorting").click(function(){

		var parent = $(this).closest('.ifqpf-popup-content');
		var li_html = '';
		var li_length = parent.find('li.ifqpf-multiple-field-child').length;

		if( li_length > 0 ){

			for (var i = 0; i <= li_length; i++) {
				
				var li_el = parent.find('li.ifqpf-multiple-field-child [data-default-sorting="'+i+'"]');
				if( li_el.length > 0 ){
					li_html = li_el.closest('li.ifqpf-multiple-field-child').clone();
				}

				if ( !ifqpf_is_empty(li_html) ){
					li_el.closest('li.ifqpf-multiple-field-child').remove();
					parent.find('ul.ifqpf-sorting-field-parent').append(li_html);
				}

			}

		}

	});

	/* Set or unset value on default sorting opiton */
	$(document).on('change','.ifqpf-enable-sorting',function(){
		
		var enabled_sorting = {};
		var first_key = Object.keys(default_sorting_option)[0];
		var first_val = default_sorting_option[first_key];

		$('.ifqpf-default-sorting').html('');

		$('.ifqpf-enable-sorting').each(function(key, val){

			if( $(this).is(":checked") ){

				$('.ifqpf-default-sorting').prop('disabled', false);
				var s_key = $(this).data('sorting-key');
				var s_label = $(this).data('sorting-label');
				
				$('.ifqpf-default-sorting').append($("<option></option>").attr("value", s_key).text(s_label)); 

				enabled_sorting[s_key] = s_label;
			}

		});

		if( Object.keys(enabled_sorting).length < 1 ){

			$('.ifqpf-default-sorting').append($("<option></option>").attr("value", first_key).text(first_val));
			$('.ifqpf-default-sorting').prop('disabled', true);

		}

		$('.ifqpf-default-sorting option[value="'+first_key+'"]').prop('selected', true);

	});

	/* Initialize sortable fields */
	$( ".ifqpf-sortable-table" ).sortable({
		cursor: "move",
		handle: ".sorting-handle",
		items: "tbody > tr",
		stop: function( event, ui ) {
			var item = ui.item;
			if( item.prev().length > 0 && !item.prev().hasClass("ifqpf-row-sortable") ){
				$(this).sortable( "cancel" );
			}
		}
	});

	/* disable hidden input on checked or unchecked checkbox  */
	$('.ifqpf-checkbox-has-hidden').change(function(){

		var hidden_input = $(this).next();

		if( $(this).is(':checked') ){
			hidden_input.prop('disabled', true);
		}else{
			hidden_input.prop('disabled', false);
		}

	});

	/* Reset all popup fields */
	$(".ifqpf-popup-reset").click(function(){

		var popup = $(this).closest('.ifqpf-popup-main');
		var popup_id = popup.attr('id');
		var fields = $('#'+popup_id+' :input');
	
		ifqpf_reset_fields(fields);

		if( popup.find('.ifqpf-sorting-fields-wrap').length > 0 ){
			popup.find('.ifqpf-sorting-fields-wrap').each(function(index, value){
				ifqpf_reset_sorting($(this));
			});
		}

	});

	/* Reset filds */
	function ifqpf_reset_fields(fields){

		$.each(fields, function(index, value){

			var $this = $(this);
			var input_type = $(this).attr('type');
			var default_val = ifqpf_check_for_default($this);

			if( ifqpf_is_empty(input_type) ){
				if( $(this).is('select') ){
					input_type = 'select';
				}
			}

			if( input_type === 'checkbox' || input_type === 'radio' ){
				if( !ifqpf_is_empty(default_val) ){
					$(this).prop('checked', true);
				}else{
					$(this).prop('checked', false);
					$(this).removeAttr('checked');
				}
				$(this).trigger('change');
			}else if( input_type === 'select' ){
				var multiple = $(this).attr('multiple');

				if( !ifqpf_is_empty(default_val) ){
					$(this).val(default_val);
				}else{
					if ( !ifqpf_is_empty(multiple) ){
						$(this).val('');
					}else{
						$(this).prop("selectedIndex", 0);
					}
				}

				if( $(this).hasClass('ifqpf-select2') ){
					$(this).trigger('change');
				}
			}else{
				if( !ifqpf_is_empty(default_val) ){
					$(this).val(default_val);
				}else{
					$(this).val('');
				}
			}

		});

	}

	function ifqpf_check_for_default(el){

		var default_val = '';
		var data_name = el.data('name');

		if( !ifqpf_is_empty(data_name) ){
			data_name = data_name.split('=>');

			if( data_name.length > 0 ){
				for (var i = 0; i < data_name.length; i++) {
					if( ifqpf_is_empty(default_val) && ifqpf_default_settings.hasOwnProperty(data_name[i]) ){
						default_val = ifqpf_default_settings[data_name[i]];
					}else if( !ifqpf_is_empty(default_val) && default_val.hasOwnProperty(data_name[i]) ){
						default_val = default_val[data_name[i]];
					}
				}
			}
		}

		return default_val;

	}

	/* Reset sorting */
	function ifqpf_reset_sorting(sorting_el){

		var parent = sorting_el;
		var li_html = '';
		var li_length = parent.find('li.ifqpf-multiple-field-child').length;

		if( li_length > 0 ){

			for (var i = 0; i <= li_length; i++) {
				
				var li_el = parent.find('li.ifqpf-multiple-field-child [data-default-sorting="'+i+'"]');
				if( li_el.length > 0 ){
					li_html = li_el.closest('li.ifqpf-multiple-field-child').clone();
				}

				if ( !ifqpf_is_empty(li_html) ){
					li_el.closest('li.ifqpf-multiple-field-child').remove();
					parent.find('ul.ifqpf-sorting-field-parent').append(li_html);
					parent.sortable({
					  cursor: "move",
					  handle: ".sorting-handle",
					});
				}

			}

		}

	}

	/* tab js */
	jQuery(".ifqpf-tab a").click(function(e){
		e.preventDefault();
	    jQuery(".ifqpf-tab").removeClass("ifqpf-tab-active-li");
	    jQuery(this).closest('.ifqpf-tab').addClass("ifqpf-tab-active-li");
	    var tagid = jQuery(this).data("tag");
	    jQuery("#ifqpf_currnet_tab").val(tagid);
	    jQuery(".ifqpf-tab-content").removeClass("ifqpf-tab-active-content").addClass("ifqpf-tab-content-hide");
	    jQuery("#" + tagid).addClass("ifqpf-tab-active-content").removeClass("ifqpf-tab-content-hide");
	});


	/* Hide show all toggle checkbox */
	if( $('.ifqpf-toggle-check').length > 0 ){

		$('.ifqpf-toggle-check').each(function(index, value){

			var popup = $(this).closest('.ifqpf-popup-content');
			var display_order = popup.find('.ifqpf-display-order').val();
			var selection_type = popup.find('.selection-type').val();

			if( display_order == 'hierarchical' && selection_type !== 'dropdown' ){
				$(this).closest('tr').show();
			}else{
				$(this).closest('tr').hide();
			}

		});

	}

	$('.ifqpf-display-order, .selection-type').change(function(){

		var popup = $(this).closest('.ifqpf-popup-content');
		var display_order = popup.find('.ifqpf-display-order').val();
		var selection_type = popup.find('.selection-type').val();

		if( display_order == 'hierarchical' && selection_type !== 'dropdown' ){
			popup.find('.ifqpf-toggle-check').closest('tr').show();
		}else{
			popup.find('.ifqpf-toggle-check').closest('tr').hide();
		}

	});


	/* Hide show taxonomy default toggle select */
	if( $('.ifqpf-field-toggle-check').length > 0 ){

		$('.ifqpf-field-toggle-check').each(function(index, value){

			var parent = $(this).closest('.ifqpf-popup-content');
			var heading = parent.find('.ifqpf-display-heading');

			if( $(this).is(":checked") && heading.is(":checked") ){
				parent.find(".ifqpf-field-defalut-toggle").closest('tr').show();
			}else{
				parent.find(".ifqpf-field-defalut-toggle").closest('tr').hide();
			}

		});

	}

	$('.ifqpf-field-toggle-check').change(function(){

		var parent = $(this).closest('.ifqpf-popup-content');
		var heading = parent.find('.ifqpf-display-heading');

		if( $(this).is(":checked") && heading.is(":checked") ){
			parent.find(".ifqpf-field-defalut-toggle").closest('tr').show();
		}else{
			parent.find(".ifqpf-field-defalut-toggle").closest('tr').hide();
		}

	});

	/* Hide show all heading text */
	if( $('.ifqpf-display-button-check').length > 0 ){

		$('.ifqpf-display-button-check').each(function(index, value){

			var text_id = $(this).data('btn-text-field');

			if( !$(this).is(":checked") ){
				$("#"+text_id).closest('tr').hide();
			}else{
				$("#"+text_id).closest('tr').show();
			}

		});

	}

	$('.ifqpf-display-button-check').change(function(){

		var text_id = $(this).data('btn-text-field');

		if( !$(this).is(":checked") ){
			$("#"+text_id).closest('tr').hide();
		}else{
			$("#"+text_id).closest('tr').show();
		}

	});

});

/* Check if give value is empty or not */
function ifqpf_is_empty(value){

	var is_empty = false;

	if( value === null || value === '' || typeof value === 'undefined' ){
	    is_empty  = true;
	}

	return is_empty;
}


// jQuery(document).ready(function() {

// 	jQuery('#search_min_input').on('input', function() {
//         var value = jQuery(this).val();
//         if (value < 1) {
//             jQuery(this).val(1);
//         }
//     });

// 	jQuery('#number_of_columns_selection_4_shop').parent().prev().addClass('shop_column_th');
// 	jQuery('#enable_filter_on_shop_page').parent().prev().addClass('enable_filter_th');

// 	jQuery('<span class="default_theme_msg">- Please select columns as per your theme settings. (Appearance -> Customize -> Woocomerce -> Product Catalog : "Products per row")</span>').insertAfter("#number_of_columns_selection_4_shop");
// 	jQuery( "span.default_theme_msg" ).wrap( "<div class='iflair_admin_notes'>Notes :</div>" );
// 	jQuery('<span class="column_msg"></span>').insertAfter("span.default_theme_msg");
// 	var col_1_val = '- Please set figure at below "On page load" & "On load more", multiple of value what we choose here. (For ex if you choose 1 than at both below field choose such as 1, 2, 3, 4, 5... and so on';
// 	var col_2_val = '- Please set figure at below "On page load" & "On load more", multiple of value what we choose here. (For ex if you choose 2 than at both below field choose such as 2, 4, 6, 8, 10... and so on';
// 	var col_3_val = '- Please set figure at below "On page load" & "On load more", multiple of value what we choose here. (For ex if you choose 3 than at both below field choose such as 3, 6, 9, 12, 15... and so on';
// 	var col_4_val = '- Please set figure at below "On page load" & "On load more", multiple of value what we choose here. (For ex if you choose 4 than at both below field choose such as 4, 8, 12, 16, 20... and so on';
// 	var col_5_val = '- Please set figure at below "On page load" & "On load more", multiple of value what we choose here. (For ex if you choose 5 than at both below field choose such as 5, 10, 15, 20, 25... and so on';
// 	var col_6_val = '- Please set figure at below "On page load" & "On load more", multiple of value what we choose here. (For ex if you choose 6 than at both below field choose such as 6, 12, 18, 24, 30... and so on';
  	
// 	var number_of_columns_selection_4_shop = jQuery("#number_of_columns_selection_4_shop").val();

// 	if(number_of_columns_selection_4_shop == 1){
// 		jQuery('.column_msg').text(col_1_val);
// 	}
// 	if(number_of_columns_selection_4_shop == 2){
// 		jQuery('.column_msg').text(col_2_val);
// 	} 
// 	if(number_of_columns_selection_4_shop == 3){
// 		jQuery('.column_msg').text(col_3_val);
// 	} 
// 	if(number_of_columns_selection_4_shop == 4){
// 		jQuery('.column_msg').text(col_4_val);
// 	} 
// 	if(number_of_columns_selection_4_shop == 5){
// 		jQuery('.column_msg').text(col_5_val);
// 	} 
// 	if(number_of_columns_selection_4_shop == 6){
// 		jQuery('.column_msg').text(col_6_val);
// 	} 
	
//   	jQuery('#number_of_columns_selection_4_shop').change(function() {
  	
// 	var number_of_columns_selection_4_shop_new = jQuery("#number_of_columns_selection_4_shop").val();

//   	if(number_of_columns_selection_4_shop_new == 1){
// 		jQuery('.column_msg').text(col_1_val);
// 		jQuery('#number_products_on_load').val(4);
// 		jQuery('#number_products_on_load_more').val(2);
// 	}
// 	if(number_of_columns_selection_4_shop_new == 2){
// 		jQuery('.column_msg').text(col_2_val);
// 		jQuery('#number_products_on_load').val(8);
// 		jQuery('#number_products_on_load_more').val(4);
// 	} 
// 	if(number_of_columns_selection_4_shop_new == 3){
// 		jQuery('.column_msg').text(col_3_val);
// 		jQuery('#number_products_on_load').val(12);
// 		jQuery('#number_products_on_load_more').val(6);
// 	} 
// 	if(number_of_columns_selection_4_shop_new == 4){
// 		jQuery('.column_msg').text(col_4_val);
// 		jQuery('#number_products_on_load').val(16);
// 		jQuery('#number_products_on_load_more').val(8);
// 	} 
// 	if(number_of_columns_selection_4_shop_new == 5){
// 		jQuery('.column_msg').text(col_5_val);
// 		jQuery('#number_products_on_load').val(20);
// 		jQuery('#number_products_on_load_more').val(10);
// 	} 
// 	if(number_of_columns_selection_4_shop_new == 6){
// 		jQuery('.column_msg').text(col_6_val);
// 		jQuery('#number_products_on_load').val(24);
// 		jQuery('#number_products_on_load_more').val(12);
// 	} 
    
//   });
  
// });