<?php
/*
* 0 == never
* 1 == mobile
* 2 == desktop
* 3 == always
*/
$compact = PeepSo::get_option_new('stream_filters_compact');
$compact_class = [];
if(in_array($compact,[1,3])) {
    $compact_class []= 'pso-posts__filters--compact-mobile'; // @TODO TBD
}

if(in_array($compact,[2,3])) {
    $compact_class []= 'pso-posts__filters--compact-desktop';// @TODO TBD
}

$compact_class = implode(' ', $compact_class);
?>

<div class="pso-posts__filters <?php echo $compact_class;?>" data-ps="filter-forms">
    <?php
    /** #8037 FILTER pill: the stream-type dropdown (now headed "Filter") carrying an embedded
     *  "Hide my posts" checkbox; or, when there is no stream-type dropdown, a standalone "Filter"
     *  dropdown holding just the checkbox. Filtering lives in one place; the toolbar saves a slot. */
    $stream_id = get_current_user_id() ? $user_stream_filters['stream_id'] : 'core_community';
    if (count($stream_id_list)) {
        $default = PeepSo::get_option('stream_id_default');

        if (!isset($stream_id_list[$stream_id]) && !array_key_exists($stream_id, $stream_id_list)) {
            $stream_id = $default;
        }

        if (!isset($stream_id_list[$stream_id]) && !array_key_exists($stream_id, $stream_id_list)) {
            reset($stream_id_list);
            $stream_id = key($stream_id_list);
        }

        !get_current_user_id() && $stream_id = 'core_community';
        $selected = $stream_id_list[$stream_id];
    }

    $has_stream_type   = (count($stream_id_list) > 1);
    $show_my_posts_list = apply_filters('peepso_show_my_posts_list', array());
    $has_my_posts      = ($show_my_posts_list && is_array($show_my_posts_list) && count($show_my_posts_list));
    ?>
    <?php if ($has_stream_type || $has_my_posts) { ?>
    <div class="pso-posts-filters__start">
        <?php if (count($stream_id_list)) { ?>
            <input type="hidden" id="peepso_stream_id" value="<?php echo $stream_id; ?>"/>
        <?php } ?>
        <?php if ($has_stream_type) { ?>
            <div class="pso-posts__filter pso-posts__filter--type pso-posts__filter--filter ps-js-dropdown ps-js-activitystream-filter" data-id="peepso_stream_id">
                <a href="javascript:" class="pso-posts-filter__toggle ps-js-dropdown-toggle" aria-haspopup="true">
                    <i class="<?php echo $selected['icon']; ?>"></i>
                    <span><?php echo $selected['label']; ?></span>
                </a>
                <div class="pso-posts-filter__box ps-js-dropdown-menu" role="menu">
                    <div class="pso-posts-filter__title">
                        <span><?php echo esc_attr__('Filter', 'peepso-core'); ?></span>
                        <a href="javascript:" class="pso-posts-filter__close ps-js-cancel" role="button" aria-label="<?php echo esc_attr__('Close', 'peepso-core'); ?>"><i class="pso-i-cross-small"></i></a>
                    </div>
                    <div class="pso-posts-filter__options">
                        <?php foreach ($stream_id_list as $key => $value) { ?>
                            <a class="pso-posts-filter__option" data-option-value="<?php echo $key; ?>" data-option-label-warning="<?php echo $value['label_warning'];?>" role="menuitem">
                                <div class="pso-posts-filter-option__control">
                                    <input type="radio" name="peepso_stream_id" id="peepso_stream_id_opt_<?php echo $key ?>"
                                           value="<?php echo $key ?>" <?php if ($key == $stream_id) echo "checked"; ?> />
                                </div>
                                <label class="pso-posts-filter-option__content" for="peepso_stream_id_opt_<?php echo $key ?>">
                                    <span class="pso-posts-filter-option__name"><?php echo $value['label']; ?></span>
                                    <div class="pso-posts-filter-option__desc"><?php echo $value['desc']; ?></div>
                                </label>
                                <div class="pso-posts-filter-option__icon">
                                    <i class="<?php echo $value['icon']; ?>"></i>
                                </div>
                            </a>
                        <?php } ?>
                    </div>
                    <?php // #8037 embedded "Hide my posts" checkbox, rendered last after a separator.
                    PeepSoTemplate::exec_template('activity', 'stream-filter-myposts', array('separator' => true)); ?>
                </div>
            </div>
        <?php } else { ?>
            <?php // No stream-type dropdown here: standalone "Filter" dropdown carrying the checkbox.
            PeepSoTemplate::exec_template('activity', 'stream-filter-filter'); ?>
        <?php } ?>
    </div>
    <?php } ?>

    <div class="pso-posts-filters__start pso-posts-filters__sort ps-js-activitystream-filters-wrapper">
        <?php // #8037 two-level Sort-by picker (family + variant), in its own pill.
        PeepSoTemplate::exec_template('activity', 'stream-filter-sort'); ?>
    </div>

    <div class="pso-posts-filters__end">
        <?php

        /** SEARCH POSTS **/
        $search = FALSE;
        $PeepSoUrlSegments = PeepSoUrlSegments::get_instance();

        #4158 ?search/querystring does not work with special chars
        if('search' == $PeepSoUrlSegments->get(1)) {
            $search = $PeepSoUrlSegments->get(2);
        }

        #4158 ?search/querystring does not work with special chars
        if(isset($_GET['filter'])) {
            $PeepSoInput = new PeepSoInput();
            $search = $PeepSoInput->value('filter', '', FALSE);
        }

        #7602 XSS
        if($search!==FALSE) $search = str_replace(['<','>','&gt;','&lt;'], ' ', $search);
        if(!strlen($search)) $search = FALSE;
        ?>
        <input type="hidden" id="peepso_search" value="exact" />
        <!-- Search -->
        <div class="pso-posts__filter pso-posts__filter--search ps-js-dropdown ps-js-activitystream-filter" data-id="peepso_search">
            <a href="#" class="pso-posts-filter__toggle pso-tip pso-tip--top ps-js-dropdown-toggle" aria-haspopup="true" aria-label="<?php echo esc_attr__('Search', 'peepso-core'); ?>">
                <i class="pso-i-search"></i>
            </a>
            <div class="pso-posts-filter__box ps-js-dropdown-menu" role="menu">
                <div class="pso-posts-filter__search">
                    <i class="pso-i-search"></i>
                    <input type="text" id="ps-activitystream-search" class="pso-input--reset pso-posts-filter-search__input"
                           placeholder="<?php echo esc_attr__('Type to search', 'peepso-core'); ?>" value="<?php echo $search;?>" />
                </div>
                <div class="pso-posts-filter__options">
                    <a class="pso-posts-filter__option" data-option-value="exact" role="menuitem">
                        <div class="pso-posts-filter-option__control">
                            <input type="radio" name="peepso_search" id="peepso_search_opt_exact" value="exact" checked />
                        </div>
                        <label class="pso-posts-filter-option__content" for="peepso_search_opt_exact">
                            <span class="pso-posts-filter-option__name"><?php echo esc_attr__('Exact phrase', 'peepso-core'); ?></span>
                        </label>
                    </a>
                    <a class="pso-posts-filter__option" data-option-value="any" role="menuitem">
                        <div class="pso-posts-filter-option__control">
                            <input type="radio" name="peepso_search" id="peepso_search_opt_any" value="any" />
                        </div>
                        <label class="pso-posts-filter-option__content" for="peepso_search_opt_any">
                            <span class="pso-posts-filter-option__name"><?php echo esc_attr__('Any of the words', 'peepso-core'); ?></span>
                        </label>
                    </a>
                </div>
                <div class="pso-posts-filter__actions">
                    <button class="pso-btn pso-btn--neutral ps-js-cancel"><?php echo esc_attr__('Cancel', 'peepso-core'); ?></button>
                    <button class="pso-btn pso-btn--primary ps-js-search" ><?php echo esc_attr__('Search', 'peepso-core'); ?></button>
                </div>
            </div>
            <span data-empty="<?php //echo esc_attr__('Search', 'peepso-core'); ?>"
                  data-keyword="<?php echo esc_attr__('Search: ', 'peepso-core'); ?>"></span>
        </div>

        <?php
        /** ADDITIONAL FILTERS - HOOKABLE **/
        do_action('peepso_action_render_stream_filters');
        ?>
    </div>
