<?php
    $category = $args['category'];
    $heading  = isset($args['heading']) ? $args['heading'] : 'FAQs';
    $class    = isset($args['class']) ? $args['class'] : '';
    $args = array(
        'post_type'           => 'faqs',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page'      =>-1,
        'orderby'             => 'menu_order',
        'order'               => 'ASC',
        'tax_query'           => array(array(
            'taxonomy'      => 'faqs_categories',
            'field'         => 'slug', //This is optional, as it defaults to 'term_id'
            'terms'         => $category ? $category : 'home',
            'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
        ))
    );

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

<?php if ($faqs->have_posts()) : ?>
    
    <section class="section faqs <?php echo $class; ?>">
        <div class="container">
            <div class="text-cont slide-down">
                <?php 
                    $faq__heading = get_field('faqs_heading');
                    $faq__text = get_field('faqs_content');
                ?>
                <h2 class="section-title"><?php echo $faq__heading ? $faq__heading : $heading; ?></h2>

                <?php if($faq__text):?>
                <p class="section-content"><?php echo $faq__text;?></p>
                <?php endif;?>
            </div>
    
            <div class="accordion" id="faqs-accordion">
                <?php $i = 0; ?>
                <?php while ($faqs->have_posts()) : $faqs->the_post(); ?>
                    <div class="accordion-item slide-down">
                        <h3 class="accordion-header" id="heading-<?php echo $i; ?>">
                            <button class="accordion-button <?php echo $i == 0 ? '' : 'collapsed'; ?>" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-<?php echo $i; ?>" aria-expanded="<?php echo $i == 0 ? 'true' : 'false'; ?>" aria-controls="collapse-<?php echo $i; ?>">
                                <?php echo get_the_title(); ?>
                            </button>
                        </h3>
                        <div id="collapse-<?php echo $i; ?>" class="accordion-collapse collapse <?php echo $i == 0 ? 'show' : ''; ?>" aria-labelledby="heading-<?php echo $i; ?>" data-bs-parent="#faqs-accordion">
                            <div class="accordion-body">
                                <?php the_content(); ?>
                            </div>
                        </div>
                    </div>
                    <?php $i++; ?>
                <?php endwhile; ?>
                <?php wp_reset_postdata(); ?>
            </div>
        </div>
    </section>
    
<?php endif; ?>