<?php

if ( wp_is_block_theme() ) {
    $header = do_shortcode(do_blocks('<!-- wp:template-part {"slug":"header","tagName":"header"} /-->'));
    $footer = do_shortcode(do_blocks('<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->'));
    wp_head();
    echo $header;
} else {
    get_header();
}


the_post();
$thread = CMA_AnswerThread::getInstance($post->ID);

if (is_404() OR empty($thread)): ?>

	<div id="primary" class="site-content cma-content">

		<div id="content" role="main" class="entry-content">

			<header class="entry-header">

				<h1 class="entry-title">404</h1>

            </header>

		</div>

	</div>



<?php else: ?>

	

<div id="primary" class="site-content cma-content">

    <div class="cma-main">

        <div class="cma-wrapper">

            <div id="content" role="main" class="entry-content">

                <header class="entry-header">

                <h1 class="entry-title"><?php echo $thread->getTitle(); ?><a class="cma-backlink" href="<?php echo esc_attr(CMA::getReferer()); ?>">&laquo; <?php echo CMA_Labels::getLocalized('back_to_previous_page'); ?></a></h1>

                </header>

                <?php

                do_action('CMA_show_messages');

                ?>

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

                    <script type="text/javascript">

                        function thumbsUp(comment_id){

                            thumbs(comment_id, 'up');

                        }

    <?php if(CMA_AnswerThread::isNegativeRatingAllowed()): ?>function thumbsDown(comment_id) {

                                thumbs(comment_id, 'down');

                            }<?php endif; ?>

                        function thumbs(comment_id, upDown){

                            jQuery('#answer-' + comment_id + ' .cma-answer-rating-loading').show();

                            var nonce = jQuery('#answer-' + comment_id).parents('.cma-answers-list').first().data('voteNonce');

                            jQuery.post(self.location, {'cma-action': 'vote', 'cma-comment': comment_id, 'cma-value': upDown, nonce: nonce}, function(data){

                                if(data.success == 1){

                                    jQuery('#answer-' + comment_id + ' .cma-answer-rating-count').text(data.message);

                                    jQuery().toastmessage('showSuccessToast', "<?php echo CMA_Labels::getLocalized('cma_thank_you_for_voting'); ?>");

                                }else {

                                    if (data.message) jQuery().toastmessage('showErrorToast', data.message);

                                }

                                jQuery('#answer-' + comment_id + ' .cma-answer-rating-loading').hide();

                            });

                        }

                    </script>

                <?php endif; ?>

                <table class="cma-answers-list cma-question-table">

                    <tr>

                        <td class="cma-question-content">

                            <div class="cma-question-body"><?php echo $thread->getContent(); ?></div>

                            <div class="cma-question-meta">

                                <div class="cma-question-author"><?php echo CMA_AnswerThread::getAvatarHtml($thread->getAuthor()->ID); printf(CMA_Labels::getLocalized('cma_posted_by_label'). ' ' . $thread->getAuthor()->display_name); ?></div>

                                <div class="cma-question-date"><?php printf(CMA_Labels::getLocalized('asked_on'), $thread->getCreationDate()); ?></div>

                            </div>

                        </td>

                    </tr>

                </table>



                <?php

                $currentSort = !empty($_GET['sort']) ? $_GET['sort'] : CMA_Settings::getOption(CMA_Settings::OPTION_ANSWER_SORTING_BY);

                if (!ctype_alnum($currentSort)) $currentSort = 'newest';

                $answers     = $thread->getAnswers($currentSort);

                if(!empty($answers)):

                    ?>

                    <?php if(CMA_AnswerThread::isRatingAllowed()): ?><ul class="cma-thread-orderby cma-answers-orderby">

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

                            <li<?php if($currentSort == 'votes'): ?> class="cma-current-sort"<?php endif; ?>><a href="?sort=votes"><?php echo CMA_Labels::getLocalized('orderby_highest_rating'); ?></a></li>

                        </ul><?php endif; ?>

                    <?php

                    foreach($answers as $answer):

                        ?>

                        <table class="cma-answers-list" data-vote-nonce="<?php echo esc_attr(wp_create_nonce('cma_vote')); ?>">

                            <tr id="answer-<?php echo $answer['id']; ?>">

                                <?php if(CMA_AnswerThread::isRatingAllowed()): ?><td class="cma-answer-rating" >

                                        <div class="cma-answer-rating-loading"></div>

                                        <a href="javascript:void(0)" onclick="thumbsUp(<?php echo $answer['id']; ?>)" class="cma-thumbs-up" title="<?php _e('Thumbs Up!', 'cm-answers'); ?>">▲</a>

                                        <div class="cma-answer-rating-count"><?php echo $answer['rating']; ?></div>

                                        <?php if(CMA_AnswerThread::isNegativeRatingAllowed()): ?><a href="javascript:void(0)" onclick="thumbsDown(<?php echo $answer['id']; ?>)" class="cma-thumbs-down" title="<?php _e('Thumbs Down!', 'cm-answers'); ?>">▼</a><?php endif; ?>



                                    </td>

                                <?php else: ?>

                                    <td class="cma-answer-norating"></td>

                                <?php endif; ?>



                                <td class="cma-answer-content<?php if(CMA_AnswerThread::isRatingAllowed()): ?> cma-answer-norating-content<?php endif; ?>">

                                    <div class="cma-answer-body"><?php echo nl2br($answer['content']); ?></div>

                                    <div class="cma-answer-meta">

                                        <div class="cma-answer-author"><?php printf(CMA_Labels::getLocalized('cma_posted_by_label'). ' ' .CMA_AnswerThread::getAvatarHtml(CMA_Answer::getById($answer['id'])->getAuthorId()) . $answer['author']); ?></div>

                                        <div class="cma-answer-date"><?php printf(CMA_Labels::getLocalized('answered_on'), $answer['date']); ?></div>

                                    </div>

                                </td>

                            </tr>

                            <?php

                        endforeach;

                    endif;

                    ?>

                </table>

                <div class="cma-form-container">

                    <?php if(is_user_logged_in()): ?>



                        <?php if(!$thread->isResolved()) : ?>

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

                            <form method="post">

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

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

                                <h3><?php echo CMA_Labels::getLocalized('post_your_answer'); ?></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="hidden" name="cma-thread" value="<?php echo $post->ID; ?>"/>

                                <textarea name="content" placeholder="<?php echo CMA_Labels::getLocalized('answer_input_label'); ?>"></textarea><br />

                                <label>

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

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

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

                                <?php if(get_current_user_id() == $thread->getAuthorId()): ?><label>

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

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

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

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

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

                            </form>

                        <?php endif; ?>



                    <?php else: ?>

                        <?php

                        do_action('CMA_login_form');

                        ?>

                    <?php endif; ?>

                </div>

            </div>

        </div>

	    <?php if(CMA_AnswerThread::isSidebarEnabled() && is_active_sidebar('cm-answers-sidebar')): ?>

            <div id="secondary" role="complementary" class="widget-area cma-sidebar" <?php

                 $maxWidth = CMA_AnswerThread::getSidebarMaxWidth();

                 if($maxWidth > 0) echo 'style="width:' . $maxWidth . 'px"';

                 ?>>

                     <?php dynamic_sidebar('cm-answers-sidebar'); ?>

            </div>

        <?php endif; ?>

    </div>

</div>

<?php

endif;

if ( wp_is_block_theme() ) {
    echo $footer;
    wp_footer();
} else {
    get_footer();
}

