<?php
/**
 * Template part for displaying product slider section
 *
 * @package Carpenter Workshop
 * @subpackage carpenter_workshop
 */

?>

<?php $carpenter_workshop_static_image = get_stylesheet_directory_uri() . '/assets/images/slider-img.png'; ?>

<?php if (get_theme_mod('carpenter_workshop_slider_arrows', true) != '') { ?>
    <section id="slider">
        <div class="owl-carousel">
            <?php
            $carpenter_workshop_selected_category = get_theme_mod('carpenter_workshop_main_product_slider_category_setting', '');
            if (!empty($carpenter_workshop_selected_category)) :
                $args = array(
                    'post_type'      => 'product',
                    'posts_per_page' => -1,
                    'tax_query'      => array(
                        array(
                            'taxonomy' => 'product_cat',
                            'field'    => 'slug',
                            'terms'    => $carpenter_workshop_selected_category,
                        ),
                    ),
                    'orderby'        => 'title',
                    'order'          => 'ASC'
                );
                $query = new WP_Query($args);
                if ($query->have_posts()) :
                    while ($query->have_posts()) : $query->the_post();?>
                    <div class="slide-box">
                        <div class="inner_content">
                            <div class="container">
                                <div class="row">
                                    <div class="col-lg-7 col-md-7 col-sm-7 px-5 align-self-center">
                                      <?php if (get_theme_mod('carpenter_workshop_slider_short_heading') != '') { ?>
                                        <p class="slider-top"><?php echo esc_html(get_theme_mod('carpenter_workshop_slider_short_heading', '')); ?></p>
                                        <?php } ?>
                                        <?php if (get_theme_mod('carpenter_workshop_show_slider_title', true)) : ?>
                                            <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                                        <?php endif; ?>
                                        <?php if (get_theme_mod('carpenter_workshop_show_slider_content', true)) : ?>
                                            <p><?php $carpenter_workshop_excerpt = get_the_excerpt(); echo esc_html( carpenter_workshop_string_limit_words( $carpenter_workshop_excerpt, esc_attr(get_theme_mod('carpenter_workshop_slider_excerpt_length','25')))); ?></p>
                                        <?php endif; ?>
                                        <div class="more-btn">
                                            <a class="btn-1" href="<?php echo esc_url(get_permalink()); ?>" target="_blank"><?php esc_html_e('Add To Cart', 'carpenter-workshop'); ?><i class="fas fa-shopping-cart px-2"></i></a>
                                        </div>  
                                    </div>
                                    <div class="col-lg-5 col-md-5 col-sm-5 align-self-center">
                                        <div class="img-box">
                                            <?php if (has_post_thumbnail()) { ?>
                                                <img src="<?php the_post_thumbnail_url('full'); ?>"/> <?php } else {
                                                echo ('<img src="' . esc_url($carpenter_workshop_static_image) . '">');
                                            } ?>
                                            <?php carpenter_workshop_ts_show_sale_percentage(); ?>
                                            <?php global $product; ?>                                            
                                              <?php 
                                                $carpenter_workshop_price = $product->get_price();
                                                if (!empty($carpenter_workshop_price)) { ?>
                                                    <div class="slider-price mb-0">
                                                        <?php
                                                            // Ensure $carpenter_workshop_price is a float
                                                            $carpenter_workshop_price = floatval($carpenter_workshop_price);

                                                            // Check if $carpenter_workshop_price is a valid float
                                                            if (is_float($carpenter_workshop_price)) {
                                                                // Format the price
                                                                $carpenter_workshop_formatted_price = number_format($carpenter_workshop_price, 0, '', '');
                                                                echo $carpenter_workshop_formatted_price . ' $ '; 
                                                                esc_html_e('only', 'carpenter-workshop');
                                                            } else {
                                                                // Handle the case when $carpenter_workshop_price is not a valid float
                                                                echo 'Price not available';
                                                            } ?>
                                                     </div>
                                                <?php }
                                                ?>
                                        </div> 
                                    </div> 
                                </div> 
                            </div>    
                        </div>
                    </div>
                <?php endwhile;
                    wp_reset_postdata();
                else : ?>
                    <div class="no-postfound"></div>
              <?php
                  endif;
              endif;
              ?>
        </div>
        <div class="clearfix"></div>
    </section>
<?php } ?>