</div>
<div class="pso-posts-filters__active" data-ps="filter-active" style="display:none">
    <div class="pso-posts-filters-active__item" data-ps="filter-active-search" style="display:none">
        <span class="pso-posts-filters-active__label"><i class="pso-i-search-alt"></i></span>
        <span class="pso-posts-filters-active__value" data-text></span>
        <a href="#" class="pso-posts-filters-active__close pso-tip pso-tip--top" data-remove
           aria-label="<?php echo esc_attr__('Remove search filter', 'peepso-core'); ?>"><i class="pso-i-cross-small"></i></a>
    </div>
    <div class="pso-posts-filters-active__item" data-ps="filter-active-hashtag" style="display:none">
        <span class="pso-posts-filters-active__label"><i class="pso-i-hashtag"></i></span>
        <span class="pso-posts-filters-active__value" data-text></span>
        <a href="#" class="pso-posts-filters-active__close pso-tip pso-tip--top" data-remove
           aria-label="<?php echo esc_attr__('Remove hashtag filter', 'peepso-core'); ?>"><i class="pso-i-cross-small"></i></a>
    </div>
</div>
<div id="ps-stream__filters-warning" class="ps-posts__filters-warning ps-posts__empty" data-ps="filter-warning" style="display:none">
    <i class="gcis gci-info-circle"></i> <?php echo esc_attr__('You are currently only viewing %s content.','peepso-core'); ?>
</div>
<?php // #8046 Inline warning shown while "hide my posts" is active (the checkbox lives in the Filter
// dropdown, so its state is otherwise invisible). The whole banner is tappable — tapping re-enables
// "show my posts" and reloads. JS toggles its display. ?>
<?php if ($has_my_posts) { ?>
<div id="ps-stream__myposts-warning" class="ps-alert ps-alert--warning ps-js-myposts-warning" data-ps="myposts-warning" role="button" tabindex="0" style="display:none;cursor:pointer">
    <i class="gcis gci-info-circle"></i> <?php echo esc_attr__('Your posts are hidden.', 'peepso-core'); ?>
</div>
<?php } ?>
