<?php
function get_shop_page_filter(){
  $items_per_page = 12;
  $paged = isset($_POST['data']['paged']) ? $_POST['data']['paged'] : 1;

  $args = array(
      'post_type'       =>  'product',
      'post_status'     =>  'publish',
      'posts_per_page'  =>  $items_per_page,
      'paged'           =>  $paged,
  );

  if ( $_POST['data'] ) {
      $post_data = $_POST['data'];
      $tax_query_array = array('relation' => 'AND');
      $meta_query_array = array('relation' => 'AND');

      if( isset( $post_data['product_category'] ) && count( $post_data['product_category'] ) ) {
          $category = $post_data['product_category'];
          $tax_query_array[] = array(
              'taxonomy'  =>  'product_cat',
              'field'     =>  'term_id',
              'terms'     =>  $category
          );
      }

      if( isset( $post_data['hw_brand'] ) && count( $post_data['hw_brand'] ) ) {
          $brands = $post_data['hw_brand'];
          $tax_query_array[] = array(
              'taxonomy'  =>  'brand',
              'field'     =>  'term_id',
              'terms'     =>  $brands
          );
      }

      if( isset( $post_data['values'] ) && count( $post_data['values'] ) ) {
          $meta_query_array[] = array(
              'key' => '_price',
              'value' => array($post_data['values'][0], $post_data['values'][1]),
              'compare' => 'BETWEEN',
              'type' => 'numeric'
          );
      }

      if (isset($post_data['stock_status'])) {
          $meta_query_array[] = array(
              'key' => '_stock_status',
              'value' => $post_data['stock_status'],
              'compare' => '='
          );
      }

      $args['tax_query'] = $tax_query_array;
      $args['meta_query'] = $meta_query_array;
  }

  $loop = new WP_Query($args);
  $shop_page_loop_html = '';

  ob_start();
  if ($loop->have_posts()):
      while ($loop->have_posts()):
          $loop->the_post();
          include(get_theme_file_path('hw-products.php'));
      endwhile;
  endif;
  $shop_page_loop_html = ob_get_clean();

  $base = $_POST['data']['base_url'];

  if (str_contains($base, '?')) {
      $base .= '&paged=999999999';
  } else {
      $base .= '?paged=999999999';
  }

  $base = str_replace(999999999, '%#%', $base);
  $current = 1;
  $total = $loop->max_num_pages;

  $pages = paginate_links(
      array(
          'base'      => $base,
          'format'    => '',
          'current'   => max(1, $paged),
          'total'     => $total,
          'prev_text' => '&larr;',
          'next_text' => '&rarr;',
          'type'      => 'list',
          'add_args'  => array(
              'product_category' => isset($post_data['product_category']) ? $post_data['product_category'] : '',
          ),
      )
  );

  $response_data = array(
      'html'          =>  $shop_page_loop_html,
      'pagination'    =>  $pages
  );

  wp_send_json($response_data);
  exit;
}

add_action('wp_ajax_get_shop_page_filter','get_shop_page_filter');
add_action('wp_ajax_nopriv_get_shop_page_filter','get_shop_page_filter');

?>