<?php 
/* 
* Block Name: Hero
* 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.
    $title = get_field('title') ?: get_the_title(get_the_id());
    $separatedTitle = get_field('separated_title') ?: false;
    $ctaIcon = get_field('cta_icon') ?: '';
    $secondaryCta = get_field('secondary_cta') ?: '';
    $secondaryCtaIcon = get_field('secondary_cta_icon') ?: '';
    $imgId = get_field('image') ? get_field('image') : get_post_thumbnail_id(get_the_id());
    $right = get_field('image_position') ? get_field('image_position') : true;
    $highlights = get_field('highlights') ? get_field('highlights') : false;
    $projects = getProject(' '); 
    $cols = 'col-12 col-md-4';
    if ($highlights && is_array($highlights) && $c = count($highlights)) {
        $className .= ' with-highlights';
        switch ($c) {
            case 1:
                $cols = 'col-12';
                break;
            case 2:
                $cols = 'col-12 col-lg-6';
                break;
            case 3:
                $cols = 'col-12 col-md-6 col-lg-4';
                break;
        }
    }
?>
    <section class="section hero-section block d-flex align-items-center <?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 ($separatedTitle): ?>
                <h1 class="h1 separated-title"><?php echo $title; ?></h1>
            <?php endif; ?>
            <div class="row pb-4">
                <?php if ($imgId): ?>
                    <div class="col-lg-6 d-none d-lg-flex justify-content-center align-items-center main-image-container">
                        <link rel=preload href="<?php echo wp_get_attachment_image_url($imgId, 'full'); ?>" as="image">
                        <?php echo wp_get_attachment_image($imgId, 'full', false, ['class' => 'hero-image no-lazy skip-lazy']); ?>
                    </div>
                <div class="col-lg-6 order-last <?php echo $right ? 'order-lg-first' : ''; ?> d-flex align-items-center">
                <?php else: ?>
                    <div class="col-lg-8 offset-lg-2 d-flex align-items-center">
                <?php endif; ?>
                    <div class="text-content">
                        <?php if (!$separatedTitle): ?>
                            <h1 class="h1"><?php echo $title; ?></h1>
                        <?php endif; ?>
                        <?php if ($projects): ?>
                            <div class="post-details d-flex align-items-center">
                                <div class="detail post-categories m-0"><?php echo $projects; ?></div>
                                <div class="separator px-2">|</div>
                                <div class="detail post-date"><i class="fa-regular fa-hourglass"></i> <?php the_time('F j, Y'); ?></div>
                                <?php $pageviewsCount = pageviewsCount(get_the_ID()); ?>
                                <?php if (current_user_can('manage_options') && $pageviewsCount): ?>
                                    <div class="separator px-2">|</div>
                                    <div class="detail pageviews-count"><?php echo $pageviewsCount; ?></div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                        <?php if ($text): ?>
                            <div class="text"><?php echo $text; ?></div>
                        <?php endif; ?>
                            <div class="cta">
                        <?php if(is_array($cta) && !empty($cta)): ?>
                            <a href="<?php echo $cta['url']; ?>" target="<?php echo $cta['target']; ?>" class="button btn btn-primary my-2"><?php echo $cta['title']; ?>
                                <?php if ($ctaIcon): ?><i class="<?php echo $ctaIcon; ?> ps-2"></i><?php endif; ?>
                            </a>
                        <?php endif; ?>
                        <?php if(is_array($secondaryCta) && !empty($secondaryCta)): ?>
                            <a href="<?php echo $secondaryCta['url']; ?>" target="<?php echo $secondaryCta['target']; ?>" class="button btn btn-secondary my-2"><?php echo $secondaryCta['title']; ?>
                                <?php if ($secondaryCtaIcon): ?><i class="<?php echo $secondaryCtaIcon; ?> ps-2"></i><?php endif; ?>
                            </a>
                        <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <?php if (is_array($highlights) && count($highlights)): ?>
                <div class="row hero-highlights pt-4">
                    <?php foreach ($highlights as $key => $highlight): ?>
                        <div class="<?php echo $cols; ?> mb-4">
                            <div class="highlight">
                                <?php if (array_key_exists('label', $highlight) && $highlight['label']): ?>
                                    <b class="label"><?php echo $highlight['label']; ?>
                                    <?php if (isset($highlight['link']) && is_array($highlight['link'])): ?>
                                        <a href="<?php echo $highlight['link']['url']; ?>" target="<?php echo $highlight['link']['target']; ?>" title="<?php echo $highlight['link']['title']; ?>">
                                            <i class="fa-regular fa-circle-right"></i>
                                    </a>
                                    <?php endif; ?>
                                </b>
                                <?php endif; ?>
                                <?php if (array_key_exists('label', $highlight) && $highlight['label']): ?>
                                    <div class="text"><?php echo $highlight['text']; ?></div>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

        </div>
    </section>
<?php endif; ?>

