<?php 
    global $rdgb;
    $rdgb = (!empty($rdgb)) ? $rdgb : Rdg_Blocks::instance();

    $block_styles = [];
    $name_block = BLOCK_RDG_BUTTONS_COLUMN;
    $block_classes = [$name_block];
    $block_buttons_column = $rdgb->settings->get_block($name_block);

    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');
    $position_block = $rdgb->helpers->get_blocks_spaces(get_fields());

    $buttons = (!empty(get_field('buttons'))) ? get_field('buttons') : null;
    $show = (!empty(get_field('show_in'))) ? get_field('show_in') : 'column';
?>

<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="content">
            <?php require RDGBLOCKS_PLUGIN_BLOCKS_DIR.'partials/titles.php'; ?>

            <?php if (is_array($buttons) && count($buttons) > 0): ?>
                <div class="buttons <?php echo $show; ?>">
                    <?php foreach($buttons as $button): ?>
                        <?php if (is_array($button['link']) && $button['link']['url'] != ''): ?>
                            <a href="<?php echo $button['link']['url']; ?>" target="<?php echo (!empty($button['link']['target'])) ? $button['link']['target'] : '_self'; ?>">
                        <?php endif; ?>

                        <?php if ($button['type'] == 'image'): ?>
                            <div class="item-button image-type">
                                <?php
                                    $button_bg = (!empty($button['background_color'])) ? $button['background_color'] : 'none';
                                    $button_padding = (!empty($button['padding'])) ? $button['padding'].'rem' : '0';
                                ?>
                                <div class="body <?php echo (!is_array($button['texts']) || count($button['texts']) == 0) ? 'center' : ''; ?>" style="--body-bg: <?php echo $button_bg; ?>; --body-padding: <?php echo $button_padding; ?>;">
                                    <?php if (!empty($button['image']['url'])): ?>
                                        <div class="image">
                                            <img src="<?php echo $button['image']['url']; ?>" alt="Image">
                                        </div>
                                    <?php endif; ?>
                                    <?php if (is_array($button['texts']) && count($button['texts']) > 0): ?>
                                        <div class="texts" style="<?php echo (!empty($button['image']['url'])) ? '--texts-with: calc(100% - 4rem);' : '--texts-with: calc(100%);'; ?>">
                                            <?php foreach($button['texts'] as $text): ?>
                                                <?php if (!empty($text['text'])): ?>
                                                    <?php
                                                        $title = $text['text'];
                                                        $size = (!empty($text['size'])) ? $text['size'].'rem;' : '2rem;';
                                                        $weight = (!empty($text['weight'])) ? $text['weight'].';' : '500;';
                                                        $color = (!empty($text['color'])) ? $text['color'].';' : '#FFFFFF;';
                                                        $text_padding = (!empty($text['text_padding'])) ? $text['text_padding'].'rem;' : '0;';
                                                    ?>
                                                    <span style="--text-size:<?php echo $size; ?> --text-padding:<?php echo $text_padding; ?> --text-weight:<?php echo $weight; ?> --text-color:<?php echo $color; ?>"><?php echo $title; ?></span>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php else: ?>
                            <?php if (!empty($button['text_basic'])): ?>
                                <?php
                                    $padding = (!empty($button['padding'])) ? $button['padding'].'rem;' : '.5rem;';
                                    $title = (!empty($button['text_basic'])) ? $button['text_basic'] : 'Link To';
                                    $size = (!empty($button['size_basic'])) ? $button['size_basic'].'rem;' : '2rem;';
                                    $weight = (!empty($button['weight_basic'])) ? $button['weight_basic'].';' : '500;';
                                    $color = (!empty($button['color_basic'])) ? $button['color_basic'].';' : '#FFFFFF;';

                                    $style = '';
                                    if ($button['style'] == 'custom') {
                                        $border_color = (!empty($button['border_color'])) ? $button['border_color'].';' : '#3a96e0;';
                                        $background_color = (!empty($button['background_color_button'])) ? $button['background_color_button'].';' : '#0160ac;';
                                        $hover_color = (!empty($button['hover_color'])) ? $button['hover_color'].';' : '#03467d;';
                                    } else {
                                        $style = $button['style'];
                                        $border_color = '';
                                        $background_color = '';
                                        $hover_color = '';
                                    }

                                ?>

                                <?php if ($button['style'] == 'custom'): ?>
                                    <div class="item-button basic-type <?php echo $style; ?>" style="--button-padding:<?php echo $padding; ?> --button-size:<?php echo $size; ?> --button-weight:<?php echo $weight; ?> --button-color:<?php echo $color; ?> --button-bg:<?php echo $background_color; ?> --button-hover:<?php echo $hover_color; ?>  --button-border:<?php echo $border_color; ?>">
                                        <span><?php echo $title; ?></span>
                                    </div>
                                <?php else: ?>
                                    <div class="item-button basic-type <?php echo $style; ?>" style="--button-padding:<?php echo $padding; ?> --button-size:<?php echo $size; ?> --button-weight:<?php echo $weight; ?> --button-color:<?php echo $color; ?>">
                                        <span><?php echo $title; ?></span>
                                    </div>
                                <?php endif; ?>
                            <?php endif; ?>
                        <?php endif; ?>

                        <?php if (is_array($button['link']) && $button['link']['url'] != ''): ?>
                            </a>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div class="message danger">
                    <span><?php echo __('Must add some buttons.'); ?></span>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
