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

global $campoal_options;
global $post;
global $current_user;

$default_accordion = isset($campoal_options['conikal_default_accordion_sign_field']) ? $campoal_options['conikal_default_accordion_sign_field'] : 'share';
$mobile_sign_style = isset($campoal_options['conikal_mobile_sign_style_field']) ? $campoal_options['conikal_mobile_sign_style_field'] : 'button';

$give_prefix        = '_give_';

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

$title              = get_the_title($petition_id);
$link               = get_permalink($petition_id);
$heading            = get_post_meta($petition_id, 'petition_heading', true);

// get form option
$form_id            = conikal_get_petition_form_id($petition_id);
$reveal_label       = get_post_meta($form_id, '_give_reveal_label', true);

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

// check user has purchases
if (class_exists('Give')) {
    $args = array(
        'give_forms' => array($form_id),
        'order' => 'DESC'
    );
    $contributes = give_get_payments( $args );

    $user_contribute = false;
    foreach($contributes as $contribute) {
        $customer_id = give_get_payment_donor_id( $contribute->ID );
        if ($customer_id) {
            $customer    = new Give_Customer( $customer_id );
            $user = get_user_by('ID', $customer->user_id);
            if ($user && $user->ID == $current_user->ID) {
                $user_contribute = true; 
            }
        }
    }  
} else {
    $user_contribute = true;
}

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

$give_attrs['id'] = $form_id;
$give_attrs['show_goal'] = false;
?>

<!-- DONATE BOX -->
<?php if (isset($form_id) && $form_id != '' && class_exists('Give')) { ?>
<div class="ui accordion accord-donate" id="accord-donate<?php echo esc_attr($petition_id) ?>">
    <div class="title display none"></div>
    <div class="content">
        <?php if ($fundraising_mode == 0) : ?>
            <?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>
            <?php endif; ?>
        <?php endif; ?>
        <?php echo give_form_shortcode($give_attrs) ?>
    </div> 
</div>
<?php } ?>

<div class="ui form citation-sidebar">
    <?php if ($mobile_sign_style == 'sidebar') { ?>
    <div class="field">
        <button class="ui primary fluid big button open-donate-box" id="open-donate-box<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><?php echo conikal_custom_icon('donate') ?><?php echo esc_html__('Help spread this Campaign', 'campoal') ?></button>
    </div>
    <?php } ?>
</div>