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

global $campoal_options;

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

$give_prefix        = '_give_';

$form_id            = get_the_ID();
$petition_id        = get_post_meta($form_id, $give_prefix . 'petition_id', true);
$backers            = give_get_form_sales_stats( $form_id );
$link               = get_permalink($form_id);
$title              = get_the_title($form_id);
$comments           = wp_count_comments($form_id);
$category           = wp_get_post_terms($form_id, 'give_forms_category', true);
$content            = get_post_meta($form_id, $give_prefix . 'form_content', true);
$excerpt            = conikal_get_excerpt_by_id($form_id, $excerpt_words_grid, $content);
$city               = get_post_meta($form_id, $give_prefix . 'city', true);
$state              = get_post_meta($form_id, $give_prefix . 'state', true);
$country            = get_post_meta($form_id, $give_prefix . 'country', true);

$goal_option        = get_post_meta($form_id, $give_prefix . 'goal_option', true);
$raise_number       = conikal_get_supporter_donors_number($form_id, 'donor', 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);

// lazy placeholder
$thumbnail          = conikal_get_petition_thumbnail($form_id, 'conikal-petition-thumbnail', false);
$thumbnail_retina   = conikal_get_petition_thumbnail($form_id, 'conikal-petition-thumbnail-retina', false);

$lazy_placeholder   = conikal_lazy_load_placeholder('thumbnail-small');
$lazy_placeholder_avatar = conikal_lazy_load_placeholder('avatar');

$post_views         = conikal_format_number('%!,0i', (int) conikal_get_post_views($form_id), true);

$goal_option        = get_post_meta($form_id, $give_prefix . 'goal_option', true);

// use petition thumbnail if donation form doesn't exist thumbnail
if (!$thumbnail && !empty($petition_id)) {
    $thumbnail          = conikal_get_petition_thumbnail($petition_id, 'conikal-petition-thumbnail', false);
    $thumbnail_retina   = conikal_get_petition_thumbnail($petition_id, 'conikal-petition-thumbnail-retina', false);
        
    if(!is_array($category) || !isset($category[0])) {
        $category       = wp_get_post_terms($petition_id, 'petition_category', true);
    }

    $city               = get_post_meta($petition_id, 'petition_city', true);
    $state              = get_post_meta($petition_id, 'petition_state', true);
    $country            = get_post_meta($petition_id, 'petition_country', true);
}
?>
<div class="ui fluid card petition-card" id="give-form-card-<?php echo esc_attr($form_id) ?>">
    <?php if (give_is_setting_enabled( $goal_option )) {
        $goal_stats = give_goal_progress_stats( $form_id );
            if ($goal_stats['raw_goal'] != 0 && $goal_stats['raw_actual'] >= $goal_stats['raw_goal']) { ?>
        <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 esc_html($post_views); ?>
                    </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">
            <?php if (is_array($category) && isset($category[0])) : ?>
                <a href="<?php echo get_term_link($category[0]->term_id) ?>" data-bjax>
                    <?php echo esc_html($category[0]->name) ?>    
                </a>
            <?php endif; ?>
        </div>
        <div class="header card-petition-title">
            <a href="<?php echo esc_url($link) ?>" data-bjax><?php echo esc_html($title) ?></a>
        </div>
        <div class="description">
            <p><?php echo esc_html($excerpt) ?></p>
        </div>
    </div>
    <div class="extra content">
        <?php if (give_is_setting_enabled( $goal_option )) : ?>
            <div class="ui tiny header">
                <?php echo sprintf( esc_html__('%1$s of %2$s raised', 'campoal'), esc_html($raise_number->sign_format), esc_html($raise_number->goal_format) ) ?>
            </div>
            <div class="ui tiny primary progress petition-goal" data-value="<?php echo esc_attr($raise_number->sign) ?>" data-total="<?php echo esc_attr($raise_number->goal) ?>">
                <div class="bar">
                    <div class="progress"></div>
                </div>
            </div>
            <div class="ui hidden divider"></div>
        <?php endif; ?>
        <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 (empty($backers)) ? esc_html_e('No Backer', 'campoal') : conikal_custom_icon('supporter') . sprintf(_n('%s backer', '%s backers', $backers, 'campoal'), conikal_format_number('%!,0i', $backers, true)); ?>
        </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>