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

global $campoal_options;
global $post;

$external_link	 		= isset($campoal_options['conikal_external_link_field']) ? $campoal_options['conikal_external_link_field'] : 0;
$minimum_signature 		= isset($campoal_options['conikal_minimum_signature_field']) ? $campoal_options['conikal_minimum_signature_field'] : 0;
$view_counter 			= isset($campoal_options['conikal_view_counter_field']) ? $campoal_options['conikal_view_counter_field'] : 0;
$decisionmakers_display = isset($campoal_options['conikal_decisionmakers_display_field']) ? $campoal_options['conikal_decisionmakers_display_field'] : 1;
$excerpt_words_list 	= isset($campoal_options['conikal_excerpt_words_list_field']) ? $campoal_options['conikal_excerpt_words_list_field'] : 1;

$id 					= get_the_ID() ? get_the_ID() : $post->ID;
$approve 				= get_post_status($id);
$link 					= get_permalink($id);
$title 					= get_the_title($id);
$category 				= wp_get_post_terms($id, 'petition_category', true);
$excerpt 				= conikal_get_excerpt_by_id($id, $excerpt_words_list);
$comments 				= wp_count_comments($id);
$city 					= get_post_meta($id, 'petition_city', true);
$state 					= get_post_meta($id, 'petition_state', true);
$country 				= get_post_meta($id, 'petition_country', true);
$receiver 				= conikal_get_decistion_makers($id, true, $decisionmakers_display);
$supporter_number 		= conikal_get_supporter_donors_number( $id, 'supporter', wp_is_mobile() );
$goal 					= $supporter_number->goal;
$sign 					= $supporter_number->sign;
$status 				= get_post_meta($id, 'petition_status', true);

$author_name 			= conikal_get_author_name(get_the_author_meta('ID'));
$author_url 			= get_author_posts_url(get_the_author_meta('ID'), get_the_author_meta('user_nicename'));
$avatar 				= conikal_get_avatar_url( get_the_author_meta('ID'), array('size' => 64) );
$avatar_retina 			= conikal_get_avatar_url( get_the_author_meta('ID'), array('size' => 128) );

$user_city          	= get_user_meta( get_the_author_meta('ID'), 'user_city', true);
$user_state         	= get_user_meta( get_the_author_meta('ID'), 'user_state', true);
$user_country       	= get_user_meta( get_the_author_meta('ID'), 'user_country', true);

// get post thumbnail
$thumbnail 				= conikal_get_petition_thumbnail($id, (wp_is_mobile() ? 'conikal-petition-medium' : 'conikal-petition-thumbnail'), false);
$thumbnail_retina 		= conikal_get_petition_thumbnail($id, (wp_is_mobile() ? 'conikal-petition-medium-retina' : 'conikal-petition-thumbnail-retina'), false);

$lazy_placeholder 		= conikal_lazy_load_placeholder(wp_is_mobile() ? 'thumbnail-medium' : 'thumbnail-small');
$lazy_placeholder_avatar = conikal_lazy_load_placeholder('avatar');
?>

