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

global $post;
global $campoal_options;
$common_donation_form  	= isset($campoal_options['conikal_common_donation_form_field']) ? $campoal_options['conikal_common_donation_form_field'] : 0;
$common_form_id     	= isset($campoal_options['conikal_common_form_id_field']) ? $campoal_options['conikal_common_form_id_field'] : 0;

$mobile_progress_bar = isset($campoal_options['conikal_mobile_progress_bar_field']) ? $campoal_options['conikal_mobile_progress_bar_field'] : 'sidebar';

$post_type = get_post_type($post->ID);
if ($post_type != 'petition' && $post_type != 'give_forms' ) {
    $petition_id = get_post_meta($post->ID, $post_type . '_post_id', true);
} else {
    $petition_id = $post->ID;
}

$give_prefix        = '_give_';

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

if (!empty($common_donation_form) && !empty($common_form_id)) {
    $form_id    = $common_form_id;
}

$goal_option        = get_post_meta($form_id, $give_prefix . 'goal_option', true);
$reveal_label       = get_post_meta($form_id, '_give_reveal_label', true);
$donation_num       = give_get_form_sales_stats( $form_id );
$goal               = get_post_meta($petition_id, 'petition_goal', true);
$status             = get_post_meta($petition_id, 'petition_status', true);
$donation_needed    = intval($goal) - intval($donation_num);
$backers            = give_get_form_sales_stats( $form_id );

if (give_is_setting_enabled( $goal_option )) {
    $progress_stats         = give_goal_progress_stats( $form_id );
    $goal_stats             = ($progress_stats['raw_goal'] != 0 ? $progress_stats['raw_goal'] : $goal);
    $goal_stats_label       = give_currency_filter( give_format_amount( $goal_stats, array( 'sanitize' => false ) ) );
    $progress_format  	    = $progress_stats['format'];

    $total_incom            = give_get_form_earnings_stats( $form_id );
    $total_raised           = give_currency_filter( give_format_amount( $total_incom, array( 'sanitize' => false ) ) );

    if ($progress_format == 'percentage') {
        $total_actual_raw   = $progress_stats['raw_actual'];
        $total_raised       = $progress_stats['actual'];
    }

    $amount_needed          = intval($goal_stats) - intval($total_incom);
    $amount_needed_label    = give_currency_filter( give_format_amount( $amount_needed, array( 'sanitize' => false ) ) );
} else {
    $goal_stats             = $goal;
    $goal_stats_label       = conikal_format_number('%!,0i', esc_html($goal_stats));

    $total_incom            = give_get_form_earnings_stats( $form_id );
    $total_raised           = give_currency_filter( give_format_amount( $total_incom, array( 'sanitize' => false ) ) );
    
    $amount_needed          = $donation_needed;
    $amount_needed_label    = conikal_format_number('%!,0i', esc_html($amount_needed));
}

?>

