<?php
    global $rdgb;
    $block_styles = [];
    $block_name = BLOCK_RDG_TOP_CAPPERS;
    $block_classes = [$block_name];
    $block_top_cappers = $rdgb->settings->get_block($block_name);

    if (!empty($block['backgroundColor'])) {
        $block_classes[] = 'has-background';
        $block_classes[] = 'has-'.$block['backgroundColor'].'-background-color';
    }

    if (!empty($block['style']['color']['background'])) {
        $block_styles[] = 'background-color: '.$block['style']['color']['background'].';';
    }

    $titles = get_field('titles');
    $cappers = get_field('cappers');
    $position_block = $rdgb->helpers->get_blocks_spaces(get_fields());
?>

<div class="rdg-blocks">
    <div class="<?php echo esc_attr(join(' ', $block_classes)); ?>" style="<?php echo $position_block . esc_attr(join(' ', $block_styles)); ?>">
        <div class="container content grid-container">
            <?php require RDGBLOCKS_PLUGIN_BLOCKS_DIR.'partials/titles.php'; ?>

            <?php if (!empty($cappers)): ?>
                <div class="cappers">
                    <?php foreach($cappers as $capper):?>
                        <div class="capper">
                            <div class="capper-header">
                                <div class="title">
                                    <span class="header-title"><?php echo $capper['title']; ?></span>
                                </div>
                            </div>
                            <div class="capper-body">
                                <?php if (isset($capper['logo']['url'])): ?>
                                    <div class="logo">
                                        <img src="<?php echo $capper['logo']['url']; ?>" alt="Image">
                                    </div>
                                <?php endif; ?>

                                <?php if (!empty($capper['name'])): ?>
                                    <div class="name">
                                        <span><?php echo $capper['name']; ?></span>
                                    </div>
                                <?php endif; ?>

                                <?php if (!empty($capper['score'])): ?>
                                    <div class="score separator">
                                        <span><?php echo $capper['score']; ?></span>
                                    </div>
                                <?php endif; ?>

                                <?php if (is_array($capper['more_cappers']) && count($capper['more_cappers']) > 0): ?>
                                    <div class="scores">
                                        <?php $i = 2; ?>
                                        <?php foreach($capper['more_cappers'] as $more): ?>
                                            <div class="score separator">
                                                <div class="score-name"><?php echo $i.'. '.$more['name'] ?></div>
                                                <div class="score-score"><?php echo $more['score']; ?></div>
                                            </div>
                                            <?php $i++; ?>
                                        <?php endforeach; ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
