<?php

/**
 * @package WordPress
 * @subpackage Campoal
 */

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

// Check user is loggedin or not, if not redirect to login page
conikal_redirect_login();

$item_per_page_setting = isset($campoal_options['conikal_item_per_page_field']) ? $campoal_options['conikal_item_per_page_field'] : '';
$item_per_page = $item_per_page_setting != '' ? $item_per_page_setting : 20;
$minimum_signature = isset($campoal_options['conikal_minimum_signature_field']) ? $campoal_options['conikal_minimum_signature_field'] : 0;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$type = isset($_GET['type']) ? sanitize_text_field($_GET['type']) : '';

$topics = get_user_meta($current_user->ID, 'follow_topics', true);
$topics = $topics ? array_reverse($topics) : false;

// paination calulate
if ($topics) {
	$total = count( $topics ); //total items in array    
	$limit = $item_per_page; //per page    
	$pages = ceil( $total / $limit ); //calculate total pages
	$paged = max($paged, 1); //get 1 page when $_GET['page'] <= 0
	$paged = min($paged, $pages); //get last page when $_GET['page'] > $totalPages
	$offset = ($paged - 1) * $limit;
	if( $offset < 0 ) $offset = 0;

	$topics = array_slice( $topics, $offset, $limit );

	$taxonomy_name = 'petition_topics';
	$term_meta_image = 'petition_topics_image';
}
?>
<div class="ui hidden divider"></div>
<div class="ui middle aligned grid">
	<div class="ten wide column">
		<h1 class="ui header submit-petition-title">
	        <div class="content">
	        	<?php esc_html_e('Topics Following', 'campoal'); ?>
	        </div>
	    </h1>
	</div>
	<div class="six wide right aligned column">
		<button class="ui icon basic button" id="dashboard-sidebar-btn"><i class="sidebar icon"></i></button>
	</div>
</div>
<div class="ui hidden divider"></div>

<?php if($topics) : ?>
	<div class="topic-cards ui three column stackable grid">
	<?php foreach ($topics as $topic_id) :
		// get term by id
		$term = get_term_by('id', $topic_id, $taxonomy_name);

		if ($term) :

			// get lastest post
	        $args = array( 
	            'posts_per_page' => 1,
	            'paged' => 1,
	            'post_type' => 'petition',
	            'post_status' => 'publish'
	        );

	        $args['tax_query'] = array(
	            'relation' => 'OR',
	            array(
	                'taxonomy' => 'petition_topics',
	                'terms'    => $term->term_id,
	            )
	        );
	        $args['tag_id'] = $term->term_id;
	        $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' => '>='
	            ));
	        }
	        $postslist = new WP_Query( $args );
	        
	        if ( $postslist->have_posts() ) {
	            $post = $postslist->posts[0];
	        }

	        // get avatar
			$term_image = get_term_meta($term->term_id, $term_meta_image, true);
	        if ($term_image == '' && $postslist->have_posts() ) {
	            $term_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'conikal-petition-medium' );
	            if ($term_image) {
	                $term_image = $term_image[0];
	            } else {
	                $term_image = get_template_directory_uri().'/images/cover-card.svg';
	            }
	        } else {
	            $term_image = get_template_directory_uri().'/images/cover-card.svg';
	        }

	        $term_link  = get_term_link($term->term_id, $taxonomy_name);
			?>
			<div class="column">
				<div class="topic-card snip">
					<img src="<?php echo esc_url($term_image ? $term_image : '') ?>" alt="<?php echo esc_attr($term->name) ?>"/>
					<a href="<?php echo esc_url($term_link) ?>" class="caption" data-bjax>
						<h3><?php echo esc_html($term->name) ?></h3>
					</a>
		            <?php if( is_user_logged_in()  ) {
		                if($topics != '') {
		                        if(in_array($term->term_id, $topics) === false) { ?>
		                        <button id="follow-topic-<?php echo esc_attr($term->term_id) ?>" class="follow-topic follow" data-id="<?php echo esc_attr($term->term_id) ?>">
		                        	<?php esc_html_e('Follow', 'campoal') ?>
		                    	</button>
		                    <?php } else { ?>
		                        <button id="follow-topic-<?php echo esc_attr($term->term_id) ?>" class="follow-topic following" data-id="<?php echo esc_attr($term->term_id) ?>">
		                        	<?php esc_html_e('Following', 'campoal') ?>
		                        </button>
		                <?php }
		                    } else { ?>
		                    	<button id="follow-topic-<?php echo esc_attr($term->term_id) ?>" class="follow-topic follow" data-id="<?php echo esc_attr($term->term_id) ?>">
		                    		<?php esc_html_e('Follow', 'campoal') ?>
		                    	</button>
		                    <?php }
		                } else { ?>
		                	<button class="signin-btn follow-topic follow">
		                		<?php esc_html_e('Follow', 'campoal') ?>
		                	</button>
		            <?php } ?>
				</div>
			</div>
	<?php endif;
	endforeach; ?>
	</div>

	<!-- PAGINATION -->
	<?php conikal_pagination_buttons(_n('Topic', 'Topics', $total, 'campoal'), $pages, $total, $item_per_page) ?>
	
	<?php wp_nonce_field('follow_ajax_nonce', 'securityFollow', true); ?>
<?php else :
    // display not found post notice
    conikal_not_found_post(__('No topics found.', 'campoal'), __("We're sorry, but you have not followed any topics yet.", 'campoal'));
endif; ?>