<?php
/**
 * Template part for displaying Product Section
 *
 * @package Online Mobile Accessories
 */

$online_mobile_accessories_product_section = get_theme_mod('online_mobile_accessories_product_section_setting', false);
$online_mobile_accessories_product_section_title = get_theme_mod('online_mobile_accessories_product_section_title');
$online_mobile_accessories_product_section_desc = get_theme_mod('online_mobile_accessories_product_section_desc');
$online_mobile_accessories_category_name = get_theme_mod('online_mobile_accessories_product_category');
?>

<?php if ($online_mobile_accessories_product_section && class_exists('WooCommerce')) : ?>
    <div class="our-products wow zoomInUp py-5" data-wow-duration="2s">
        <div class="container">
            <div class="side-border text-center mb-5">
                <?php if ($online_mobile_accessories_product_section_title) : ?>
                  <h2 class="product-sec-title text-capitalize mb-0"><?php echo esc_html($online_mobile_accessories_product_section_title); ?></h2>
                <?php endif; ?>
                <?php if ($online_mobile_accessories_product_section_desc) : ?>
                  <p class="product-sec-desc"><?php echo esc_html($online_mobile_accessories_product_section_desc); ?></p>
                <?php endif; ?>
            </div>
            <?php if ($online_mobile_accessories_category_name && $online_mobile_accessories_category_name !== 'select') : ?>
                <div class="row">
                    <?php
                    $online_mobile_accessories_args = array(
                      'post_type' => 'product',
                      'posts_per_page' => 4,
                      'ignore_sticky_posts' => true,
                      'tax_query' => array(
                        'taxonomy' => 'product_cat',
                        'field'    => 'slug',
                        'terms'    => $online_mobile_accessories_category_name,
                      ),
                    );
                    $online_mobile_accessories_loop = new WP_Query($online_mobile_accessories_args);
                    if ($online_mobile_accessories_loop->have_posts()) :
                        while ($online_mobile_accessories_loop->have_posts()) : $online_mobile_accessories_loop->the_post();
                            global $product; 
                            if (!$product || !is_a($product, 'WC_Product')) {
                                $product = wc_get_product(get_the_ID());
                            }

                            $online_mobile_accessories_regular_price = (float) $product->get_regular_price();
                            $online_mobile_accessories_sale_price    = (float) $product->get_sale_price();
                            $online_mobile_accessories_discount_percentage = 0;

                            if ($online_mobile_accessories_regular_price > 0 && $online_mobile_accessories_sale_price > 0) {
                                $online_mobile_accessories_discount_percentage = round((($online_mobile_accessories_regular_price - $online_mobile_accessories_sale_price) / $online_mobile_accessories_regular_price) * 100);
                            }
                            ?>
                            <div class="col-xl-3 col-lg-3 col-md-6 col-12 box mb-5">
                                <div class="product-image-wrapper position-relative">
                                    <div class="product-img text-center p-3">
                                        <?php if (has_post_thumbnail()) :
                                            the_post_thumbnail('medium');
                                        else : ?>
                                            <img src="<?php echo esc_url(get_stylesheet_directory_uri() . '/images/default1.png'); ?>" alt="<?php esc_attr_e('Default', 'online-mobile-accessories'); ?>">
                                        <?php endif; ?>
                                    </div>
                                    <?php if ($online_mobile_accessories_discount_percentage > 0) : ?>
                                        <span class="discount-tag position-absolute">-<?php echo esc_html($online_mobile_accessories_discount_percentage); ?>%</span>
                                    <?php endif; ?>
                                    <div class="product-icons position-absolute">
                                        <?php if (shortcode_exists('woosw')) { ?>
                                            <div class="wishlist-main mb-1">
                                                <?php echo do_shortcode('[woosw]'); ?>
                                            </div>
                                        <?php } ?>
                                        <div class="addcart position-relative">
                                            <?php woocommerce_template_loop_add_to_cart(); ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="product-details mt-4">
                                    <h3 class="product-title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h3>
                                    <div class="price">
                                        <?php echo wp_kses_post($product->get_price_html()); ?>
                                    </div>
                                </div>
                            </div>
                        <?php endwhile;
                    else : ?>
                        <p class="no-products"><?php esc_html_e('No products found.', 'online-mobile-accessories'); ?></p>
                    <?php endif;
                    wp_reset_postdata(); ?>
                </div>
            <?php else : ?>
                <p class="no-products text-center mt-3">
                    <?php esc_html_e('Please select a category in the Customizer.', 'online-mobile-accessories'); ?>
                </p>
            <?php endif; ?>
        </div>
    </div>
<?php endif; ?>