<?php
global $campoal_options;
global $current_user;

$home_tabs_sortable = isset($campoal_options['conikal_home_tabs_sortable_field']) ? $campoal_options['conikal_home_tabs_sortable_field'] : array('trending' => 1, 'recent' => 1, 'newsfeed' => 1);
$posts_per_page_setting = isset($campoal_options['conikal_petitions_per_page_field']) ? $campoal_options['conikal_petitions_per_page_field'] : '';
$posts_per_page = $posts_per_page_setting != '' ? $posts_per_page_setting : 10;

$recent_petitions = conikal_recent_petitions(false, false, 1);

$home_tabs = array(
    'newsfeed' => array(
        'id'        => 'newsfeed',
        'icon'      => (is_user_logged_in() ? 'newspaper icon' : 'fire icon'),
        'label'     => (is_user_logged_in() ? esc_html__('Feed', 'campoal') : esc_html__('Featured', 'campoal')),
        'callback'  => (is_user_logged_in() ? 'conikal_load_newsfeed_petitions' : 'conikal_load_featured_petitions'),
    ),
    'trending' => array(
        'id'        => 'trending',
        'icon'      => 'lightning icon',
        'label'     => esc_html__('Trending', 'campoal'),
        'callback'  => 'conikal_load_trending_petitions',
    ),
    'recent' => array(
        'id'        => 'recent',
        'icon'      => 'wait icon',
        'label'     => esc_html__('Recent', 'campoal'),
        'callback'  => 'conikal_load_recent_petitions',
    ),
);
?>

    <div class="ui sticky" id="home-tab-sticky">
        <div class="ui pointing secondary menu" id="home-petition-tab">
            <?php 
            $i = 1;
            foreach ($home_tabs_sortable as $tab => $status) {
                if ($status) { ?>
                    <a class="item<?php echo esc_attr( $i == 1 ? ' active' : '' ) ?>" data-tab="<?php echo esc_attr( $home_tabs[$tab]['id'] ) ?>" id="tab-menu-<?php echo esc_attr( $home_tabs[$tab]['id'] ) ?>"><i class="<?php echo esc_attr( $home_tabs[$tab]['icon'] ) ?>"></i><?php echo esc_html( $home_tabs[$tab]['label'] ) ?></a>
                <?php }
                $i++;
            } ?>
        </div>
    </div>

    <?php 
    $i = 1;
    foreach ($home_tabs_sortable as $tab => $status) {
        if ($status) { ?>
            <div class="home-tab ui tab<?php echo esc_attr( $i == 1 ? ' active' : '' ) ?>" data-tab="<?php echo esc_attr( $home_tabs[$tab]['id'] ) ?>" id="tab-<?php echo esc_attr( $home_tabs[$tab]['id'] ) ?>">
                
                <?php if ( empty($recent_petitions->have_posts()) ) {
                    // alert no campaign founded in the country selected
                    conikal_not_found_post(__('The country you selected has no campaigns found.', 'campoal'), __("No campaigns in your country, global campaigns are shown instead.", 'campoal'), 'warning', 'globe icon');
                } ?>

                <div id="content-placeholder-<?php echo esc_attr( $home_tabs[$tab]['id'] ) ?>" class="content-placeholder">
                    <div class="ui hidden divider"></div>
                    <?php
                    $petition_placeholder_name = wp_is_mobile() ? 'petition-placeholder-mobile' : 'petition-placeholder';
                    $petition_placeholder = conikal_lazy_load_placeholder($petition_placeholder_name);
                    
                    // print peititon placeholder
                    for ($i=0; $i < 6; $i++) { ?>
                        <img src="<?php echo esc_url($petition_placeholder); ?>" alt="<?php echo esc_attr('Loading thumbnail placeholder', 'campoal') ?>">        
                    <?php } ?>
                </div>
                
                <div id="content-<?php echo esc_attr( $home_tabs[$tab]['id'] ) ?>"></div>
                <div class="ui hidden divider"></div>
                <!-- LOAD MORE PETITIONS BUTTON -->
                <button class="ui basic fluid large button" id="load-<?php echo esc_attr( $home_tabs[$tab]['id'] ) ?>" data-page="1" data-number="<?php echo esc_attr($posts_per_page) ?>" data-author="<?php echo esc_attr($current_user->ID) ?>" data-type="<?php echo esc_attr( $home_tabs[$tab]['callback'] ) ?>" data-country="<?php echo esc_attr( empty($recent_petitions->have_posts()) ? 'WORLD' : '' ) ?>"><i class="long arrow down icon"></i><?php esc_html_e('Load more...', 'campoal'); ?></button>
            </div>
            <div class="clearfix"></div>
        <?php }
        $i++;
    } ?>