<?php 
/* 
* Block Name: Service
* 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
    $fixedPrice = get_field('fixed_price') ?: false;
    $price = get_field('price') ?: false;
    $salesPrice = get_field('sales_price') ?: false;
    $currency = get_field('currency') ?: '€';
    $benefits = get_field('benefits') ?: false;
    $shortcode = get_field('form_shortcode') ?: false;
    $right = get_field('form_position') ?: false;
    $schema = get_field('schema') ?: true;
?>
    <section class="section service-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">
                <div class="col-md-6">
                    <?php echo apply_filters('the_content', $shortcode); ?>
                </div>
                <div class="col-md-6 order-last <?php echo $right ? 'order-md-first' : ''; ?> text-content">
                    <?php if ($title): ?>
                        <h2 class="h2"><?php echo $title; ?></h2>
                    <?php endif; ?>
                    <?php if ($text): ?>
                        <div class="text"><?php echo $text; ?></div>
                    <?php endif; ?>
                    <?php if ($price): ?>
                        <h3 class="price">
                            <?php if ($salesPrice): ?>
                                <span class="old"><span class="currency"><?php echo $currency; ?></span><?php echo $price; ?></span> <span class="currency"><?php echo $currency; ?></span><?php echo $salesPrice; ?><?php if (!$fixedPrice): ?><small>/<?php _e('hour', 'boldizart'); ?></small><?php endif; ?>
                            <?php else: ?>
                                <span class="currency"><?php echo $currency; ?></span><?php echo $price; ?><?php if (!$fixedPrice): ?><small>/<?php _e('hour', 'boldizart'); ?></small><?php endif; ?>
                            <?php endif; ?>
                        </h3>
                    <?php endif; ?>
                    <?php if (is_array($benefits) && count($benefits)): ?>
                        <ul class="benefits">
                            <?php foreach ($benefits as $benefit): ?>
                                <li><i class="fa fa-check ok"></i> <?php echo $benefit['benefit']; ?></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <?php if ($schema) {
            /**
             * @todo get the featured image url
             */
            $serviceImage = get_the_post_thumbnail_url(get_the_id());
            $logo = 'https://boldizart.com/wp-content/uploads/2021/05/ba.svg';
            $permalink = get_permalink(get_the_id());
            $data = [
                '@context' => 'http://schema.org/',
                '@type' => 'Service',
                'serviceType' => $title,
                'name' => $title,
                'url' => $permalink,
                'description' => wp_trim_words($text, 200, '...'),
                'image' => $serviceImage,
                'provider' => [
                    '@type' => 'LocalBusiness',
                    'name' => 'BoldizArt',
                    'image' => $logo,
                    'priceRange' => $currency.$price.($fixedPrice ? '/'.__('hour', 'boldizart') : ''),
                    'telephone' => '+381 65 99 22 806',
                    'address' => 'Somborska 21, Novi Sad, Serbia',
                    'url' => 'https://boldizart.com/',
                    'aggregateRating' => [
                        '@type' => 'AggregateRating',
                        'ratingValue' => 4.9,
                        'bestRating' => 5,
                        'reviewCount' => rand(3, 27)
                    ],
                    'areaServed' => [
                        '@type' => 'Country',
                        'name' => 'Serbia'
                    ]
                ]
            ];

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

