<?php
/*
* Template part for the custom filters
*
*/
$page_term_id = '';
if ( isset( get_queried_object()->term_id ) ) {
  $page_term_id = get_queried_object()->term_id;
}

// print_r($page_term_id);
// exit;

$max_price = 0;
global $wpdb;
$results = $wpdb->get_results( "SELECT MAX(cast(meta_value as unsigned)) AS max_price FROM `{$wpdb->prefix}postmeta` WHERE `meta_key` = '_price'" );
if ( count( $results ) && isset( $results[0] ) && isset( $results[0]->max_price ) ) {
  $max_price = $results[0]->max_price;
}
if ($max_price) {
  $max_price = +$max_price;
}

?>

<div class="accordion shop-page-filters" id="accordionExample1">
  <div class="accordion-item sidebar-product-category">
    <h2 class="accordion-header" id="headingOne1">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne1" aria-expanded="true" aria-controls="collapseOne1">
        <?php echo esc_html('Categories', 'hardware-dealer-pro'); ?>
      </button>
    </h2>
    <div id="collapseOne1" class="accordion-collapse collapse show car-repair-filter" aria-labelledby="headingOne1" data-bs-parent="#accordionExample1">
      <div class="accordion-body">
        <?php $terms = get_terms( array(
          'post_type' => 'product',
          'taxonomy' => 'product_cat',
          'hide_empty' => true,
          'parent'     => 0,
          'order' => 'DES',
        ) );


        $cat_arr = [];
        $checked_cat = '';
        if ( isset( $_GET['product_category'] ) && $_GET['product_category'] ) {
          $checked_cat = $_GET['product_category'];
        }

        if ($checked_cat) {
          $cat_arr = explode(',', $checked_cat);
        }
        

        ?>
        <div class="accordion" id="accordionExample2">
        <ul class="shop-page-produts-category parent_category">
          <?php
          // echo "<pre>";
          // print_r($terms);
          foreach ($terms as $parent_category_key =>  $parent_category) {
            //remove uncategorized from loop
            if( $parent_category->slug == 'uncategorized' ){
              continue;
            }
            echo '<div class="accordion-item">';
            echo '<li class="parent-category">';
            ?>
            <h4 class="accordion-header" id="heading-<?php echo $parent_category->name ?>">
             <button class="accordion-button <?php echo ($parent_category_key > 1) ? 'collapsed' : ''; ?>
" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-<?php echo $parent_category_key ?>" aria-expanded="true" aria-controls="collapse-<?php echo $parent_category_key ?>">
                <?php echo $parent_category->name ?>
            </button>
            </h4>
            <?php
            // echo '<h4>' . $parent_category->name . '</h4>';
            // Get child categories
            $child_categories = get_terms( array(
              'taxonomy'   => 'product_cat',
              'parent'     => $parent_category->term_id,
              'hide_empty' => false,
          ));

          if ( ! empty( $child_categories ) && ! is_wp_error( $child_categories ) ) {
            echo '<div id="collapse-' . $parent_category_key . '" class="accordion-collapse collapse ' . ($parent_category_key === 1 ? 'show' : '') . '" aria-labelledby="heading-' . $parent_category_key . '" data-bs-parent="#accordionExample2">';
           echo '<div class="accordion-body">';
            echo '<ul class="child-categories">';
            foreach ( $child_categories as $term_key => $term_value ) {
                ?>
                 <li class="child-category">
                    <label>
                      <input class="category-filter-checkbox" type="checkbox" name="product_category" value="<?php echo $term_value->term_id; ?>" <?php if (in_array($term_value->term_id, $cat_arr)) { echo "checked"; } ?> />
                      <span><?php echo $term_value->name; ?></span>
                    </label>
                </li>
                <?php
            }
            echo '</ul>';
            echo '</div>';
            echo '</div>';
        }
            ?>
            </li>
      </div>
          <?php } ?>
        </ul>
      </div>
      </div>
    </div>
  </div>
  <div class="accordion-item sidebar-product-price">
    <h2 class="accordion-header" id="headingTwo1">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo1" aria-expanded="false" aria-controls="collapseTwo1">
        <?php echo esc_html('Price', 'hardware-dealer-pro'); ?>
      </button>
    </h2>
    <div id="collapseTwo1" class="accordion-collapse collapse car-repair-filter" aria-labelledby="headingTwo1" data-bs-parent="#accordionExample1">
      <div class="accordion-body">
        <div id="product-price-slider"></div>
        <label for="#product-amount" class="product-price-range">
          <span id="product-amount-start">$0</span>
          <span id="product-amount-end">$<?php echo $max_price; ?></span>
        </label>
      </div>
    </div>
  </div>

  <div class="accordion-item sidebar-product-brand">
    <h2 class="accordion-header" id="headingFive1">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseFive1" aria-expanded="false" aria-controls="collapseFive1">
        <?php echo esc_html('Brands', 'hardware-dealer-pro'); ?>
      </button>
    </h2>
    <div id="collapseFive1" class="accordion-collapse collapse car-repair-filter" aria-labelledby="headingFive1" data-bs-parent="#accordionExample1">
      <div class="accordion-body">
      <ul class="shop-page-produts-brand">
        <?php
          $term_brand = get_terms( array(
            'post_type' => 'product',
            'taxonomy' => 'brand',
            'hide_empty' => false
          ) );

          foreach ($term_brand as $term_brand_key => $term_brand_value) {
            ?>
            <li>
              <label>
                <input type="checkbox" name="hw_brand" value="<?php echo $term_brand_value->term_id; ?>" />
                <span><?php echo $term_brand_value->name; ?></span>
              </label>
            </li>
            <?php
          }
        ?>
        </ul>
      </div>
    </div>
  </div>


  <div class="accordion-item sidebar-product-stock">
    <h2 class="accordion-header" id="headingStock">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseStock" aria-expanded="false" aria-controls="collapseStock">
        <?php echo esc_html('Availability', 'hardware-dealer-pro'); ?>
      </button>
    </h2>
    <div id="collapseStock" class="accordion-collapse collapse car-repair-filter" aria-labelledby="headingStock" data-bs-parent="#accordionExample1">
      <div class="accordion-body">
      <ul class="shop-page-produts-stock">
        <li>
            <label>
                <!-- <input type="checkbox" name="in_stock" id="in_stock" value="" /> -->
                <input type="checkbox" id="in-stock" name="stock_status" value="instock" />
                <span>In Stock</span>
            </label>
        </li>
        <li>
            <label>
                <!-- <input type="checkbox" name="out_of_stock" id="out_of_stock" value="" /> -->
                <input type="checkbox" id="out-of-stock" name="stock_status" value="outofstock" />
                <span>Out of Stock</span>
            </label>
        </li>
      </ul>
      </div>
    </div>
  </div>
</div>
