<?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;

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

// user dashboard link
$user_dashboard_page = isset($campoal_options['conikal_page_dashboard_user_field']) ? $campoal_options['conikal_page_dashboard_user_field'] : 0;
$user_dashboard_page = ($user_dashboard_page ? get_permalink($user_dashboard_page) : '');

$followers = array();
if ($type == 'followers') {
	$followers = get_user_meta($current_user->ID, 'follow_user', true);
    $followers = $followers ? array_reverse($followers) : false;
} else {
	$followers = get_user_meta($current_user->ID, 'following_user', true);
	$followers = $followers ? array_reverse($followers) : false;
}

// paination calulate
if ($followers) {
	$total = count( $followers ); //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;

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

	$current_user_follows = get_user_meta($current_user->ID, 'follow_user', true);
}
?>
<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 if ($type == 'followers') {
	        		esc_html_e('Following', 'campoal');
	        	} else {
	        		esc_html_e('Followers', '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>

<div class="ui pointing secondary menu">
  <a href="<?php echo esc_url(add_query_arg(array('section' => 'users_follow', 'type' => 'followers'), $user_dashboard_page)) ?>" class="item<?php echo esc_attr($type == 'followers' ? ' active' : '') ?>" data-tab="followers">
  	<?php esc_html_e('Following', 'campoal') ?>
  </a>
  <a href="<?php echo esc_url(add_query_arg(array('section' => 'users_follow', 'type' => 'following'), $user_dashboard_page)) ?>" class="item<?php echo esc_attr($type == 'following' ? ' active' : '') ?>" data-tab="following">
  	<?php esc_html_e('Followers', 'campoal') ?>
  </a>
</div>

<?php if ($followers) : ?>
	<table class="ui very basic celled very padded striped table" data-tab="followers">
	  	<thead>
	    <tr>
	    	<th>
	    		<div class="ui grid computer only">
	    			<div class="sixteen wide mobile nine wide computer column"><?php ($type == 'followers' ? esc_html_e('User Following', 'campoal') : esc_html_e('User Followers', 'campoal')) ?></div>
	    			<div class="six wide mobile three wide computer column"><?php esc_html_e('Joined', 'campoal') ?></div>
	    			<div class="three wide mobile one wide computer center aligned column"><?php esc_html_e('Petitions', 'campoal') ?></div>
	    		</div>	
	    	</th>
	  	</tr>
	 	</thead>
	  	<tbody>
		<?php
	    foreach ($followers as $user_id) :
	        	$user = get_user_by('ID', $user_id);

	        	if ($user) :
	                $user_id 				= $user->ID;
		            $user_name 				= $user->display_name;
		            $user_bio 				= $user->description;
		            $user_register 			= $user->user_registered;
		            $user_address 			= get_user_meta($user_id, 'user_address', true);
		            $user_neigborhood 		= get_user_meta($user_id, 'user_neigborhood', true);
		            $user_city 				= get_user_meta($user_id, 'user_city', true);
		            $user_state 			= get_user_meta($user_id, 'user_state', true);
		            $user_country 			= get_user_meta($user_id, 'user_country', true);
		            $user_lat 				= get_user_meta($user_id, 'user_lat', true);
		            $user_lng 				= get_user_meta($user_id, 'user_lng', true);
		            $user_gender 			= get_user_meta($user_id, 'user_gender', true);
		            $user_birthday 			= get_user_meta($user_id, 'user_birthday', true);
		            $user_petition_count 	= count_user_posts($user_id, 'petition');
		            $user_avatar 			= conikal_get_avatar_url( $user_id, array('size' => 64) );
					$user_avatar_retina 	= conikal_get_avatar_url( $user_id, array('size' => 128) );

		            $lazy_placeholder_avatar = conikal_lazy_load_placeholder('avatar');
	    	?>
	    	<tr>
		    	<td>
			    	<div class="ui middle aligned grid">
			    		<div class="sixteen wide mobile nine wide computer column">
			    			<h4 class="ui image header">
						        <img class="ui mini circular image lazy" data-src="<?php echo esc_url($user_avatar); ?>" data-retina="<?php echo esc_url($user_avatar_retina); ?>" src="<?php echo esc_url($lazy_placeholder_avatar); ?>" alt="<?php echo esc_attr($user_name) ?>">
						        <div class="content">
						            <a href="<?php echo get_author_posts_url($user_id) ?>" class="header" data-bjax><?php echo esc_html($user_name) ?></a>
						            <div class="sub header">
						            <?php if ($user_country || $user_state || $user_city) { ?>
									<?php echo esc_html($user_city ? $user_city . ', ' : '') . ($user_state ? $user_state . ', ' : '') . ($user_country ? $user_country : '') ?>
									<?php } ?>
						         	</div>
						        </div>
					      	</h4>
			    		</div>
			    		<div class="seven wide mobile three wide computer column">
			    			<?php echo date_i18n(get_option('date_format'), strtotime($user_register))  ?>
			    		</div>
			    		<div class="two wide mobile one wide computer center aligned column">
			    			<?php echo esc_html($user_petition_count ? $user_petition_count : '-') ?>
			    		</div>
			    		<div class="seven wide mobile three wide computer three right aligned column">
			    			<?php if (is_user_logged_in()) {
			                    if ($current_user_follows != '') {
			                        if (in_array($user_id, $current_user_follows) === false) { ?>
			                            <a href="javascript:void(0)" id="follow-page-<?php echo esc_attr($user_id) ?>" class="ui tiny basic circular button follow-page follow" data-id="<?php echo esc_attr($user_id); ?>"><i class="plus icon"></i><?php esc_html_e('Follow', 'campoal') ?></a>
			                        <?php } else { ?>
			                            <a href="javascript:void(0)" id="follow-page-<?php echo esc_attr($user_id) ?>" class="ui tiny primary circular button follow-page following" data-id="<?php echo esc_attr($user_id); ?>"><i class="checkmark user icon"></i><?php esc_html_e('Following', 'campoal') ?></a>
			                    <?php } 
			                        } else { ?>
			                        <a href="javascript:void(0)" id="follow-page-<?php echo esc_attr($user_id) ?>" class="ui tiny basic circular button follow-page follow" data-id="<?php echo esc_attr($user_id); ?>"><i class="plus icon"></i><?php esc_html_e('Follow', 'campoal') ?></a>
			                    <?php }
			                } else { ?>
			                	<a href="javascript:void(0)" id="follow-page-<?php echo esc_attr($user_id) ?>" class="ui tiny basic circular button signin-btn" data-id="<?php echo esc_attr($user_id); ?>"><i class="plus icon"></i><?php esc_html_e('Follow', 'campoal') ?></a>
			                <?php } ?>
			    		</div>
			    	</div>
			    </td>
			</tr>
	        <?php endif;
	    endforeach;
	?>
	  	</tbody>
	  	<tfoot>
	    	<tr>
				<th colspan="5">
				<!-- PAGINATION -->
				<?php conikal_pagination_buttons(_n('User', 'Users', $total, 'campoal'), $pages, $total, $item_per_page) ?>
	    		</th>
	  		</tr>
		</tfoot>
	</table>
	<?php wp_nonce_field('follow_ajax_nonce', 'securityFollow', true); ?>
<?php else :
    // display not found post notice
    conikal_not_found_post(__('No users found.', 'campoal'), __("We're sorry, but you have not followed any users yet.", 'campoal'));
endif; ?>