<?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;
$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_grid 	= isset($campoal_options['conikal_excerpt_words_grid_field']) ? $campoal_options['conikal_excerpt_words_grid_field'] : 1;

$id                     = get_the_ID();
$link                   = get_permalink($id);
$title                  = get_the_title($id);
$excerpt                = conikal_get_excerpt_by_id($id, $excerpt_words_grid);
$comments               = wp_count_comments($id);
$receiver               = conikal_get_decistion_makers($id, true, $decisionmakers_display);
$city                   = get_post_meta($id, 'petition_city', true);
$state                  = get_post_meta($id, 'petition_state', true);
$country                = get_post_meta($id, 'petition_country', true);

$supporter_number       = conikal_get_supporter_donors_number($id, 'supporter', true);
$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, 'conikal-petition-thumbnail', false);
$thumbnail_retina           = conikal_get_petition_thumbnail($id, 'conikal-petition-thumbnail-retina', false);

$lazy_placeholder           = conikal_lazy_load_placeholder('thumbnail-small');
$lazy_placeholder_avatar    = conikal_lazy_load_placeholder('avatar');
?>
<div class="ui fluid card petition-card" id="petition-card-<?php echo esc_attr($id) ?>">
    <?php if ($sign >= $goal || $status == '1') { ?>
        <span class="ui primary right large corner label victory-label">
            <?php echo conikal_custom_icon('victory') ?>
        </span>
    <?php } ?>
    <?php if (!empty($thumbnail)) : ?>
        <a href="<?php echo esc_url($link) ?>" class="image petition-thumbnail" 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 big button"><i class="external alternate icon"></i></div>
                    </div>
                    <?php if ($country || $state || $city) { ?>
                        <div class="petition-location"><i class="map marker icon"></i><?php echo esc_html($city ? $city . ', ' : '') . esc_html($state ? $state . ', ' : '') . esc_html($country ? $country : '') ?></div>
                    <?php } ?>
                    <?php if ($view_counter == 1 && function_exists('conikal_get_post_views')) { ?>
                    <div class="view-counter">
                        <i class="eye icon"></i>
                        <?php echo conikal_format_number('%!,0i', (int) conikal_get_post_views($id), true); ?>
                    </div>
                    <?php } ?>
                </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 endif; ?>
    <div class="content petition-content">
        <div class="meta receiver">
            <span class="truncate petition-to"><?php echo conikal_custom_icon('send') ?> <?php echo sprintf(esc_html__('Petition to %s', 'campoal'), esc_html($receiver)) ?></span>
        </div>
        <div class="header card-petition-title<?php echo esc_attr(!$thumbnail ? ' no-thumbnail' : '') ?>">
            <a href="<?php echo esc_url($link) ?>" data-bjax><?php echo esc_html($title) ?></a>
        </div>
        <div class="description<?php echo esc_attr(!$thumbnail ? ' no-thumbnail' : '') ?>">
            <p><?php echo esc_html($thumbnail ? $excerpt : conikal_get_excerpt_by_id($id, 30)) ?></p>
        </div>
        <?php if (!$thumbnail) : ?>
            <?php if ($country || $state || $city) { ?>
                <div class="meta petition-location"><i class="map marker icon"></i><?php echo esc_html($city ? $city . ', ' : '') . esc_html($state ? $state . ', ' : '') . esc_html($country ? $country : '') ?></div>
            <?php } ?>
            <?php if ($view_counter == 1 && function_exists('conikal_get_post_views')) { ?>
            <div class="meta view-counter">
                <i class="eye icon"></i>
                <?php echo sprintf(_n('%s view', '%s views', (int) conikal_get_post_views($id), 'campoal'), conikal_format_number('%!,0i', (int) conikal_get_post_views($id), true)); ?>
            </div>
            <?php } ?>
        <?php endif; ?>
    </div>
    <div class="extra content">
        <span class="right floated">
            <a href="<?php echo esc_url($author_url) ?>" data-bjax>
                <img class="ui avatar image supporter-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>
        </span>
        <span class="ui primary label">
            <?php echo conikal_custom_icon('supporter') . esc_html($supporter_number->label); ?>
        </span>
        <?php if ($comments->approved != 0) { ?>
            <span class="ui label"><i class="comments icon"></i><?php echo esc_html(conikal_format_number('%!,0i', $comments->approved, true)) ?></span>
        <?php } ?>
    </div>
    <div class="ui bottom attached 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>