<?php 
/* 
* Block name: Columns
* 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
    $columns = get_field('columns') ?: false;
    $cols = get_field('columns_per_row') ?: false;
    $align = get_field('columns_align') ?: '';
?>
    <section class="section columns-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">
            <?php if ($title): ?>
                <h2 class="h2"><?php echo $title; ?></h2>
            <?php endif; ?>
            <?php if ($text): ?>
                <div class="text"><?php echo $text; ?></div>
            <?php endif; ?>
            <div class="row d-flex <?php echo $align; ?>">
                <?php if ($columns && is_array($columns)): ?>
                    <?php foreach ($columns as $column): ?>
                        <div class="<?php echo $cols; ?>">
                            <div class="item">
                                <div class="icon-container">
                                    <?php if (array_key_exists('icon', $column) && $iconId = $column['icon']) {
                                        $path = wp_get_original_image_path($iconId);
                                        if (strtolower(pathinfo($path, PATHINFO_EXTENSION)) == "svg") {
                                            include($path);
                                        } else {
                                            echo wp_get_attachment_image($iconId, 'thumbnail', false, ['class' => 'icon']);
                                        }
                                    } ?>
                                </div>
                                <h3 class="h3"><?php echo $column['title']; ?></h2>
                                <div class="text"><?php echo $column['text']; ?></div>
                                <?php if (is_array($column['cta']) && !empty($column['cta'])): ?>
                                    <div class="cta">
                                        <a href="<?php echo $column['cta']['url']; ?>" target="<?php echo $column['cta']['target']; ?>" class="button btn btn-primary"><?php echo $column['cta']['title']; ?></a>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach ?>
                <?php endif ?>
            </div>
        </div>
    </section>
<?php endif; ?>
