<?php
/**
 * Template Name: Custom Home Page
 */

get_header(); 

?>

<main role="main" id="skip_content">
  <?php do_action( 'affiliate_marketing_business_above_slider' ); ?>
  
  <?php 
    $affiliate_marketing_business_number = get_theme_mod('affiliate_marketing_business_slide_number');
    if($affiliate_marketing_business_number != ''){
  ?>
    <div class="slider-section py-4">
      <div class="container">
        <div class="row">
          <div class="<?php if (get_theme_mod('affiliate_marketing_business_banner_text') != '' || get_theme_mod('affiliate_marketing_business_banner_button_url') != '' || get_theme_mod('affiliate_marketing_business_banner_button_text') != '' || get_theme_mod('affiliate_marketing_business_banner_image') != '') { ?> col-xl-9 col-lg-8 col-md-8 col-12 ps-lg-0<?php } else {?> col-lg-12 col-md-12<?php }?>">
            <?php if( get_theme_mod('affiliate_marketing_business_slider_hide', true) != '' || get_theme_mod( 'affiliate_marketing_business_display_slider',true) != ''){ ?>
              <section id="slider" class="mw-100 m-auto p-0">
                <div id="carouselExampleIndicators" class="carousel slide carousel-fade" data-bs-ride="carousel" data-type="multi" data-interval="false">
                  <div class="carousel-inner" role="listbox">
                    <?php for ($affiliate_marketing_business_i=1; $affiliate_marketing_business_i<=$affiliate_marketing_business_number; $affiliate_marketing_business_i++) {?>
                      <div <?php if($affiliate_marketing_business_i == 1){echo 'class="carousel-item active"';} else{ echo 'class="carousel-item"';}?>>
                        <div class="row">
                          <div class="col-xl-6 col-lg-6 col-md-7 col-6 inner_carousel-col">
                            <div class="carousel-caption">
                              <div class="inner_carousel wow zoomIn data-wow-delay=".25s>
                                <?php if(get_theme_mod('affiliate_marketing_business_slider_small_text'.$affiliate_marketing_business_i) != ''){ ?>
                                  <p class="small-title mb-3"><?php echo esc_html(get_theme_mod('affiliate_marketing_business_slider_small_text'.$affiliate_marketing_business_i)); ?></p>
                                <?php }?>
                                <?php if(get_theme_mod('affiliate_marketing_business_slider_title'.$affiliate_marketing_business_i) != ''){ ?>
                                  <h1><?php echo esc_html(get_theme_mod('affiliate_marketing_business_slider_title'.$affiliate_marketing_business_i)); ?></h1>
                                <?php }?>
                                <?php if(get_theme_mod('affiliate_marketing_business_slider_text'.$affiliate_marketing_business_i) != ''){ ?>
                                  <p class="my-3">
                                    <?php
                                      $affiliate_marketing_business_slider_text = get_theme_mod('affiliate_marketing_business_slider_text' . $affiliate_marketing_business_i); 
                                      $affiliate_marketing_business_word_limit = 12; // Change this to your desired word limit

                                      // Trim the text to the specified number of words
                                      $affiliate_marketing_business_slider_excerpt_text = wp_trim_words($affiliate_marketing_business_slider_text, $affiliate_marketing_business_word_limit, '...');

                                      echo '<p class="my-3">' . esc_html($affiliate_marketing_business_slider_excerpt_text) . '</p>';
                                    ?>
                                  </p>
                                <?php }?>
                                <?php if( get_theme_mod('affiliate_marketing_business_slider_button_text'.$affiliate_marketing_business_i) != '' || get_theme_mod('affiliate_marketing_business_slider_btn_icon'.$affiliate_marketing_business_i) != '' ){ ?>
                                  <div class="read-more my-md-4 text-start">
                                    <a href="<?php echo esc_html(get_theme_mod('affiliate_marketing_business_slider_button_link'.$affiliate_marketing_business_i)); ?>"><i class="<?php echo esc_attr(get_theme_mod('affiliate_marketing_business_slider_btn_icon'.$affiliate_marketing_business_i)); ?>"></i><?php echo esc_html( get_theme_mod('affiliate_marketing_business_slider_button_text'.$affiliate_marketing_business_i)); ?><span class="screen-reader-text"><?php echo esc_html( get_theme_mod('affiliate_marketing_business_slider_button_text'.$affiliate_marketing_business_i)); ?></span></a>
                                  </div>
                                <?php } ?>
                              </div>
                            </div>
                          </div>
                          <div class="col-xl-6 col-lg-6 col-md-5 col-6 slider-carousel text-center">
                            <?php if ( get_theme_mod('affiliate_marketing_business_side_img'.$affiliate_marketing_business_i) != "" ) {?>
                              <img class="slider-carousel-img wow swing" src="<?php echo esc_url(get_theme_mod('affiliate_marketing_business_side_img'.$affiliate_marketing_business_i)); ?>" alt="<?php echo esc_attr(get_theme_mod('affiliate_marketing_business_slide_title'.$affiliate_marketing_business_i, true)); ?>" title="#slidecaption<?php echo esc_html($affiliate_marketing_business_i); ?>">
                            <?php } ?>
                          </div>
                        </div>
                      </div>
                    <?php } ?>
                  </div>
                  <?php if( get_theme_mod( 'affiliate_marketing_business_display_slider_icons', true) == true ) { ?>
                    <a class="carousel-control-prev" data-bs-target="#carouselExampleIndicators" data-bs-slide="prev" role="button">
                      <span class="carousel-control-prev-icon w-auto h-auto" aria-hidden="true"><i class="<?php echo esc_attr(get_theme_mod('affiliate_marketing_business_slider_previous_icon','fa-solid fa-left-long')); ?>" ></i></span>
                      <span class="screen-reader-text"><?php esc_html_e( 'Previous','affiliate-marketing-business' );?></span>
                    </a>
                    <a class="carousel-control-next" data-bs-target="#carouselExampleIndicators" data-bs-slide="next" role="button">
                      <span class="carousel-control-next-icon w-auto h-auto" aria-hidden="true"><i class="<?php echo esc_attr(get_theme_mod('affiliate_marketing_business_slider_next_icon','fa-solid fa-right-long')); ?>" ></i></span>
                      <span class="screen-reader-text"><?php esc_html_e( 'Next','affiliate-marketing-business' );?></span>
                    </a>  
                  <?php }?>
                </div> 
                <div class="clearfix"></div>
              </section>
            <?php }?>  
          </div>
          <?php if (get_theme_mod('affiliate_marketing_business_banner_image') != '') { ?>
            <div class="col-xl-3 col-lg-4 col-md-4 col-12">
              <div class="banner-section wow fadeInDown">
                <div class="banner-content">
                  <?php if(get_theme_mod('affiliate_marketing_business_banner_small_text') != ''){ ?>
                    <p class="mb-3"><?php echo esc_html(get_theme_mod('affiliate_marketing_business_banner_small_text')); ?></p>
                  <?php }?>
                  <?php if(get_theme_mod('affiliate_marketing_business_banner_text') != ''){ ?>
                    <h2><?php echo esc_html(get_theme_mod('affiliate_marketing_business_banner_text')); ?></h2>
                  <?php }?>
                  <?php if(get_theme_mod('affiliate_marketing_business_banner_price') != ''){ ?>
                    <p class="price mb-3"><?php echo esc_html(get_theme_mod('affiliate_marketing_business_banner_price')); ?></p>
                  <?php }?>
                  <?php if(get_theme_mod('affiliate_marketing_business_banner_button_url') != ''){ ?>
                    <a href="<?php echo esc_url(get_theme_mod('affiliate_marketing_business_banner_button_url')); ?>" class="banner-btn"><i class="<?php echo esc_attr(get_theme_mod('affiliate_marketing_business_banner_btn_icon')); ?>"></i><?php echo esc_html(get_theme_mod('affiliate_marketing_business_banner_button_text')); ?></a>
                  <?php }?>
                </div>
                <?php if(get_theme_mod('affiliate_marketing_business_banner_image') != ''){ ?>
                  <img src="<?php echo esc_url(get_theme_mod('affiliate_marketing_business_banner_image')); ?>" alt="<?php echo esc_attr('Coupon Image', 'affiliate-marketing-business'); ?>">
                <?php }?>
              </div>
            </div>
          <?php }?>
        </div>
      </div>
    </div>
  <?php }?>
  <?php do_action( 'affiliate_marketing_business_below_slider' ); ?>

