<?php
$title      = $args['title'];
$category   = $args['category'];

$args = array(
    'post_type'           => 'testimonials',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      => -1,
    'orderby'             => 'ASC',
    'order'               => 'ASC',
    'tax_query'           => array(array(
        'taxonomy'      => 'testimonials_categories',
        'field'         => 'slug',
        'terms'         => $category ? $category : '',
        'operator'      => 'IN'
    ))
);

$the_query = new WP_Query($args);
?>

<section class="section we-loved-customers pt-0 pb-lg-0">
    <div class="container">
        <div class="row gx-lg-5 gy-5 align-items-lg-center">
            <div class="col-lg-6">
                <div class="customer-reviews-wrapper">
                    
                    <?php if ($the_query->have_posts()): ?>
                        <?php 
                        $counter = 0; 
                        echo '<div class="customer-reviews-first">'; 
                        while ($the_query->have_posts()): 
                            $the_query->the_post();
                            $post_id = get_the_ID();
                            $title = get_the_title();
                            $content = get_the_content();
                            $featured_image_url = get_the_post_thumbnail_url($post_id, 'full'); 
                        ?>
                            <div class="customer-reviews">
                                <div class="customer-reviews-text">
                                    <p><?php echo esc_html($content); ?></p>
                                </div>
                                <div class="customer-reviews-author">
                                    <img src="<?php echo esc_url($featured_image_url ?: get_template_directory_uri().'/assets/img/loved-customers/default-avatar.webp'); ?>" 
                                         alt="<?php echo esc_html($title); ?>" class="img-fluid" loading="lazy">
                                    <h2 class="reviews-author-name"><?php echo esc_html($title); ?>,</h2>
                                    <h2 class="reviews-author-position"><?php echo get_field('customer_position'); ?></h2>
                                </div>
                            </div>
                        
                            <?php 
                            $counter++; 
                            if ($counter == 5) echo '</div><div class="customer-reviews-second">';
                            ?>
                        <?php endwhile; ?>
                        </div> <!-- Close customer-reviews-second -->
                        
                        <?php wp_reset_postdata(); ?>
                    <?php endif; ?>

                </div>
            </div>
            
            <div class="col-lg-6">
                <div class="text-cont slide-left">
                    <h2 class="section-title text-capitalize">
                       <?php echo get_field('customer_title'); ?>
                    </h2>
                    <p class="section-content">
                    <?php echo get_field('customer_description'); ?>
                    </p>
                    <div class="buttons-wrapper">
                        <a href="<?php echo get_field('customer_button_link'); ?>" class="purple-outline-btn animated-btn" rel="nofollow" target="_blank"><?php echo get_field('customer_button_text'); ?></a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