<?php if (give_is_setting_enabled( $goal_option ) && (int)$total_incom >= (int)$goal_stats || (int)$status == 1) { ?>
    <div class="ui grid">
        <div class="thirteen wide column">
            <div class="ui medium header sign-section-title text victory">
                <?php echo conikal_custom_icon('victory_inverse') ?>
                <div class="content">
                    <?php ( (int)$status == 1 ? esc_html_e('Confirm Finished!', 'campoal') : esc_html_e('Finished!', 'campoal') ); ?>
                </div>
            </div>
        </div>
        <div class="three wide right aligned column">
            <a href="javascript:void(0)" class="font big close-mobile-sign" id="close-mobile-sign<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><i class="angle down icon"></i></a>
        </div>
    </div>
    <?php if ($mobile_progress_bar == 'sidebar' || $mobile_progress_bar == 'both') { ?>
    <div class="ui indicating tiny victory progress petition-goal" data-value="<?php echo esc_attr($goal_stats) ?>" data-total="<?php echo esc_attr($goal_stats) ?>">
        <div class="bar">
            <div class="progress"></div>
        </div>
        <div class="label">
            <span><?php echo esc_html(sprintf(__('This campaign won with %s raised', 'campoal'), $total_raised)); ?></span>
        </div>
    </div>
    <?php } ?>
<?php } elseif ((int)$status == 2) { ?>
    <div class="ui grid">
        <div class="thirteen wide column">
            <div class="ui mediumm header sign-section-title">
                <i class="lock icon"></i>
                <div class="content">
                    <?php esc_html_e('Fundraising closed', 'campoal') ?>
                </div>
            </div>
        </div>
        <div class="three wide right aligned column">
            <a href="javascript:void(0)" class="font big close-mobile-sign" id="close-mobile-sign<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><i class="angle down icon"></i></a>
        </div>
    </div>
    <?php if ($mobile_progress_bar == 'sidebar' || $mobile_progress_bar == 'both') { ?>
    <div class="ui tiny progress petition-goal" data-value="<?php echo esc_attr($total_incom) ?>" data-total="<?php echo esc_attr($goal_stats) ?>">
        <div class="bar">
            <div class="progress"></div>
        </div>
        <?php if (give_is_setting_enabled( $goal_option )) { ?>
            <div class="label">
                <span class="ned_no" id="ned_no<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>" data-value="<?php echo esc_attr($amount_needed) ?>" data-format="<?php echo esc_attr($amount_needed_label) ?>">
                    <?php echo esc_html($amount_needed_label) ?>
                </span> 
                <?php echo esc_html_e('needed to reach', 'campoal') . ' ' . esc_html($goal_stats_label) ?>
            </div>
        <?php } else { ?>
            <div class="label">
                <?php echo (empty($backers)) ? esc_html_e('No backers raised this fund', 'campoal') : sprintf(_n('Raised by %s backer', 'Raised by %s backers', $backers, 'campoal'), conikal_format_number('%!,0i', $backers, true)); ?>
            </div>
        <?php } ?>
    </div>
    <?php } ?>
<?php } else { ?>
    <div class="ui grid">
        <div class="thirteen wide column">
            <div class="ui medium header sign-section-title">
                <?php echo conikal_custom_icon('supporter') ?>
                <div class="content">
                    <span class="fav_no">
                        <?php echo esc_html( $total_raised ) ?>
                    </span> 
                    <?php echo esc_html__('Raised', 'campoal') ?>
                </div>
            </div>
        </div>
        <div class="three wide right aligned column">
            <a href="javascript:void(0)" class="font big close-mobile-sign" id="close-mobile-sign<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><i class="angle down icon"></i></a>
        </div>
    </div>
    <?php if ($mobile_progress_bar == 'sidebar' || $mobile_progress_bar == 'both') { ?>
    <div class="ui indicating tiny primary progress petition-goal" data-value="<?php echo esc_attr($total_incom) ?>" data-total="<?php echo esc_attr($goal_stats) ?>">
        <div class="bar">
            <div class="progress"></div>
        </div>
        <?php if (give_is_setting_enabled( $goal_option )) { ?>
            <div class="label">
                <span class="ned_no" id="ned_no<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>" data-value="<?php echo esc_attr($amount_needed) ?>" data-format="<?php echo esc_attr($amount_needed_label) ?>">
                    <?php echo esc_html($amount_needed_label) ?>
                </span> 
                <?php echo esc_html_e('needed to reach', 'campoal') . ' ' . esc_html($goal_stats_label) ?>
            </div>
        <?php } else { ?>
            <div class="label">
                <?php echo (empty($backers)) ? esc_html_e('No backers raised this fund', 'campoal') : sprintf(_n('Raised by %s backer', 'Raised by %s backers', $backers, 'campoal'), conikal_format_number('%!,0i', $backers, true)); ?>
            </div>
        <?php } ?>
    </div>
    <?php } ?>
<?php } ?>
            