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

global $campoal_options;
global $post;

$recent_supporters          = isset($campoal_options['conikal_recent_supporters_donors_field']) ? $campoal_options['conikal_recent_supporters_donors_field'] : 10;
$recent_donors              = $recent_supporters;
$show_supporters            = isset($campoal_options['conikal_show_supporters_section_field']) ? $campoal_options['conikal_show_supporters_section_field'] : 0;
$show_donors                = isset($campoal_options['conikal_show_donors_section_field']) ? $campoal_options['conikal_show_donors_section_field'] : 0;
$supporters_list_page       = isset($campoal_options['conikal_page_supporters_list_field']) ? $campoal_options['conikal_page_supporters_list_field'] : 0;
$contributer_list_page      = isset($campoal_options['conikal_page_supporters_list_field']) ? $campoal_options['conikal_page_supporters_list_field'] : 0;
$supporters_donors_style    = isset($campoal_options['conikal_supporters_donors_style_field']) ? $campoal_options['conikal_supporters_donors_style_field'] : 'list';

$petition_id = $post->ID;
$supporter_number = conikal_get_supporter_donors_number($petition_id);
$goal = $supporter_number->goal;
$sign_num = $supporter_number->sign;

if ( intval($sign_num) < $recent_supporters ) {
    $recent_supporters = $sign_num;
}

// get form option
$form_id = get_post_meta($petition_id, 'petition_contribute', true);
$contribute_approve = get_post_status($form_id);
if ($contribute_approve != 'publish') {
    $form_id = '';
}

// get donors number
if (class_exists('Give')) { 
    $donors_num = give_get_form_sales_stats( $form_id );
    if ( intval($donors_num) < $recent_donors ) {
        $recent_donors = $donors_num;
    }
}


// get fundraising mode setting
$fundraising_mode = conikal_fundraising_mode_setting($petition_id);

$supporters_list_page = ($supporters_list_page ? get_permalink($supporters_list_page) : '');
$supporters_list_page = add_query_arg(array('id' => $petition_id, 'type' => 'supporter'), $supporters_list_page);

$contributer_list_page = ($contributer_list_page ? get_permalink($contributer_list_page) : '');
$contributer_list_page = add_query_arg(array('id' => $petition_id, 'type' => 'donor'), $contributer_list_page);
?>

<div class="petition-supporters-section">
    <?php if ($show_supporters == 1 && $fundraising_mode != 1) { ?>
    <div class="ui basic vertical segment supporters-section" id="supporters-section<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>" data-load="0">
        <h3 class="ui dividing header"><?php echo sprintf(_n('%s Supporter', '%s Supporters', $sign_num, 'campoal'), conikal_format_number('%!,0i', $sign_num)) ?></h3>

        <?php
        if ($supporters_list_page && $supporters_donors_style == 'avatar') { ?>
            <div class="ui right floated tiny header">
                <a href="<?php echo esc_url($supporters_list_page ? $supporters_list_page : '') ?>" class="text grey" data-bjax><?php esc_html_e('See more...', 'campoal') ?></a>
            </div>
        <?php } ?>

        <div id="recent-supporters-list<?php echo esc_attr($petition_id) ?>" class="supporters-list <?php echo esc_attr($supporters_donors_style == 'avatar' ? 'ui mini images' : 'ui relaxed list') ?>">
        <?php for ($i=0; $i < $recent_supporters; $i++) { ?>
           <?php if ($supporters_donors_style == 'avatar') { ?>
                <img class="ui image" alt="<?php esc_attr_e('Wireframe avatar loader', 'campoal') ?>" src="<?php echo esc_url(get_template_directory_uri() . '/images/wireframe-avatar-loader.svg') ?>">
            <?php } else { ?>
                <div class="item">
                    <img class="ui image supporter-image-loader" alt="<?php esc_attr_e('Wireframe avatar loader', 'campoal') ?>" src="<?php echo esc_url(get_template_directory_uri() . '/images/wireframe-supporter-loader.svg') ?>">
                </div>
            <?php } ?>
        <?php } ?>
        </div>

        <?php
        if ($supporters_list_page && $supporters_donors_style == 'list') { ?>
            <a href="<?php echo esc_url($supporters_list_page ? $supporters_list_page : '') ?>" class="text grey" data-bjax><?php esc_html_e('See more...', 'campoal') ?></a>
        <?php } ?>
    </div>
    <?php } ?>

    <!-- CONTRIBUTER & DONOR LIST -->
    <?php if (isset($form_id) && !empty($form_id) && $show_donors == 1 && !empty($donors_num) && class_exists('Give')) { ?>
    <div class="ui basic vertical segment donors-section" id="donors-section<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>" data-load="0">
        <h3 class="ui dividing header"><?php echo sprintf( _n('%s Backer', '%s Backers', $donors_num, 'campoal'), conikal_format_number('%!,0i', $donors_num) ) ?></h3>

        <?php
        if ($contributer_list_page && $supporters_donors_style == 'avatar') { ?>
            <div class="ui right floated tiny header">
                <a href="<?php echo esc_url($contributer_list_page ? $contributer_list_page : '') ?>" class="text grey" data-bjax><?php esc_html_e('See more...', 'campoal') ?></a>
            </div>
        <?php } ?>

        <div id="recent-donors-list<?php echo esc_attr($petition_id) ?>" class="donors-list <?php echo esc_attr($supporters_donors_style == 'avatar' ? 'ui mini images' : 'ui relaxed list') ?>">
        <?php for ($i=0; $i < $recent_donors; $i++) { ?>
            <?php if ($supporters_donors_style == 'avatar') { ?>
                <img class="ui image" alt="<?php esc_attr_e('Wireframe avatar loader', 'campoal') ?>" src="<?php echo esc_url(get_template_directory_uri() . '/images/wireframe-avatar-loader.svg') ?>">
            <?php } else { ?>
                <div class="item">
                    <img class="ui image supporter-image-loader" alt="<?php esc_attr_e('Wireframe avatar loader', 'campoal') ?>" src="<?php echo esc_url(get_template_directory_uri() . '/images/wireframe-supporter-loader.svg') ?>">
                </div>
            <?php } ?>
        <?php } ?>
        </div>

        <?php
        if ($contributer_list_page && $supporters_donors_style == 'list') { ?>
            <a href="<?php echo esc_url($contributer_list_page ? $contributer_list_page : '') ?>" class="text grey" data-bjax><?php esc_html_e('See more...', 'campoal') ?></a>
        <?php } ?>

    </div>
    <?php } ?>
</div>