<!-- Product Section -->
  <?php if( get_theme_mod( 'affiliate_marketing_business_our_services') != '' || get_theme_mod( 'affiliate_marketing_business_section_text') != '' || get_theme_mod( 'affiliate_marketing_business_section_title') != '') { ?>
    <section id="product-sec" class="py-5">
      <div class="container wow fadeInDown">
        <div class="heading-box text-center mb-4">
          <?php if( get_theme_mod( 'affiliate_marketing_business_section_title') != '') { ?>
            <h3 class="py-3"><?php echo esc_html(get_theme_mod('affiliate_marketing_business_section_title',''));?></h3>  
          <?php } ?>
        </div>
        <div class="row">
          <?php if ( class_exists( 'WooCommerce' ) && get_theme_mod('affiliate_marketing_business_product_page') != '' ) {?>
            <?php $args = array(
              'post_type' => 'product',
              'product_cat' => get_theme_mod('affiliate_marketing_business_product_page'),
              'order' => 'ASC',
              'hide_empty' => 0,
              'posts_per_page' => 3,
            );
            $loop = new WP_Query( $args );
              while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
              <div class="col-xl-4 col-lg-4 col-md-12 col-12 pt-0 pb-4 px-3 product-main-div">
                <div class="main-product-section mx-md-0 mx-lg-0 mx-2 p-4">
                  <div class="row">
                    <div class="col-xl-5 col-lg-12 col-md-5 col-12 text-center">
                      <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.esc_url(woocommerce_placeholder_img_src()).'" />'; ?>
                    </div>
                    <div class="col-xl-7 col-lg-12 col-md-7 col-12 text-lg-start text-md-start text-center">
                      <div class="product-content">
                        <h4><a href="<?php echo esc_url(get_permalink( $loop->post->ID )); ?>"><?php the_title(); ?></a></h4>
                        <p class="product-text">
                          <?php 
                            $affiliate_marketing_business_product_description = $product->get_description(); 
                            $affiliate_marketing_business_excerpt_length = 100; // Number of characters for the excerpt
                            $affiliate_marketing_business_product_description_excerpt = substr($affiliate_marketing_business_product_description, 0, $affiliate_marketing_business_excerpt_length);
                            echo $affiliate_marketing_business_product_description_excerpt . '...'; // Add ellipsis to indicate the text is truncated
                          ?>
                        </p>
                        <?php if( $product->is_type( 'simple' ) || $product->is_type( 'external' ) ){ woocommerce_template_loop_rating( $loop->post, $product ); } ?>

                        <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?> py-3"><?php echo $product->get_price_html(); ?></p>
                        <?php
                          $affiliate_marketing_business_sale_start_date = get_post_meta($product->get_id(), '_sale_price_dates_from', true);
                          $affiliate_marketing_business_sale_end_date = get_post_meta($product->get_id(), '_sale_price_dates_to', true);
                        ?>
                        <div class="countdown-timer" 
                             data-sale-start="<?php echo esc_attr($affiliate_marketing_business_sale_start_date); ?>" 
                             data-sale-end="<?php echo esc_attr($affiliate_marketing_business_sale_end_date); ?>">
                          <span id="countdown"></span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            <?php endwhile; wp_reset_query(); ?>
            <?php } ?>
        </div>
      </div>
    </section>
  <?php }?>

  <div class="container front-page-content">
    <?php while ( have_posts() ) : the_post(); ?>
      <div class="new-text"><?php the_content(); ?></div>
    <?php endwhile; // end of the loop. ?>
  </div>
</main>

<?php get_footer(); ?>