jQuery(document).ready(function($) {
    // $('form.cart').on('click', 'form.cart .plus, form.cart .minus, .quantity .plus, .quantity .minus', function() {
    //     console.log("button clicked");
    //     var qty = $(this).closest('.quantity').find('.qty');
    //     var currentVal = parseFloat(qty.val());
    //     var max = parseFloat(qty.attr('max'));
    //     var min = parseFloat(qty.attr('min'));
    //     var step = parseFloat(qty.attr('step'));

    //     if ($(this).is('.plus')) {
    //         if (currentVal < max || isNaN(max)) {
    //             qty.val((currentVal + step).toFixed(0));
    //             qty.trigger('change');
    //         }
    //     } else {
    //         if (currentVal > min || isNaN(min)) {
    //             qty.val((currentVal - step).toFixed(0));
    //             qty.trigger('change');
    //         }
    //     }
    // });

    var Interval = null;
    function show_loading_box() {
      // console.log("loading");
      jQuery(".spinner-loading-box").css("display", "none");
      clearInterval(Interval);
    }
    jQuery('document').ready(function () {
      Interval = setInterval(show_loading_box, 1000);
    });


//==================== show category dropdown start =====================

    var show_category = $(".show_category");
    var header_top_cat = $(".header-top-cat .elementor-image-box-title");
    var category_wrapper = $(".category-wrapper");
    show_category.click(
      function(evt){
        evt.stopPropagation();
          category_wrapper.toggleClass("show");
      }
  )

  //  Close the popup when clicking outside of the dropdown
   jQuery('body').on("click", function(event) {
    jQuery('.category-wrapper').removeClass('show');
});





//==================== show category dropdown end =====================

//==================== Home Page Product Category Slider Start =====================

$('.product-cat-slider>.elementor-widget-container>.wl-layout-horizontal').slick({
    infinite: true,
    slidesToShow: 9,
    slidesToScroll: 1,
    responsive: [
      {
        breakpoint: 1400,
        settings: {
          slidesToShow: 7,
        }
      },
      {
        breakpoint: 1200,
        settings: {
          slidesToShow: 6,
        }
      },
      {
        breakpoint: 1024,
        settings: {
          slidesToShow: 5,
        }
      },
      {
        breakpoint: 768,
        settings: {
          slidesToShow: 3,
        }
      },
      {
        breakpoint: 576,
        settings: {
          slidesToShow: 2,
        }
      },
      {
        breakpoint: 400,
        settings: {
          slidesToShow: 1,
        }
      }
    ]
  });

//==================== Home Page Product Category Slider End =====================

// ================== Best Supplies Section Of Home Page Start =================

var best_supplies_tab_title = $(".best_supplies_tab_title .elementor-widget-wrap");
var product_cat = $(".best-supplies-section .product-cat");

best_supplies_tab_title.append(product_cat);

// ================== Best Supplies Section Of Home Page End =================

//==================== Home Page handyman Slider Start =====================

$('.handyman-slider .handyman-list').slick({
    infinite: true,
    slidesToShow: 5,
    slidesToScroll: 1,
    responsive: [
      {
        breakpoint: 1200,
        settings: {
          slidesToShow: 4,
        }
      },
      {
        breakpoint: 1024,
        settings: {
          slidesToShow: 3,
        }
      },
      {
        breakpoint: 768,
        settings: {
          slidesToShow: 2,
        }
      },
      {
        breakpoint: 576,
        settings: {
          slidesToShow: 1,
        }
      }
    ]
  });

//==================== Home Page handyman Slider End =====================

// ================== Our Best Sponcer Section Of Home Page Start =================

var img_swiper_button_prev = $(".best_sponcer_img_slider .elementor-swiper-button-prev");
var img_swiper_button_next = $(".best_sponcer_img_slider .elementor-swiper-button-next");
var best_sponcer_next = $("#best_sponcer_next");
var best_sponcer_prev = $("#best_sponcer_prev");

best_sponcer_next.click(() => img_swiper_button_next.click());
best_sponcer_prev.click(() => img_swiper_button_prev.click());

// ================== Our Best Sponcer Section Of Home Page End =================

// ================== Best Supplies Section Of Home Page Start =================

var product_categories_tab_title = $(".product_categories_tab_title .elementor-widget-wrap");
var product_categories_product_cat = $(".product_categories_tab_section .product-cat");

product_categories_tab_title.append(product_categories_product_cat);

// ================== Best Supplies Section Of Home Page End =================

// ================== Our Blog Of Home Page Start =================

var blog_swiper_button_prev = $(".home-blog-slider .ea-arrows-container .ea-navigation-prev i");
var blog_swiper_button_next = $(".home-blog-slider .ea-arrows-container .ea-navigation-next i");
var letest_blog_next = $("#letest_blog_next");
var letest_blog_prev = $("#letest_blog_prev");


letest_blog_next.click(() => blog_swiper_button_prev.click());
letest_blog_prev.click(() => blog_swiper_button_next.click());

// ================== Our Blog Of Home Page Start =================

// ================== Our handyman Slider Of About Us Page Start =================

var about_page_slider_button_prev = $(".handyman-slider-about .slick-next.slick-arrow");
var about_page_slider_button_next = $(".handyman-slider-about .slick-prev.slick-arrow");
var about_next = $("#about_next");
var about_prev = $("#about_prev");


about_next.click(() => about_page_slider_button_prev.click());
about_prev.click(() => about_page_slider_button_next.click());

// ================== Our handyman Slider Of About Us Page End =================

// ================== Related Product Section Of Single Product Page Start =================

var related_product_button_prev = $(".woocommerce div.product section.related.products .product-items-wrapper .slick-prev");
var related_product_button_next = $(".woocommerce div.product section.related.products .product-items-wrapper .slick-next");
var arrow_nav_right = $("#arrow_nav_right");
var arrow_nav_left = $("#arrow_nav_left");
$('.woocommerce div.product section.related.products .product-items-wrapper').slick({
    infinite: true,
    slidesToShow: 5,
    slidesToScroll: 1,
    prevArrow: arrow_nav_left,
    nextArrow: arrow_nav_right,
    responsive: [
      {
        breakpoint: 1200,
        settings: {
          slidesToShow: 4,
        }
      },
      {
        breakpoint: 1024,
        settings: {
          slidesToShow: 3,
        }
      },
      {
        breakpoint: 768,
        settings: {
          slidesToShow: 2,
        }
      },
      {
        breakpoint: 576,
        settings: {
          slidesToShow: 1,
        }
      }
    ]

  });



// ================== Related Product Section Of Single Product Page End =================

const get_woocommerce_currency_symbol = wp_customscripts_object.get_woocommerce_currency_symbol;

jQuery( "#product-price-slider" ).slider({
  range: true,
  min: 0,
  max: parseInt(wp_customscripts_object.product_max_price),
  values: [0, parseInt(wp_customscripts_object.product_max_price)],
  change: function( event, ui ) {
    car_repair_pro_filters();
  },
  slide: function( event, ui ) {
    jQuery( "#product-amount-start" ).text(
      get_woocommerce_currency_symbol + ui.values[ 0 ]
    );
    jQuery( "#product-amount-end" ).text(
      get_woocommerce_currency_symbol + ui.values[ 1 ]
    );
  }
});




jQuery('.car-repair-filter [type="checkbox"]').on('change', function (event) {
  car_repair_pro_filters();
});


  // Update pagination links click handler
  jQuery(document).on('click', '.woocommerce-pagination a.page-numbers', function(event) {
    event.preventDefault(); // Prevent default link behavior
    var page = jQuery(this).text(); // Get the page number from the clicked pagination link
    car_repair_pro_filters(page); // Trigger the AJAX request with the page number
  });


function car_repair_pro_filters(page) {
  var data_obj = {};

  data_obj['values'] = jQuery('#product-price-slider').slider( "values" );

  jQuery('.car-repair-filter [type="checkbox"]:checked').each(function (index, element) {
    if (!Array.isArray(data_obj[jQuery(element).attr('name')])) {
      data_obj[jQuery(element).attr('name')] = new Array();
    }
    data_obj[jQuery(element).attr('name')].push(jQuery(element).val())
  });

//i ad

if (jQuery('#in-stock').is(':checked')) {
data_obj['stock_status'] = 'instock';
} else if (jQuery('#out-of-stock').is(':checked')) {
data_obj['stock_status'] = 'outofstock';
}

//end



   // Add the page number to the data object if provided
   if (page) {
    data_obj['paged'] = page;
}


  setQueryParams(data_obj)

  data_obj.base_url = window.location.href

  jQuery.post(wp_customscripts_object.ajaxurl, {
    'action': 'get_shop_page_filter',
    'data': data_obj
  },
    function (response) {
      // console.log(response);
      jQuery('.hw-shop .product-items-wrapper').html(response.html)
     

      // console.log('response.pagination', response.pagination);
      jQuery('.woocommerce-pagination').html(response.pagination); // Update pagination
    });

}


function setQueryParams(data_obj) {
  // console.log('data_obj', data_obj)
  let url = new URL(window.location.href)
  let params = new URLSearchParams(url.search)

  const data_obj_keys = Object.keys(data_obj)

  // console.log('data_obj_keys', data_obj_keys)

  for (let index = 0; index < data_obj_keys.length; index++) {
    const element = data_obj_keys[index]

    // console.log('element', element)

    if (
      (element != 'base_url')
    ) {
      params.set(element, data_obj[element])
    }
  }

  // console.log('params.toString()', params.toString())

  const querySting = params.toString()

  window.history.replaceState(null, null, `?${querySting}`)

  // console.log('url', url)

  

}




});




document.addEventListener("DOMContentLoaded", function() {
  var imageLink = document.getElementById("imageLink");
  var popup = document.getElementById("popup");
  var popupImage = document.getElementById("popupImage");
  var closeButton = document.querySelector(".close");

  // Check if the image link exists before adding the event listener
  if (imageLink) {
      imageLink.addEventListener("click", function(event) {
          event.preventDefault();

          // Get the href attribute of the clicked link
          var imageUrl = this.getAttribute("href");

          // Set the image source
          popupImage.src = imageUrl;

          // Show the popup
          popup.style.display = "block";
      });
  }

  // Check if the close button exists before adding the event listener
  if (closeButton) {
      closeButton.addEventListener("click", function() {
          popup.style.display = "none";
      });
  }

  // Close the popup when clicking outside of the image
  window.addEventListener("click", function(event) {
      if (event.target == popup) {
          popup.style.display = "none";
      }
  });
});
