<?php
global $wp;
$wp_query = CMA_AnswerController::$query;

// ----------------------------------------------------------------------------------------
// If AJAX request - restore some params
if ($widgetOptions = CMA_AnswerController::restoreWidgetOptions()) {
	if (!empty($widgetOptions['displayOptions'])) $displayOptions = $widgetOptions['displayOptions'];
}

if (empty($displayOptions) OR !is_array($displayOptions)) $displayOptions = array();
// if (empty($questions)) {
// 	$questions = array_filter(array_map(function($post) {
// 		return CMA_Thread::getInstance($post->ID);
// 	}, $wp_query->posts));
// }

// -----------------------------------------------------------------------------------------

$current_url = home_url(add_query_arg(array(), $wp->request), 'relative');
$maxNumPages = (isset($maxNumPages))?$maxNumPages:NULL;

$paginationArguments = compact('maxNumPages');
if (!empty($widget)) $paginationArguments['paged'] = 0;
$dataPagination = json_encode($paginationArguments);
$paginationArguments = isset($paginationArguments) ? array_merge($paginationArguments, $_GET) : array();
$paginationArguments['widgetCacheId'] = (!empty($widgetCacheId) ? $widgetCacheId : (!empty($_GET['widgetCacheId']) ? $_GET['widgetCacheId'] : null));
$paginationArguments['cmatag'] = isset($_GET['cmatag']) ? $_GET['cmatag'] : (isset($displayOptions['tag']) ? $displayOptions['tag'] : '');

$paginationBase = get_post_type_archive_link(CMA_Thread::POST_TYPE);

$currentPage = $wp_query->get('paged');

$permalinkn = add_query_arg(urlencode_deep(array_filter($paginationArguments)), trailingslashit(trailingslashit($paginationBase)));

$permalink = add_query_arg(urlencode_deep(array_filter($paginationArguments)), trailingslashit(trailingslashit($paginationBase) . ($currentPage ? 'page/'. $currentPage : '')));
$permalink = apply_filters('cma_questions_ajax_permalink', $permalink);
// var_dump($permalink);
// var_dump($wp_query->posts);

add_filter('cma_questions_widget_number', function($i) { return $i+1; });
// mkk
?>
<?php do_action('cma_header'); ?>
<?php do_action('cma_index_questions_top', $displayOptions); ?>

<?php //do_action('cma_index_header_after'); ?>
<?php do_action('CMA_breadcrumbs'); ?>

<div class="cma-questions-widget<?php if (is_main_query()) echo ' cma-main-query'; ?>"
     data-ajax="<?php echo isset($ajax) ? 'cma-ajax-active' : ''; ?>"
     data-permalink="<?php echo esc_attr($permalink); ?>"
     id="cma-questions-widget-<?php echo apply_filters('cma_questions_widget_number', 0); ?>"
     style="min-width: <?php echo CMA_Settings::getOption(CMA_Settings::OPTION_QUESTION_AJAX_MIN_WIDTH); ?>">

<?php do_action('CMA_show_messages'); ?>


