<?php 
/* 
* Block Name: FAQ
* Post Type: page, post
*/
?>
<?php if (isset($block['data']['previewImage'])): ?>
    <?php do_action('block_image', __FILE__); ?>
<?php else: ?>
    <?php

    // Block helper
    require \get_template_directory() . '/template-parts/base/block-helper.php';
    if (isset($disable) && $disable) {
        return;
    }

    // Load values and assing defaults.
    $imgId = get_field('image') ?: false;
    $imgPosition = get_field('image_position');
    $faqs = get_field('faqs') ?: false;
    $schema = get_field('schema') ?: true;
?>
    <section class="section faq-section block <?php echo $className; ?>" 
        <?php if ($bgUrl): ?>
            style="background-image: url('<?php echo $bgUrl; ?>');"
            data-desktop-image="<?php echo $bgUrl; ?>"
        <?php endif; ?>
        <?php if ($mobileBgUrl): ?>
            data-mobile-image="<?php echo $mobileBgUrl; ?>"
        <?php endif; ?>
        <?php if ($sectionId): ?>
            id="<?php echo $sectionId; ?>"
        <?php endif; ?>
    >
        <div class="container">
            <div class="row">
                <?php if ($imgId): ?>
                    <div class="col-lg-5 d-sm-none d-lg-flex <?php echo $imgPosition ? 'order-lg-1' : ''; ?> align-items-center justify-content-center">
                        <?php echo wp_get_attachment_image($imgId, 'large', false, ['class' => 'hero-image']); ?>
                    </div>
                    <div class="col-lg-7">
                <?php else: ?>
                    <div class="col-lg-8 offset-lg-2">
                <?php endif; ?>
                    <div class="intro">
                        <?php if ($title): ?>
                            <h2 class="h2 text-center d-block"><?php echo $title; ?></h2>
                        <?php endif; ?>
                        <?php if ($text): ?>
                            <div class="text"><?php echo $text; ?></div>
                        <?php endif; ?>
                        <?php if ($faqs && is_array($faqs)): ?>
                        <ul class="faq">
                            <?php foreach ($faqs as $key => $faq): ?>
                            <li class="article <?php if (!$key) { echo 'active'; }?>">
                                <p class="question"><strong><?php echo $faq['question']; ?></strong></p>
                                <div class="answer"><?php echo $faq['answer']; ?></div>
                            </li>
                            <?php endforeach; ?>
                        </ul>
                        <?php endif; ?>
                        <?php if (is_array($cta) && !empty($cta)): ?>
                            <div class="cta pt-4">
                                <a href="<?php echo $cta['url']; ?>" target="<?php echo $cta['target']; ?>" class="button btn btn-primary"><?php echo $cta['title']; ?></a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        <?php 
            if ($schema) {
                // Set the questions
                $questions = [];
                if ($faqs && is_array($faqs)) {
                    foreach ($faqs as $faq) {
                        $questions[] = [
                            '@type' => 'Question',
                            'name' => $faq['question'],
                            'acceptedAnswer' => [
                                '@type' => 'Answer',
                                'text' => $faq['answer']
                            ]
                        ];
                    }
                }

                if (!empty($questions)) {
                    $data = [
                        '@context' => 'http://schema.org/',
                        '@type' => 'FAQPage',
                        'mainEntity' => $questions
                    ];

                    echo '<script type="application/ld+json">'.json_encode($data).'</script>';
                }
            } 
        ?>
    </section>
<?php endif; ?>

