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

get_header(); ?>

<main id="skip-content">
<?php if (get_theme_mod('house_roofing_slider_section_setting', true) != '') { ?>
  <section id="top-slider">
    <div class="container p-0 slider-mainn">
      <?php $house_roofing_slide_pages = array();
        for ( $house_roofing_count = 1; $house_roofing_count <= 3; $house_roofing_count++ ) {
          $house_roofing_mod = intval( get_theme_mod( 'house_roofing_top_slider_page' . $house_roofing_count ));
          if ( 'page-none-selected' != $house_roofing_mod ) {
            $house_roofing_slide_pages[] = $house_roofing_mod;
          }
        }
        if( !empty($house_roofing_slide_pages) ) :
          $house_roofing_args = array(
            'post_type' => 'page',
            'post__in' => $house_roofing_slide_pages,
            'orderby' => 'post__in'
          );
          $house_roofing_query = new WP_Query( $house_roofing_args );
          if ( $house_roofing_query->have_posts() ) :
            $i = 1;
      ?>
      <div class="owl-carousel" role="listbox">
        <?php  while ( $house_roofing_query->have_posts() ) : $house_roofing_query->the_post(); ?>
          <div class="slide-box" data-count="<?php echo esc_attr( $i ); ?>">
    
            <div class="slider-image">
              <?php if ( has_post_thumbnail() ) {
                the_post_thumbnail();
              } else { ?>
                <img 
                  class="main-image-default" 
                  src="<?php echo esc_url( get_template_directory_uri() . '/assets/img/slider1.png' ); ?>" 
                  alt="<?php echo esc_attr__( 'Default Slider Image', 'house-roofing' ); ?>" 
                />
              <?php } ?>
              <div class="overlay"></div>
            </div>

            <div class="slider-inner-box">
              
              <?php 
              $short_heading = get_theme_mod( 'house_roofing_slider_short_heading' );
              if ( ! empty( $short_heading ) ) { ?>
                <h5 class="main-heading">
                  <?php echo esc_html( $short_heading ); ?>
                </h5>
              <?php } ?>

              <h3 class="m-0 pb-3">
                <?php the_title(); // already escaped by WP ?>
              </h3>

              <p class="post-excerpt">
                <?php echo esc_html( wp_trim_words( get_the_content(), 30, '...' ) ); ?>
              </p>

              <div class="slide-btn mt-4">
                <a href="<?php echo esc_url( get_theme_mod( 'house_roofing_slider_button_url' ) ); ?>">
                  <?php echo esc_html( get_theme_mod( 'house_roofing_slider_button_text' ) ); ?>
                  <i class="fas fa-arrow-right ms-2" aria-hidden="true"></i>
                </a>
              </div>

            </div>

            <div class="count-box">
              <h6>
                <span id="active-count">
                  <?php echo esc_html( str_pad( $i, 2, '0', STR_PAD_LEFT ) ); ?>
                </span>
                /<?php echo esc_html( '03' ); ?>
              </h6>
            </div>

          </div>
        <?php $i++; endwhile;
        wp_reset_postdata();?>
      </div>
      <?php else : ?>
        <div class="no-postfound"></div>
      <?php endif;
      endif;?>
      
    </div>
  </section>
<?php }?>

<?php if (get_theme_mod('house_roofing_activities_section_setting', true) != '') { ?>
  <section class="featured py-5">
    <div class="container ">
      <div class="heading text-center mb-4">
        <?php if(get_theme_mod('house_roofing_services_heading') != ''){ ?>
          <h3 class="main-heading"></i><?php echo esc_html(get_theme_mod('house_roofing_services_heading')); ?></h3>
        <?php }?>
        <?php if(get_theme_mod('house_roofing_services_content') != ''){ ?>
          <p class="main-content"><?php echo esc_html(get_theme_mod('house_roofing_services_content')); ?></p>
        <?php }?>
      </div>
      <div class="owl-carousel m-0 ser-box">
        <?php
          $house_roofing_services_cat = get_theme_mod('house_roofing_services_sec_category','');
          if($house_roofing_services_cat){
            $house_roofing_page_query5 = new WP_Query(array( 'category_name' => esc_html($house_roofing_services_cat,'house-roofing'),'posts_per_page' => 6));
            $i=1;
            while( $house_roofing_page_query5->have_posts() ) : $house_roofing_page_query5->the_post(); ?>
              <div class="service-box">
                <div class="feature-box m-0">
                  <div class="ser-content">
                    <div class="service-icon">
                      <?php if(has_post_thumbnail()){
                        the_post_thumbnail();
                        } else{?>
                        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/img/services1.png" alt="" />
                      <?php } ?>
        
                      <p class="post-categories">
                        <?php
                          $categories = get_the_category();
                          if ( ! empty( $categories ) ) {

                            // Get only the first category
                            $cat_name = $categories[0]->name;

                            // Limit to 12 characters (including spaces)
                            if ( mb_strlen($cat_name) > 13 ) {
                              echo esc_html( mb_substr($cat_name, 0, 13) . '..' );
                            } else {
                              echo esc_html( $cat_name );
                            }
                          }
                        ?>
                      </p>
                    </div>
                    <div class="services-content">
                      <h4 class="m-0">
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                      </h4>
                      <!-- Post Content / Excerpt -->
                      <p class="post-excerpt">
                        <?php echo esc_html( wp_trim_words( get_the_content(), 5, '...' ) ); ?>
                      </p>
                    </div>
                  </div>
                </div>
              </div>
            <?php $i++; endwhile;
          wp_reset_postdata();
        } ?>
      </div>
    </div>
  </section>
<?php }?>
  <section id="page-content">
    <div class="container">
      <div class="py-3">
        <?php
          if ( have_posts() ) :
            while ( have_posts() ) : the_post();
              the_content();
            endwhile;
          endif;
        ?>
      </div>
    </div>
  </section>
</main>

<?php get_footer(); ?>