<div class="cma-questions-container cma-container" data-pagination='<?php echo esc_attr($dataPagination); ?>'>

	<?php if( isset($_GET["sort"]) || isset($_GET["cmatag"]) || isset($_GET["search"]) AND empty($widget)) : ?>
		<div class="cma-backlink-wrapper"><a class="cma-backlink cma-backlink-ajaxtag" href="<?php echo esc_attr(CMA::getReferer()) ?>">&laquo; <?php echo CMA_Labels::getLocalized('back_full_questions_list') ?></a></div>
	<?php endif; ?>

		<?php if (!empty($displayOptions['sortbar'])):
            $currentSort = (!empty($_GET['sort']) AND ctype_alnum($_GET['sort'])) ? $_GET['sort'] : CMA_Settings::getOption(CMA_Settings::OPTION_INDEX_ORDER_BY);
            ?>
            <ul class="cma-thread-orderby">

                <li<?php if( $currentSort == 'newest' ): ?> class="cma-current-sort"<?php endif; ?>><a href="<?php echo esc_attr(add_query_arg(array('sort' => 'newest'), get_pagenum_link(0))); ?>"><?php echo CMA_Labels::getLocalized('orderby_newest'); ?></a></li>

                <li<?php if( $currentSort == 'hottest' ): ?> class="cma-current-sort"<?php endif; ?>><a href="<?php echo esc_attr(add_query_arg(array('sort' => 'hottest'), get_pagenum_link(0))); ?>"><?php echo CMA_Labels::getLocalized('orderby_hottest'); ?></a></li>

                <?php if( CMA_AnswerThread::isRatingAllowed() ): ?><li<?php if( $currentSort == 'votes' ): ?> class="cma-current-sort"<?php endif; ?>><a href="<?php echo esc_attr(add_query_arg(array('sort' => 'votes'), get_pagenum_link(0))); ?>"><?php echo CMA_Labels::getLocalized('orderby_votes'); ?></a></li><?php endif; ?>

                <li<?php if( $currentSort == 'views' ): ?> class="cma-current-sort"<?php endif; ?>><a href="<?php echo esc_attr(add_query_arg(array('sort' => 'views'), get_pagenum_link(0))); ?>"><?php echo CMA_Labels::getLocalized('orderby_views'); ?></a></li>

            </ul>
        <?php endif; ?>
        <?php do_action('cma_follow_thread_top',$displayOptions); ?>
		<?php do_action('CMA_questions_table_before', $displayOptions); ?>
	
		<?php
        if (!empty($questions)): ?>
			<div class="cma-thread-list<?php
				if (CMA_Settings::getOption(CMA_Settings::OPTION_INDEX_NUMBERS_VERTICALLY)) echo ' numbers-vertically';
                if (CMA_Settings::getOption(CMA_Settings::OPTION_INDEX_NUMBERS_RIGHT_SIDE) || $displayOptions['metaposition']) echo ' numbers-right-side';
				if ($displayOptions['resolvedPrefix']) echo ' display-resolved-prefix';
			?>">
				<div class="cma-thread-list-header">
						<?php if ($displayOptions['votes'] OR $displayOptions['views'] OR $displayOptions['answers'] OR !empty($displayOptions['statusinfo'])): ?>
								<?php if( $displayOptions['votes'] ): ?>
									<div><?php echo CMA_Labels::getLocalized('votes_col'); ?></div>
								<?php endif; ?>
			
								<?php if( $displayOptions['answers'] ): ?>
									<div><?php echo CMA_Labels::getLocalized('answers_header'); ?></div>
								<?php endif ?>
								
								<?php if( $displayOptions['views'] ): ?>
									<div><?php echo CMA_Labels::getLocalized('views_col'); ?></div>
								<?php endif ?>

						<?php endif; ?>
	
						<div><?php echo CMA_Labels::getLocalized('Question'); ?></div>
						
				</div>

				<div class="cma-thread-list-body">
					<?php foreach($questions as $thread):
						do_action('cma_question_snippet', $thread, $displayOptions);
					endforeach; ?>
				</div>
			</div>
			
			<?php do_action('CMA_questions_table_after', $displayOptions); ?>
		
		<?php else: ?>
			<p class="c"><?php echo CMA_Labels::getLocalized('no_questions_found'); ?></p>
		<?php endif; ?>
		
		<?php if( $displayOptions['pagination'] ):
			echo CMA_AnswerController::showPagination($paginationArguments, $paginationBase);
		endif; // pagination ?>
	
	<?php if (CMA_Settings::getOption(CMA_Settings::OPTION_PRIVATE_QUESTIONS_ENABLED)): ?>
		<script type="text/javascript">
			jQuery(function($) {
				CMA_private_question_init($);
			});
		</script>
	<?php endif; ?>
    <div class="cma-form-container">

        <?php if( CMA_AnswerThread::canPostQuestions() ): ?>

            <div style="clear:both;height:40px;"></div>

            <form method="post" class="cma-thread-add" id="cma-thread-add">

                <input type="hidden" name="cma-action" value="add" />
                <input type="hidden" name="cma-form-location" value="cminds_answers_shortcode" />

                <input type="hidden" name="nonce" value="<?php echo esc_attr(wp_create_nonce('cma_question')); ?>" />

                <h3><?php echo CMA_Labels::getLocalized('ask_a_question'); ?></h3>

                <?php if (CMA_AnswerThread::getMarkupBoxShow()): ?>

                    <ul class="cma-form-notes">

                        <li><?php _e('Allow markup', 'cm-answers'); ?>: &lt;strong&gt;, &lt;em&gt;, &lt;a&gt;</li>

                        <li><?php _e('Wrap your code using', 'cm-answers'); ?> &lt;pre&gt;&lt;/pre&gt;</li>

                    </ul>

                <?php endif; ?>

                <input type="text" size="50" name="thread_title" placeholder="<?php echo CMA_Labels::getLocalized('enter_question_title'); ?>" data-limitify="<?php echo esc_attr(CMA_Settings::getOption(CMA_Settings::OPTION_JS_LIMIT_QUESTION_TITLE));?>"/><br />
                <?php if ( CMA_Settings::getOption(CMA_Settings::OPTION_QUESTION_DESCRIPTION_OPTIONAL) ) {
                    $placeholder = CMA_Labels::getLocalized('enter_question_content_optional');
                } else {
                    $placeholder = CMA_Labels::getLocalized('enter_question_content');
                } ?>
                <textarea name="thread_comment" cols="50" rows="3" placeholder="<?php echo $placeholder; ?>" data-limitify="<?php
                echo esc_attr(CMA_Settings::getOption(CMA_Settings::OPTION_JS_LIMIT_QUESTION_DESCRIPTION));
                ?>"></textarea><br />

                <div style="clear:both;height:5px;"></div>

                <label>

                    <input name="thread_notify" type="checkbox" value="1"/>

                    <?php echo CMA_Labels::getLocalized('notify_me_of_follow'); ?></label><br />

                <div style="clear:both;height:10px;"></div>

                <input type="submit" value="<?php echo CMA_Labels::getLocalized('button_add_question'); ?>" />

            </form>

        <?php

        else:

            do_action('CMA_login_form');

        endif;

        ?>

    </div>
	
</div>
</div>
