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

global $campoal_options;
global $current_user;
global $post;

$similar 		    = isset($campoal_options['conikal_similar_field']) ? $campoal_options['conikal_similar_field'] : 0;
$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;
$similar_display    = isset($campoal_options['conikal_similar_display_field']) ? $campoal_options['conikal_similar_display_field'] : 2;

$orig_categorys =  wp_get_post_terms($post->ID, 'give_forms_category', array('fields' => 'ids'));
$orig_tags =  wp_get_post_terms($post->ID, 'give_forms_tag', array('fields' => 'ids'));
$exclude_ids = array($post->ID);

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

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

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

$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_tags && $orig_categorys && $similar) : ?>
    <div class="give-similar-section">
        <div class="ui basic secondary vertical segment">
            <div class="ui container">
                <div class="ui hidden divider"></div>
                <div class="ui large header"><?php esc_html_e('Similar campaigns', '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) { ?><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-give', 'grid'); ?>
                    <?php if ($similar_display) { ?></div><?php } ?>
                <?php endwhile; ?>
                
                <?php if ($similar_display) { ?>
                    </div></div>
                <?php } else { ?>
                    </div>
                <?php } ?>
                <div class="ui hidden section divider"></div>
            </div>
        </div>
    </div>

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