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

global $campoal_options;
global $post;

$similar_base = isset($campoal_options['conikal_similar_base_field']) ? $campoal_options['conikal_similar_base_field'] : 'both';
$similar_per_page = isset($campoal_options['conikal_similar_petition_per_page_field']) ? $campoal_options['conikal_similar_petition_per_page_field'] : 3;
$minimum_signature = isset($campoal_options['conikal_minimum_signature_field']) ? $campoal_options['conikal_minimum_signature_field'] : 2;
$similar_display = isset($campoal_options['conikal_similar_display_field']) ? $campoal_options['conikal_similar_display_field'] : 2;

$orig_categorys =  wp_get_post_terms($post->ID, 'petition_category', array('fields' => 'ids'));
$orig_topics =  wp_get_post_terms($post->ID, 'petition_topics', array('fields' => 'ids'));
$exclude_ids = array($post->ID);

$args = array(
    'posts_per_page' => $similar_per_page,
    'post_type' => 'petition',
    'post_status' => 'publish',
    'post__not_in' => $exclude_ids
);

if ($similar_base === 'topics') {
    if($orig_topics) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'petition_topics',
                'field'    => 'id',
                'terms'    => $orig_topics,
            ),
        );
    }
} elseif ($similar_base === 'category') {
    if($orig_categorys) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'petition_category',
                'field'    => 'id',
                'terms'    => $orig_categorys,
            )
        );
    }
} else {
    if($orig_topics && $orig_categorys) {
        $args['tax_query'] = array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'petition_category',
                'field'    => 'id',
                'terms'    => $orig_categorys,
            ),
            array(
                'taxonomy' => 'petition_topics',
                'field'    => 'id',
                'terms'    => $orig_topics,
            )
        );
    }
}

$args['meta_query'] = array('relation' => 'AND');

if ($minimum_signature != 0) {
    array_push($args['meta_query'], array(
        'key'     => 'petition_sign',
        'value'   => $minimum_signature,
        'type'    => 'NUMERIC',
        'compare' => '>='
    ));
}

    array_push($args['meta_query'], array(
        'key'     => 'petition_status',
        'value'   => '2',
        'compare' => '!='
    ));


$similar_query = new wp_query($args);


// similar display
$similar_display = ($similar_display == 'slider' && $similar_query->post_count > 3) ? true : false;
?>

<?php if ($similar_query->have_posts() && $orig_topics && $orig_categorys) : ?>
    <div class="petition-similar-section">
        <div class="ui basic secondary vertical segment">
            <div class="ui container">
                <div class="ui hidden section divider"></div>
                <div class="ui huge header"><?php esc_html_e('Similar petitions', 'campoal'); ?></div>
                <div class="title-divider"></div>
                <div class="ui hidden divider"></div>
                <?php if ($similar_display) { ?>
                    <div class="recent-petition-carousel carousel-v1"><div class="post-carousel" data-column="3">
                <?php } else { ?>
                    <div class="ui three stackable cards petition-cards">
                <?php } ?>
                
                <?php while( $similar_query->have_posts() ) : $similar_query->the_post(); ?>
                    <?php if ($similar_display == 'slider') { ?><div><?php } ?>
                        <?php
                        /*
                         * 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', 'grid'); ?>
                    <?php if ($similar_display) { ?></div><?php } ?>
                <?php endwhile; ?>
                
                <?php if ($similar_display == 'slider') { ?>
                    </div></div>
                <?php } else { ?>
                    </div>
                <?php } ?>
                <div class="ui hidden section divider"></div>
            </div>
        </div>
    </div>

    <?php endif;
        wp_reset_query();
        wp_reset_postdata();
    ?>
