<?php
    global $rdgb;
    $block_styles = [];
    $block_name = BLOCK_RDG_LEADER_BOARD;
    $block_classes = [$block_name];
    $block_leader_board = $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');
    $position_block = $rdgb->helpers->get_blocks_spaces(get_fields());
    $leaders_titles = $block_leader_board->get_leader_board_titles(true);
    $date_ranges = $block_leader_board->get_leader_board_date_range();

    $category = get_field('category');
    $date_type = (!empty(get_field('date_type'))) ? get_field('date_type') : null;
    $since_date = (!empty(get_field('since_date'))) ? get_field('since_date') : null;
    $to_date = (!empty(get_field('to_date'))) ? get_field('to_date') : null;

    if ($block_leader_board->is_caching()) {
        $api_url = get_field('leader_board_api_url', 'option');
        $api_token = get_field('leader_board_api_token', 'option');
        $items = (!empty(get_field('number_of_items'))) ? get_field('number_of_items') : 365;

        if (!empty($api_url) && !empty($api_token)) {
            $api_attributes = $block_leader_board->get_api_attributes($items, ['category' => $category]);
            $block_custom_id = (!empty($block['data']['custom_id'])) ? $block['data']['custom_id'] : '';
            $cache_file = $rdgb->helpers->get_cache_name($block_name, $post_id, $block_custom_id);
            $response = $block_leader_board->get_resources($api_attributes, $cache_file);

            if (empty($response->message)) {
                $leaders = $response->json;
            } else {
                $leaders = null;
                $message = $response->message;
            }
        } else {
            $message = __('Configure API connection correctly.');
        }
    }

    if (strpos($category, '-')) {
        list($category_primary, $sub_category) = explode('-', $category);
    } else {
        $sub_category = null;
        $category_primary = $category;
    }

    $menus = get_field('menu');
?>

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

            <div class="table">
                <?php if (is_array($menus) && count($menus) > 0): ?>
                    <div class="table-menu">
                        <ul class="menu">
                            <?php $id = 1; ?>
                            <?php foreach($menus as $menu): ?>
                                <?php $class = ($menu['status_menu'] == 'unactive') ? $menu['status_menu'] : 'active' ?>
                                <li><a id="rdg-leader-board-link-<?php echo $id; ?>" href="#" class="<?php echo $class; ?>" data-category="<?php echo $menu['category']; ?>"><?php echo $menu['title']; ?></a></li>
                                <?php $id++; ?>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                <?php endif; ?>
                <div class="table-filter">
                    <ul class="filter">
                        <li>
                            <span class="init">Categories</span>
                            <select id="rdg-leader-board-category">
                                <?php foreach ($leaders_titles as $leader_key => $leader_title): ?>
                                    <?php if (is_array($leader_title)): ?>
                                        <optgroup label="<?php echo $leader_key; ?>">
                                    <?php endif; ?>

                                    <?php if (is_array($leader_title)): ?>
                                        <?php foreach ($leader_title as $key => $sub_titles ): ?>
                                            <?php $select = ''; ?>
                                            <?php if ($key == $category_primary.'-'.$sub_category): ?>
                                                <?php $select = 'selected'; ?>
                                            <?php endif; ?>
                                            
                                            <option value="<?php echo $key; ?>" <?php echo $select; ?>><?php echo $sub_titles; ?></option>
                                        <?php endforeach; ?>
                                    <?php else: ?>
                                        <?php $select = ''; ?>
                                        <?php if ($leader_key == $category_primary): ?>
                                            <?php $select = 'selected'; ?>
                                        <?php endif; ?>
                                        <option value="<?php echo $select; ?>" <?php echo $select; ?>><?php echo $leader_title; ?></option>
                                    <?php endif; ?>

                                    <?php if (is_array($leader_title)): ?>
                                        </optgroup>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </select>
                        </li>
                        <li>
                            <span>Bet Type</span>
                            <select name="" id="">
                                <option>All</option>
                            </select>
                        </li>
                        <li>
                            <span>Period</span>
                            <select name="" id="">
                                <option>All</option>
                            </select>
                        </li>
                        <li>
                            <span>Date Range</span>
                            <select id="rdg-leader-board-date-range">
                                <?php foreach ($date_ranges as $range_key => $range_title): ?>
                                    <?php $select = ''; ?>
                                    <?php if ($range_key == $items): ?>
                                        <?php $select = 'selected'; ?>
                                    <?php endif; ?>
                                    <option value="<?php echo $range_key; ?>" <?php echo $select; ?>><?php echo $range_title; ?></option>
                                <?php endforeach; ?>
                            </select>
                        </li>
                        <li>
                            <span>Pick Range</span>
                            <select name="" id="">
                                <option>All</option>
                            </select>
                        </li>
                    </ul>
                </div>
                <div class="list">
                    <div class="list-header">
                        <span></span>
                        <span></span>
                        <span class="name">Total Bets <i class="fal fa-sort"></i></span>
                        <span>Win <i class="fal fa-sort"></i></span>
                        <span>Loss <i class="fal fa-sort"></i></span>
                        <span>Push <i class="fal fa-sort"></i></span>
                        <span>Win% <i class="fal fa-sort"></i></span>
                        <span>Win Amount <i class="fal fa-sort"></i></span>
                        <span>Risk Amount <i class="fal fa-sort"></i></span>
                        <span>Net <i class="fal fa-sort"></i></span>
                    </div>
                    <div class="list-items">
                        <?php if (is_array($leaders) && count($leaders) > 0): ?>
                            <?php require 'partials/api.php'; ?>
                        <?php else: ?>
                            <?php if (!empty($message)): ?>
                                <?php require 'partials/message.php'; ?>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
