<?php
/**
 * Template part for displaying the services section
 *
 * @package Doctor General Physician
 * @subpackage doctor_general_physician
 */

// Static image used as a fallback when there is no featured image
$doctor_general_physician_static_image = get_stylesheet_directory_uri() . '/assets/images/header_img.png'; 

$doctor_general_physician_about_sec = get_theme_mod('doctor_general_physician_about_sec', true);
?>

<?php if (!empty($doctor_general_physician_about_sec)) { ?>

  <section id="about" class="clearfix my-md-5 my-3 px-md-0 px-2">
      <div class="container">
          <div class="row">
              <?php
              // Get the selected about page from the theme mod
              $doctor_general_physician_about_page = array();
              $doctor_general_physician_mod = absint(get_theme_mod('doctor_general_physician_about_page'));
              
              // Check if a valid page has been selected in the Customizer
              if ('page-none-selected' != $doctor_general_physician_mod) {
                  $doctor_general_physician_about_page[] = $doctor_general_physician_mod;
              }

              // Proceed only if a valid page has been selected
              if (!empty($doctor_general_physician_about_page)) :
                  $doctor_general_physician_args = array(
                      'post_type' => 'page', // Querying pages
                      'post__in' => $doctor_general_physician_about_page, // Query only the selected page
                      'orderby' => 'post__in', // Maintain the order
                  );

                  $doctor_general_physician_query = new WP_Query($doctor_general_physician_args);

                  // Check if the query has posts to display
                  if ($doctor_general_physician_query->have_posts()) :
                      while ($doctor_general_physician_query->have_posts()) : 
                          $doctor_general_physician_query->the_post(); ?>
                          
                          <div class="col-lg-5 col-md-6 col-12 mb-md-2 mb-5 align-self-center">
                              <div class="about-main-img">
                                  <?php if (has_post_thumbnail()) { ?>
                                      <!-- Display post's featured image -->
                                      <img src="<?php the_post_thumbnail_url('full'); ?>" alt="<?php the_title_attribute(); ?>" />
                                  <?php } else { ?>
                                      <!-- Fallback image if no featured image is set -->
                                      <img src="<?php echo esc_url($doctor_general_physician_static_image); ?>" alt="<?php esc_attr_e('Default image', 'doctor-general-physician'); ?>" />
                                  <?php } ?>
                              </div>
                          </div>
                      
                      <?php endwhile; ?>

                      <div class="col-lg-7 col-md-6 about-box align-self-center">
                          <?php 
                          // Display the custom heading set in the theme mod
                          $doctor_general_physician_about_short_heading = get_theme_mod('doctor_general_physician_about_short_heading', '');
                          if (!empty($doctor_general_physician_about_short_heading)) { ?>
                              <h2 class="abt-title text-capitalize"><?php echo esc_html($doctor_general_physician_about_short_heading); ?></h2>
                          <?php } ?>
                          
                          <?php 
                          // Loop through the posts again to display content
                          while ($doctor_general_physician_query->have_posts()) : 
                              $doctor_general_physician_query->the_post(); ?>
                              <h3 class="mb-lg-3 mb-2"><a href="<?php the_permalink(); ?>" class="text-capitalize"><?php the_title(); ?></a></h3>
                              <p class="about-content"><?php echo wp_trim_words(get_the_content(), 100); // Limit to 70 words ?></p>
                              <div class="abt-btn mt-lg-5 mt-3">
                                  <a href="<?php the_permalink(); ?>"><?php esc_html_e('Learn More', 'doctor-general-physician'); ?></a>
                              </div>
                          <?php endwhile; ?>

                          <?php wp_reset_postdata(); // Reset post data after the query ?>
                      </div>

                  <?php else : ?>
                      <!-- Display if no posts found -->
                      <div class="no-postfound"><?php esc_html_e('No posts found', 'doctor-general-physician'); ?></div>
                  <?php endif; ?>

              <?php endif; ?>
              
              <div class="clearfix"></div>
          </div>
      </div>
  </section>

<?php } ?>
