<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;
global $current_user;
global $wp;

get_header();

$sidebar_position = isset($campoal_options['conikal_sidebar_field']) ? $campoal_options['conikal_sidebar_field'] : 'right';
$show_bc = isset($campoal_options['conikal_breadcrumbs_field']) ? $campoal_options['conikal_breadcrumbs_field'] : 0;
$view_counter = isset($campoal_options['conikal_view_counter_field']) ? $campoal_options['conikal_view_counter_field'] : 0;
$posts_per_page = isset($campoal_options['conikal_petitions_per_page_field']) ? $campoal_options['conikal_petitions_per_page_field'] : 10;
$blog_layout = isset($campoal_options['conikal_blog_layout_field']) ? $campoal_options['conikal_blog_layout_field'] : 'list';
$signed_author_page = isset($campoal_options['conikal_signed_author_page_field']) ? $campoal_options['conikal_signed_author_page_field'] : 0;
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>
<div class="wrapper author">
    <div class="ui container mobile-full">
    <?php if($show_bc == 1) {
        $author_name = conikal_get_author_name($curauth->ID);
        conikal_petition_breadcrumbs($author_name);
    } else {
        print '<div class="none-breadcrumb"></div>';
    } ?>

    <div class="page content">

    <?php
    // get author's petition
    $author_petitions = conikal_author_petitions($curauth->ID);

    // get target tab param from url
    if (isset($_GET['tab'])) {
        $tab = $_GET['tab'];
    } else {
        if (function_exists('conikal_register_petition_type')) {
            if ( ($current_user->ID != $curauth->ID) && !$author_petitions->have_posts() && $signed_author_page == 1 ) {
                $tab = 'signed';
            } else {
                $tab = 'petitions';
            }
        } else {
            $tab = 'posts';
        }
    }

    // check current user is in their author page
    if ($current_user->ID == $curauth->ID) {
        $user_is_author = true;
    } else {
        $user_is_author = false;
    }

    $order      = isset($_GET['order']) && !empty($_GET['order']) ? $_GET['order'] : 'DESC';
    $orderby    = isset($_GET['orderby']) && !empty($_GET['orderby']) ? $_GET['orderby'] : 'ID';
    ?>

    <div class="ui grid mobile-full">
        <!-- RIGHT SIDEBAR -->
        <?php get_template_part('templates/author-info'); ?>

        <div class="sixteen wide mobile eleven wide computer column mobile-full" id="author-content">
            <?php if (function_exists('conikal_register_petition_type')) : ?>
            <div class="display-tablet only display-computer only">
                <div class="ui secondary menu">
                    <a href="<?php echo home_url( add_query_arg( array('tab' => 'petitions'), $wp->request ) ) ?>" class="item<?php echo esc_attr($tab === 'petitions' ? ' active' : '') ?>" id="petitions" data-bjax><i class="folder icon"></i><?php esc_html_e('Petitions', 'campoal') ?></a>
                    <?php if ($signed_author_page == 1 || $user_is_author) : ?>
                        <a href="<?php echo home_url( add_query_arg( array('tab' => 'signed'), $wp->request ) ) ?>" class="item<?php echo esc_attr($tab === 'signed' ? ' active' : '') ?>" id="signed" data-bjax><i class="check square icon"></i><?php esc_html_e('Signed', 'campoal') ?></a>
                    <?php endif; ?>
                    <?php if ($user_is_author) : ?>
                        <a href="<?php echo home_url( add_query_arg( array('tab' => 'bookmarks'), $wp->request ) ) ?>" class="item<?php echo esc_attr($tab === 'bookmarks' ? ' active' : '') ?>" id="bookmarks" data-bjax><i class="bookmark icon"></i><?php esc_html_e('Bookmarks', 'campoal') ?></a>
                    <?php endif; ?>
                    <?php if (have_posts()) : ?>
                        <a href="<?php echo home_url( add_query_arg( array('tab' => 'posts'), $wp->request ) ) ?>" class="item<?php echo esc_attr($tab === 'posts' ? ' active' : '') ?>" id="posts" data-bjax><i class="archive icon"></i><?php esc_html_e('Posts', 'campoal') ?></a>
                    <?php endif; ?>

                    <div class="right menu">
                        <?php get_template_part('templates/sorting-options') ?>
                    </div>
                </div>
            </div>
            <div class="display-mobile only">
                <div class="ui secondary large menu">
                    <div class="ui dropdown nav-submenu item">
                        <div class="text">
                            <?php 
                            switch ($tab) {
                                case 'petitions':
                                    esc_html_e('Petitions', 'campoal');
                                    break;

                                case 'signed':
                                    esc_html_e('Signed', 'campoal');
                                    break;

                                case 'bookmarks':
                                    esc_html_e('Bookmarks', 'campoal');
                                    break;

                                case 'posts':
                                    esc_html_e('Posts', 'campoal');
                                    break;
                                
                                default:
                                    esc_html_e('Posts', 'campoal');
                                    break;
                            } ?>
                        </div>
                        <i class="dropdown icon"></i>
                        <div class="menu">
                            <a href="<?php echo home_url( add_query_arg( array('tab' => 'petitions'), $wp->request ) ) ?>" class="item<?php echo esc_attr($tab === 'petitions' ? ' active' : '') ?>" id="petitions" data-bjax><i class="folder icon"></i><?php esc_html_e('Petitions', 'campoal') ?></a>
                            <?php if ($signed_author_page == 1 || $user_is_author) : ?>
                                <a href="<?php echo home_url( add_query_arg( array('tab' => 'signed'), $wp->request ) ) ?>" class="item<?php echo esc_attr($tab === 'signed' ? ' active' : '') ?>" id="signed" data-bjax><i class="check square icon"></i><?php esc_html_e('Signed', 'campoal') ?></a>
                            <?php endif; ?>
                            <?php if ($user_is_author) : ?>
                                <a href="<?php echo home_url( add_query_arg( array('tab' => 'bookmarks'), $wp->request ) ) ?>" class="item<?php echo esc_attr($tab === 'bookmarks' ? ' active' : '') ?>" id="bookmarks" data-bjax><i class="bookmark icon"></i><?php esc_html_e('Bookmarks', 'campoal') ?></a>
                            <?php endif; ?>
                            <a href="<?php echo home_url( add_query_arg( array('tab' => 'posts'), $wp->request ) ) ?>" class="item<?php echo esc_attr($tab === 'posts' ? ' active' : '') ?>" id="posts" data-bjax><i class="archive icon"></i><?php esc_html_e('Posts', 'campoal') ?></a>
                        </div>
                    </div>
                    
                    <div class="right menu">
                        <?php get_template_part('templates/sorting-options') ?>
                    </div>
                </div>
            </div>
            <div class="ui hidden divider"></div>
            <?php else : ?>
            <div class="ui secondary large menu">
                <?php get_template_part('templates/sorting-options') ?>
            </div>
            <?php endif; ?>

            <?php if ($tab === 'petitions') : ?>
                <?php
                if ($user_is_author) {
                    $author_petitions = conikal_my_petitions($curauth->ID);
                } else {
                    $author_petitions = $author_petitions;
                }

                // get total posts founded
                if($author_petitions->have_posts()) {
                    $total_p = $author_petitions->found_posts;
                } else {
                    $total_p = 0;
                }
                // display petition's author
                if($author_petitions->have_posts()) : ?>
                    <div id="content">
                        <?php while($author_petitions->have_posts()) : $author_petitions->the_post();
                            /*
                             * Include the petition template for the content.
                             * If you want to override this in a child theme, then include a file
                             */
                            get_template_part('loop/content-petition', 'list');

                        endwhile; ?>
                    </div>

                    <div class="ui hidden divider"></div>

                    <!-- LOAD MORE BUTTON -->
                    <button class="ui basic fluid large button" id="load-more" data-order="<?php echo esc_attr($order) ?>" data-orderby="<?php echo esc_attr($orderby) ?>" data-page="2" data-number="<?php echo esc_attr($posts_per_page) ?>" data-author="<?php echo esc_attr($curauth->ID) ?>" data-type="<?php echo esc_attr($user_is_author ? 'conikal_load_my_petitions' : 'conikal_load_author_petitions') ?>"><i class="long arrow down icon"></i><?php esc_html_e('Load more...', 'campoal'); ?></button>     
                <?php else :
                    // display not found post notice
                    conikal_not_found_post(__('No campaigns found.', 'campoal'), __("We're sorry, but no campaigns have been started yet.", 'campoal'));
                    endif; ?>
                <?php elseif ($tab === 'signed' && ($signed_author_page == 1 || $user_is_author)) : ?>
                <?php 
                 $signed_petitions = conikal_signed_petitions($curauth->ID);

                // get total posts founded
                if($signed_petitions) {
                    $total_p = $signed_petitions->found_posts;
                } else {
                    $total_p = 0;
                }
                
                // display petition's author
                if($signed_petitions) : ?>
                    <div id="content">
                        <?php while($signed_petitions->have_posts() ) : $signed_petitions->the_post();
                            /*
                             * Include the petition template for the content.
                             * If you want to override this in a child theme, then include a file
                             */
                            get_template_part('loop/content-petition', 'list');

                        endwhile; ?>
                    </div>

                    <div class="ui hidden divider"></div>

                    <!-- LOAD MORE BUTTON -->
                    <button class="ui basic fluid large button" id="load-more" data-order="<?php echo esc_attr($order) ?>" data-orderby="<?php echo esc_attr($orderby) ?>" data-page="2" data-number="<?php echo esc_attr($posts_per_page) ?>" data-author="<?php echo esc_attr($curauth->ID) ?>" data-type="conikal_load_signed_petitions"><i class="long arrow down icon"></i><?php esc_html_e('Load more...', 'campoal'); ?></button>     
                    
                <?php else :
                    // display not found post notice
                    conikal_not_found_post(__('No campaigns found.', 'campoal'), __("We're sorry, but no campaigns have been signed yet.", 'campoal'));
                endif; ?>
            <?php elseif ($tab === 'bookmarks' && $user_is_author) : ?>
                <?php 
                $bookmark_petitions = conikal_bookmark_petitions($curauth->ID);

                // get total posts founded
                if($bookmark_petitions) {
                    $total_p = $bookmark_petitions->found_posts;
                } else {
                    $total_p = 0;
                }
                // display petition's author
                if($bookmark_petitions) : ?>
                    <div id="content">
                        <?php while($bookmark_petitions->have_posts() ) : $bookmark_petitions->the_post();
                            /*
                             * Include the petition template for the content.
                             * If you want to override this in a child theme, then include a file
                             */
                            get_template_part('loop/content-petition', 'list');

                        endwhile; ?>
                    </div>

                    <div class="ui hidden divider"></div>

                    <!-- LOAD MORE BUTTON -->
                    <button class="ui basic fluid large button" id="load-more" data-order="<?php echo esc_attr($order) ?>" data-orderby="<?php echo esc_attr($orderby) ?>" data-page="2" data-number="<?php echo esc_attr($posts_per_page) ?>" data-author="<?php echo esc_attr($curauth->ID) ?>" data-type="conikal_load_bookmark_petitions"><i class="long arrow down icon"></i><?php esc_html_e('Load more...', 'campoal'); ?></button>     
                    
                <?php else :
                    // display not found post notice
                    conikal_not_found_post(__('No campaigns found.', 'campoal'), __("We're sorry, but no campaigns have been bookmarked yet.", 'campoal'));
                endif; ?>
            <?php else : ?>
                <div id="content">
                <?php
                /*
                 * Loop post content template
                 * If you want to override this in a child theme, then include a file
                 */
                get_template_part('loop/loop-post', 'content'); ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
    </div>
    </div>
</div>

<?php wp_nonce_field('load_petitions_ajax_nonce', 'securityPetitions', true); ?>

<?php get_footer(); ?>