<div class="ui segments petition-list-card" id="petition-card-<?php echo esc_attr($id) ?>">
	<?php if ($approve == 'pending') : ?>
		<div class="ui active centered inverted dimmer">
			<div class="content">
				<div class="center">
			        <h2 class="ui icon header">
			        	<i class="warning icon"></i>
			        	<?php esc_html_e('Pending approval!', 'campoal') ?>
			        </h2>
		    	</div>
		    </div>
	    </div>
	<?php endif; ?>
	<div class="ui padded segment">
	    <?php if ($sign >= $goal || $status == '1') { ?>
	        <div class="ui primary right corner large label victory-label">
	                <?php echo conikal_custom_icon('victory') ?>
	        </div>
	    <?php } ?>
	    <div class="ui grid">
	        <div class="sixteen wide mobile ten wide tablet ten wide computer column">
	            <div class="petition-content">
	                <div class="ui grid">
	                    <div class="sixteen wide column">
	                        <div class="ui large header list-petition-title">
	                            <div class="content">
	                                <div class="sub header truncate petition-to"><?php echo conikal_custom_icon('send') ?><?php echo sprintf(esc_html__('Petition to %s', 'campoal'), esc_html($receiver)) ?></div>
	                                <a href="<?php echo esc_url($link) ?>" data-bjax><?php echo esc_html($title) ?></a>
	                            </div>
							</div>
	                    </div>
	                    <div class="sixteen wide column tablet computer only excerpt">
	                        <div class="description">
	                        	<p><?php echo esc_html($excerpt) ?></p>
	                        </div>
	                    </div>
	                </div>
	            </div>
	            <div class="ui grid">
	                <div class="petition-footer">
	                    <div class="sixteen wide column">
	                        <?php if ($country || $state || $city) { ?>
	                        <span class="text grey place"><i class="map marker icon"></i><?php echo esc_html($city ? $city . ', ' : '') . esc_html($state ? $state . ', ' : '') . esc_html($country ? $country : '') ?></span>
	                        <?php } ?>
	                        <div class="ui tiny indicating primary progress petition-goal" data-value="<?php echo esc_attr($sign) ?>" data-total="<?php echo esc_attr($status == '1' ? $sign : $goal) ?>">
	                            <div class="bar">
	                                <div class="progress"></div>
	                            </div>
	                        </div>
	                    </div>
	                </div>
	            </div>
	        </div>
	        <div class="sixteen wide mobile six wide tablet six wide computer column">
	        	<?php if ($thumbnail) : ?>
		            <a class="ui fluid image petition-thumbnail" href="<?php echo esc_url($link) ?>" target="<?php echo esc_attr($external_link != 0 ? '_blank' : '_self') ?>" <?php echo esc_attr($external_link != 0 ? '' : 'data-bjax') ?>>
		                <div class="ui dimmer">
		                    <div class="content">
		                        <div class="center">
		                            <div class="ui icon inverted circular large button"><i class="external alternate icon"></i></div>
		                        </div>
		                        <div class="view-counter">
		                        <?php if ($view_counter == 1 && function_exists('conikal_get_post_views')) { ?>
		                            <i class="eye icon"></i>
		                            <?php echo conikal_format_number('%!,0i', (int) conikal_get_post_views($id), true); ?>
		                        <?php } ?>
		                        </div>
		                    </div>
		                </div>
		                <img class="ui fluid image lazy" data-src="<?php echo esc_url($thumbnail) ?>" data-retina="<?php echo esc_url($thumbnail_retina) ?>" src="<?php echo esc_url($lazy_placeholder) ?>" alt="<?php echo esc_attr($title) ?>">
		            </a>
		        <?php elseif (!wp_is_mobile()): ?>
		        	<div class="ui placeholder segment no-thumbnail">
		        		<a class="ui icon circular basic large button" href="<?php echo esc_url($link) ?>" target="<?php echo esc_attr($external_link != 0 ? '_blank' : '_self') ?>" <?php echo esc_attr($external_link != 0 ? '' : 'data-bjax') ?>>
							<i class="external alternate icon"></i>
						</a>
						<div class="view-counter">
                        <?php if ($view_counter == 1 && function_exists('conikal_get_post_views')) { ?>
                            <i class="eye icon"></i>
                            <?php echo conikal_format_number('%!,0i', (int) conikal_get_post_views($id), true); ?>
                        <?php } ?>
                        </div>
					</div>
		        <?php endif; ?>
	        </div>
	    </div>
	</div>
	<div class="ui padded segment footer-border">
	    <div class="ui grid">
	        <div class="ten wide tablet ten wide computer column tablet computer only">
	            <span class="ui primary label petition-label-supporters">
	                <?php echo conikal_custom_icon('supporter') . esc_html($supporter_number->label); ?>
	            </span>
	            <?php if($category) :
	            	if (array_key_exists(0, $category)) : ?>
	            <a class="ui label petition-label-category" href="<?php echo get_term_link($category[0]->term_id) ?>" data-bjax>
	                <i class="tag icon"></i><?php echo esc_html($category[0]->name); ?>
	            </a>
	            <?php endif;
	            endif; ?>
	            <?php if ($comments->approved != 0) : ?>
		            <a href="<?php echo esc_url($link) . '#comments' . esc_attr($id); ?>" class="ui label petition-label-comments" data-bjax>
		                <i class="comments icon"></i><?php echo sprintf(_n('%s comment', '%s comments', $comments->approved, 'campoal'), conikal_format_number('%!,0i', $comments->approved, true)); ?>
		            </a>
		        <?php endif; ?>
	        </div>
	        <div class="six wide tablet six wide computer right aligned column tablet computer only">
				<a href="<?php echo esc_url($author_url) ?>" data-bjax>
					<img class="ui avatar image supporter-avatar petition-author-avatar lazy" 
					data-src="<?php echo esc_url($avatar) ?>" 
					data-retina="<?php echo esc_url($avatar_retina) ?>" 
					src="<?php echo esc_url($lazy_placeholder_avatar) ?>"
					alt="<?php echo esc_attr($author_name) ?>" 
					data-title="<?php echo esc_attr($author_name) ?>" 
					data-content="<?php if ($user_country || $user_state || $user_city) { ?>
					<?php echo esc_attr($user_city ? $user_city . ', ' : '') . esc_attr($user_state ? $user_state . ', ' : '') . esc_attr($user_country ? $user_country : '') ?>
					<?php } ?>">
				</a>
	        </div>

	        <div class="thirteen wide column mobile only">
	            <span class="ui primary label petition-label-supporters">
	                <?php echo conikal_custom_icon('supporter') . esc_html($supporter_number->label) ?>
	            </span>
	            <?php if ($comments->approved != 0) : ?>
		            <a href="<?php echo esc_url($link) . '#comments' . esc_attr($id); ?>" class="ui label petition-label-comments" data-bjax>
		                <i class="comments icon"></i><?php echo conikal_format_number('%!,0i', $comments->approved, true); ?>
		            </a>
		        <?php endif; ?>
	        </div>
	        <div class="three wide right aligned column mobile only">
				<a href="<?php echo esc_url($author_url) ?>" data-bjax>
					<img class="ui avatar image supporter-avatar petition-author-avatar lazy" 
					data-src="<?php echo esc_url($avatar) ?>" 
					data-retina="<?php echo esc_url($avatar_retina) ?>" 
					src="<?php echo esc_url($lazy_placeholder_avatar) ?>"
					alt="<?php echo esc_attr($author_name) ?>" 
					data-title="<?php echo esc_attr($author_name) ?>" 
					data-content="<?php if ($user_country || $user_state || $user_city) { ?>
					<?php echo esc_attr($user_city ? $user_city . ', ' : '') . esc_attr($user_state ? $user_state . ', ' : '') . esc_attr($user_country ? $user_country : '') ?>
					<?php } ?>">
				</a>
	        </div>
	    </div>
	</div>
</div>