<?php 
/* 
* Block Name: Vote
* Post Type: page, post
*/
if (isset($block['data']['previewImage'])):
    echo blockImage(__FILE__);
else:

    // Enable script and style
    wp_enqueue_style('block-style-vote');
    wp_enqueue_script('block-script-vote');

    // Include helpers
    include get_template_directory() . '/inc/block-helper.php';
    $sectionId = $sectionId ?: 'vote' . get_the_ID();

    // Load values and assing defaults
    $resultsLabel = get_field('results_label') ?: false;
    $choices = get_field('choices') ?: false;
    $headingLevel = get_field('heading_level') ?: 'h2';
    $showCount = get_field('show_vote_count') ?: false;
    ?>

    <section class="section vote-section-wrapper block <?php echo $className; ?>" id="<?php echo $sectionId; ?>" data-pid="<?php the_ID(); ?>">
        <div class="container">
            <?php if ($title): ?>
                <<?php echo $headingLevel; ?>><?php echo $title; ?></<?php echo $headingLevel; ?>>
            <?php endif; ?>
            <div class="choices-wrapper">
                <?php if ($text): ?>
                    <div class="text">
                        <?php echo $text; ?>
                    </div>
                <?php endif; ?>
                <?php if (is_array($choices) && count($choices)): ?>
                    <div class="choices row g-3 d-flex justify-content-center">
                        <?php foreach ($choices as $no => $choice): ?>
                            <div class="col-md-4 col-lg-3">
                                <div class="choice d-flex justify-content-center align-items-center p-3"
                                    data-no="choice<?php echo $no; ?>"
                                    style="background-color: <?php echo $choice['choice_color']; ?>; color: <?php echo $choice['text_color']; ?>;" 
                                >
                                    <?php echo $choice['choice']; ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
            <div class="results-wrapper d-none">
                <?php if ($resultsLabel): ?>
                    <div class="text"><?php echo $resultsLabel; ?></div>
                <?php endif; ?>
                <?php if (is_array($choices) && count($choices)): ?>
                    <div class="results">
                        <?php foreach ($choices as $no => $choice): ?>
                            <div class="result choice<?php echo $no; ?> py-2">
                                <?php echo $choice['choice']; ?>
                                <?php if ($showCount): ?>
                                    <span class="ps-2">(<span class="vote-count">0</span>)</span>
                                <?php endif; ?>
                                <div class="progress">
                                    <div class="py-2 progress-bar progress-bar-striped" role="progressbar" style="width: 0%; background-color: <?php echo $choice['choice_color']; ?>; color: <?php echo $choice['text_color']; ?>;" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100">0%</div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </section>
<?php endif; ?>
