<?php
/**
 * Template part for displaying the about section
 *
 * @package Appliances Store
 * @subpackage appliances_store
 */

?>
<?php if ( get_theme_mod( 'appliances_store_project_show_hide', true)) : ?>
  <section id="Product-section" class="px-md-0 px-3">
      <div class="container">
          <div class="row">
              <div class="col-lg-4 col-md-4 slider-product">
                <?php if ( class_exists( 'WooCommerce' ) ) {
                    $appliances_store_shop_selected_product_id = get_theme_mod( 'appliances_store_shop_slider_product', '' );
                    if ( ! empty( $appliances_store_shop_selected_product_id ) ) {
                        $appliances_store_shop_args = array(
                            'post_type'      => 'product',
                            'p'              => $appliances_store_shop_selected_product_id,
                            'posts_per_page' => 1,
                            'order'          => 'ASC',
                        );
                        $appliances_store_shop_loop = new WP_Query( $appliances_store_shop_args );
                        while ( $appliances_store_shop_loop->have_posts() ) : $appliances_store_shop_loop->the_post();
                            global $product; ?>
                            <div class="product-box">
                                <div class="product-box-img position-relative">
                                    <?php if ( has_post_thumbnail( $appliances_store_shop_loop->post->ID ) ) {
                                        echo get_the_post_thumbnail( $appliances_store_shop_loop->post->ID, 'shop_catalog' );
                                    } else {
                                        echo '<img src="' . esc_url( wc_placeholder_img_src() ) . '" alt="' . esc_attr__( 'Placeholder', 'appliances-store' ) . '" />';
                                    } ?>
                                    <div class="product-box-content">
                                        <div class="product-rating align-self-center">
                                            <?php 
                                            woocommerce_template_loop_rating(); 
                                            $appliances_store_average_rating = $product->get_average_rating();
                                            if ( $appliances_store_average_rating > 0 ) : 
                                            ?>
                                                <span class="rating-count">
                                                    (<?php echo esc_html( $appliances_store_average_rating ); ?>)
                                                </span>
                                            <?php endif; ?>
                                        </div>
                                        <h3 class="my-1 text-start">
                                          <a href="<?php echo esc_url( get_permalink( $appliances_store_shop_loop->post->ID ) ); ?>">
                                              <?php the_title(); ?>
                                          </a>
                                        </h3>
                                        <div class="tab-products">
                                          <p class="my-0 product-price">
                                            <?php 
                                            echo '<span class="product-price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
                                            ?>
                                          </p>
                                          <div class="add-cart-btn">
                                            <?php woocommerce_template_loop_add_to_cart(); ?>
                                          </div>
                                        </div>
                                    </div>
                                </div>
                                <?php if ( $product->is_on_sale() ) : ?>
                                  <span class="sale-tag"><?php esc_html_e( 'Sale', 'appliances-store' ); ?></span>
                                <?php endif; ?>
                            </div>
                        <?php endwhile;
                        wp_reset_postdata();
                    }
                } ?>
              </div>
              <div class="col-lg-8 col-md-8">
                <?php
                    $appliances_store_woo_image = get_stylesheet_directory_uri() . '/assets/images/woo-placeholder.png';
                ?>
                <div id="projects-sec">
                    <div class="container">
                        <div class="title-head">
                            <div class="row">
                                <div class="col-lg-11 col-md-7 col-12 align-self-center p-0">
                                  <?php 
                                  $appliances_store_main_heading = get_theme_mod( 'appliances_store_projetcs_main_heading', '' );
                                  if ( $appliances_store_main_heading ) : ?>
                                    <h2 class="mb-3 mt-md-0 mt-3 text-md-start text-center"><?php echo esc_html( $appliances_store_main_heading ); ?></h2>
                                  <?php endif; ?>
                                </div>
                                <div class="col-lg-1 col-md-2 align-self-center">
                                </div>
                            </div>
                            <div class="main-tab-col p-md-0">
                                <?php 
                                $appliances_store_tab_text = get_theme_mod('appliances_store_projetcs_text1', '');
                                 $appliances_store_tabs_number = get_theme_mod('appliances_store_projetcs_number', 6);
                                if ($appliances_store_tab_text) : ?>
                                    <div class="owl-carousel tab-carousel"> <!-- Owl Carousel wrapper -->
                                        <?php
                                        for ($appliances_store_j = 1; $appliances_store_j <= $appliances_store_tabs_number; $appliances_store_j++) :
                                            $appliances_store_tab_text = get_theme_mod('appliances_store_projetcs_text' . $appliances_store_j, '');
                                            if ($appliances_store_tab_text) :
                                                $appliances_store_tab_id = sanitize_title($appliances_store_tab_text);
                                                $appliances_store_icon_class = get_theme_mod('appliances_store_services_icon' . $appliances_store_j, '');
                                        ?>
                                          <div class="tab-box"> <!-- Carousel Item -->
                                            <button class="tablinks <?php echo $appliances_store_j == 1 ? 'active' : '' ?>" onclick="appliances_store_projetcs_tab(event, '<?php echo esc_attr($appliances_store_tab_id); ?>')">
                                              <div class="icon-main">
                                                <i class="<?php echo esc_attr($appliances_store_icon_class); ?>"></i>
                                              </div>
                                              <?php echo esc_html($appliances_store_tab_text); ?>
                                            </button>
                                          </div>
                                        <?php endif; endfor; ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                            <div class="prod-box">
                              <?php for ( $appliances_store_j = 1; $appliances_store_j <= $appliances_store_tabs_number; $appliances_store_j++ ) :
                                $appliances_store_tab_text = get_theme_mod( 'appliances_store_projetcs_text' . $appliances_store_j, '' );
                                $appliances_store_tab_id = sanitize_title( $appliances_store_tab_text );
                              ?>
                                <div id="<?php echo esc_attr( $appliances_store_tab_id ); ?>" class="tabcontent mt-3" style="display: none;">
                                  <div class="owl-carousel owl-theme">
                                    <?php
                                    $appliances_store_category_slug = get_theme_mod( 'appliances_store_projetcs_category' . $appliances_store_j, '' );
                                    if ( $appliances_store_category_slug ) :
                                      $args = array(
                                        'post_type' => 'product',
                                        'posts_per_page' => 50,
                                        'tax_query' => array(
                                          array(
                                            'taxonomy' => 'product_cat',
                                            'field'    => 'slug',
                                            'terms'    => sanitize_text_field( $appliances_store_category_slug ),
                                          ),
                                        ),
                                      );
                                      $query = new WP_Query( $args );

                                      if ( $query->have_posts() ) :
                                        while ( $query->have_posts() ) : $query->the_post();
                                          $product = wc_get_product( get_the_ID() );

                                          if ( $product ) :
                                    ?>
                                      <div class="item mb-md-0 mb-3">
                                        <div class="box">
                                          <?php if ( has_post_thumbnail() ) : ?>
                                            <div class="product-image">
                                              <?php the_post_thumbnail( 'full', array( 'alt' => get_the_title() ) ); ?>

                                              <?php if ( $product->is_on_sale() ) : ?>
                                                <span class="sale-tag"><?php esc_html_e( 'Sale', 'appliances-store' ); ?></span>
                                              <?php endif; ?>
                                            </div>
                                          <?php else : ?>
                                            <img src="<?php echo esc_url( $appliances_store_woo_image ); ?>" alt="<?php esc_attr_e( 'Product Category Image','appliances-store' ); ?>">
                                          <?php endif; ?>

                                          <div class="main-product-content">
                                            <div class="product-rating align-self-center">
                                              <?php 
                                              woocommerce_template_loop_rating(); 
                                              $appliances_store_average_rating = $product->get_average_rating();
                                              if ( $appliances_store_average_rating > 0 ) : 
                                              ?>
                                                  <span class="rating-count">
                                                      (<?php echo esc_html( $appliances_store_average_rating ); ?>)
                                                  </span>
                                              <?php endif; ?>
                                            </div>
                                            <h3 class="my-0">
                                              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                            </h3>
                                            <div class="tab-products">
                                              <p class="my-0 product-price">
                                                <?php 
                                                echo '<span class="product-price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
                                                ?>
                                              </p>
                                              <div class="add-cart-btn">
                                                <?php woocommerce_template_loop_add_to_cart(); ?>
                                              </div>
                                            </div>
                                          </div>
                                        </div>
                                      </div>
                                    <?php
                                        endif; 
                                        endwhile; 
                                        wp_reset_postdata();
                                      endif;
                                    endif; ?>
                                  </div>
                                </div>
                              <?php endfor; ?>
                            </div>
                        </div>
                    </div>
                </div>
              </div>
          </div>
      </div>
  </section>
<?php endif; ?>
