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

global $campoal_options;
global $wp_query;

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;
$sortby_default = isset($campoal_options['conikal_sortby_default_field']) ? $campoal_options['conikal_sortby_default_field'] : 'date';
$sorder_default = isset($campoal_options['conikal_order_default_field']) ? $campoal_options['conikal_order_default_field'] : 'DESC';
$search_result_page = isset($campoal_options['conikal_page_petitions_search_results_field']) ? $campoal_options['conikal_page_petitions_search_results_field'] : 0;
$search_result_page = ($search_result_page ? get_permalink($search_result_page) : '');

// get sorting options 
$order_options = conikal_sort_petition_options('order');
$orderby_options = conikal_sort_petition_options('orderby');

$order      = isset($_GET['order']) && !empty($_GET['order']) ? sanitize_text_field($_GET['order']) : '';
$orderby    = isset($_GET['orderby']) && !empty($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) : '';
?>
<div class="wrapper petition ui vertical white color very padded segment">
    <div class="ui container search-form-container">
        <div class="ui <?php echo esc_attr(is_active_sidebar('main-widget-area') ? 'relaxed' : 'centered') ?> grid">
            <div class="sixteen wide mobile sixteen wide tablet <?php echo esc_attr(is_active_sidebar('main-widget-area') ? 'sixteen' : 'eleven') ?> wide computer column" id="search-form-content">
                <?php if (function_exists('conikal_register_petition_type')) : ?>
                <div class="ui secondary menu">
                    <a href="<?php echo esc_url(add_query_arg(array('s' => get_search_query()), home_url('/'))) ?>" class="item active"><?php esc_html_e('All', 'campoal') ?></a>
                    <a href="<?php echo esc_url(add_query_arg(array('q' => get_search_query()), $search_result_page)) ?>" class="item"><?php esc_html_e('Campaigns', 'campoal') ?></a>
                </div>
                <?php endif; ?>

                <form role="search" class="post-search" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="GET">
                    <div class="ui fluid search">
                        <div class="ui icon fluid large input">
                            <input type="text" class="prompt search-field" name="s" placeholder="<?php esc_attr_e('Search...', 'campoal') ?>" value="<?php echo esc_attr( get_search_query() ); ?>">
                            <i class="search icon"></i>
                        </div>
                        <div class="results"></div>
                    </div>
                </form>

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

                <!-- start order button -->
                <div class="ui floating dropdown small basic circular button search-order">
                    <?php echo esc_html(!empty($order) ? $order_options[$order] : __('Order', 'campoal')) ?>
                    <div class="menu">
                        <?php foreach ($order_options as $key => $value) { ?>
                            <a class="item" href="<?php echo add_query_arg( array_merge($_GET, array('order' => $key)), home_url( $wp->request ) ); ?>"><?php echo esc_html($value) ?></a>
                        <?php } ?>
                    </div>
                </div>
                <div class="ui floating dropdown small basic circular button search-orderby">
                    <?php echo esc_html(!empty($orderby) ? $orderby_options[$orderby] : __('Sort by', 'campoal')) ?>
                    <div class="menu">
                        <?php foreach ($orderby_options as $key => $value) { ?>
                            <a class="item" href="<?php echo add_query_arg( array_merge($_GET, array('orderby' => $key)), home_url( $wp->request ) ); ?>"><?php echo esc_html($value) ?></a>
                        <?php } ?>
                    </div>
                </div>
                <!-- end order button -->
            </div>
        </div>
    </div>
</div>
<div class="wrapper" id="wrapper">
    <div class="ui container">
        <div class="ui hidden section divider"></div>
        <div class="page content">
            <div class="ui <?php echo esc_attr(is_active_sidebar('main-widget-area') ? 'relaxed' : 'centered') ?> grid">
                <?php if(is_active_sidebar('main-widget-area') && $sidebar_position == 'left') { ?>
                    <div class="sixteen wide mobile sixteen wide tablet five wide computer column computer only post-left-widget-sidebar" id="post-left-widget-sidebar">
                        <?php get_sidebar(); ?>
                    </div>
                <?php } ?>
                <div class="sixteen wide mobile sixteen wide tablet eleven wide computer column" id="content">

                    <?php if ( have_posts() ) : ?>
                        <div class="ui large header search-results-title">
                            <?php echo sprintf( _n('%1$s result for ”%2$s”', '%1$s results for ”%2$s”', $wp_query->found_posts, 'campoal'), $wp_query->found_posts, get_search_query() ) ?>
                        </div>
                        <?php
                        // Start the loop.
                        while ( have_posts() ) : the_post(); ?>

                            <?php
                            /*
                             * Run the loop for the search to output the results.
                             * If you want to overload this in a child theme then include a file
                             * called content-search.php and that will be used instead.
                             */
                            get_template_part('loop/content-post', 'search');

                        // End the loop.
                        endwhile; ?>

                        <!-- PAGINATION -->
                        <?php conikal_pagination_buttons(_n('Result', 'Results', $wp_query->found_posts, 'campoal'), $wp_query->max_num_pages, $wp_query->found_posts, $wp_query->post_count) ?>
                    <?php else : ?>
                        <div class="ui hidden section divider"></div>
                        <?php // display not found post notice
                        conikal_not_found_post(sprintf( esc_html__('No results for ”%s”', 'campoal'), get_search_query() ), __("We're sorry, but no posts matched your criteria.", 'campoal'));
                    endif; ?>
                </div>
                <?php if(is_active_sidebar('main-widget-area') && $sidebar_position == 'right') { ?>
                    <div class="sixteen wide mobile sixteen wide tablet five wide computer column post-right-widget-sidebar" id="post-right-widget-sidebar">
                        <?php get_sidebar(); ?>
                    </div>
                <?php } ?>
            </div>
        </div>
    </div>
</div>
<?php get_footer(); ?>