<?php
/**
 * Template part for displaying about section
 *
 * @package DigitalX Agencies
 * @subpackage digitalx_agencies
 */

$digitalx_agencies_servicesec = get_theme_mod('digitalx_agencies_service_show_hide', true);

if ($digitalx_agencies_servicesec == '1') : 
?>
<section id="service-section">
  <div class="container"> 
    <div class="row mb-2">
      <div class="col-lg-6 col-md-6">
        <div class="serv-head mb-3">
          <?php if (get_theme_mod('digitalx_agencies_serv_top_title')) { ?>
            <p class="serv-top-title mb-0"><?php echo esc_html(get_theme_mod('digitalx_agencies_serv_top_title')); ?></p>
          <?php } ?>
          <?php if (get_theme_mod('digitalx_agencies_section_title')) : 
              $digitalx_agencies_title = get_theme_mod('digitalx_agencies_section_title');
              $digitalx_agencies_words = explode(' ', trim($digitalx_agencies_title));
              $digitalx_agencies_last_word = array_pop($digitalx_agencies_words);
              $digitalx_agencies_title_without_last = implode(' ', $digitalx_agencies_words);
          ?>
              <h2 class="text-capitalize mt-2 mb-0" id="section-title">
                  <?php if (!empty($digitalx_agencies_title_without_last)) : ?>
                      <?php echo esc_html($digitalx_agencies_title_without_last); ?> 
                  <?php endif; ?>
                  <span class="highlight-last"><?php echo esc_html($digitalx_agencies_last_word); ?></span>
              </h2>
          <?php endif; ?>
        </div>
      </div>
      <div class="col-lg-6 col-md-6 align-self-center">
        <div class="right-serv-details">
          <?php if (get_theme_mod('digitalx_agencies_offer_section_text')) { ?>
            <p class="serv-description mb-md-0 mb-3"><?php echo esc_html(get_theme_mod('digitalx_agencies_offer_section_text')); ?></p>
          <?php } ?>
        </div>
      </div>
    </div>
    <div class="owl-carousel owl-theme">
      <?php
      $digitalx_agencies_post_category = get_theme_mod('digitalx_agencies_offer_section_category');
      if ($digitalx_agencies_post_category) :
        $digitalx_agencies_category_id = term_exists($digitalx_agencies_post_category, 'category');
        if ($digitalx_agencies_category_id !== 0 && $digitalx_agencies_category_id !== null) :
          $digitalx_agencies_posts_to_show = get_theme_mod('digitalx_agencies_posts_to_show', 2);
          $digitalx_agencies_page_query = new WP_Query(array(
            'category_name' => esc_attr($digitalx_agencies_post_category),
            'posts_per_page' => $digitalx_agencies_posts_to_show,
          ));

          $digitalx_agencies_post_count = 0;
          if ($digitalx_agencies_page_query->have_posts()) :
            while ($digitalx_agencies_page_query->have_posts()) : $digitalx_agencies_page_query->the_post(); 
              $digitalx_agencies_post_count++;
          ?>
              <div class="item">
                <div class="serv-box">
                      <div class="inner-box-image">
                        <?php if (has_post_thumbnail()) : ?>
                          <img src="<?php echo esc_url(get_the_post_thumbnail_url(null, 'full')); ?>" />
                        <?php else : ?>
                          <img class="serv-color" src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/sliderimage.png' ); ?>" alt="<?php esc_attr_e( 'Default Image', 'digitalx-agencies' ); ?>" />
                        <?php endif; ?>
                      </div>
                      <div class="inner-box text-start">
                        
                        <div class="row">
                          <div class="col-lg-9 col-md-9 col-9 align-self-center">
                            <div class="serv-cat mt-md-3 mt-3">
                              <?php
                              $digitalx_agencies_post_id = get_the_ID();
                              $digitalx_agencies_categories = get_the_category( $digitalx_agencies_post_id );

                              if ( ! empty( $digitalx_agencies_categories ) && ! is_wp_error( $digitalx_agencies_categories ) ) {
                                  $digitalx_agencies_categories = array_slice( $digitalx_agencies_categories, 0, 3 );

                                  echo '<div class="post-categories mb-2">';

                                  foreach ( $digitalx_agencies_categories as $category ) {
                                      echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" class="tag-link">' . esc_html( $category->name ) . '</a>';
                                  }

                                  echo '</div>';
                              }
                              ?>
                            </div>
                            <h3 class="mb-0 text-capitalize"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                          </div>
                          <div class="col-lg-3 col-md-3 col-3 align-self-center">
                            <div class="more-details">
                              <a href="<?php the_permalink(); ?>"><i class="fas fa-arrow-right"></i></a>
                            </div>
                          </div>
                        </div>
                      </div>
                </div>
              </div>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
          <?php else : ?>
            <div class="no-postfound"></div>
          <?php endif; ?>
        <?php endif; ?>
      <?php endif; ?>
    </div>
  </div>
</section>
<?php endif; ?>