<?php
        if (!defined('ABSPATH')) {
            exit;
        }
        $templates = new Chama_Template_Loader;
        $post_id = get_the_ID();
        $title = get_the_title();
        $content = get_post_meta($post_id, 'campaign_description', true);
        $minimum_amount = get_post_meta($post_id, 'campaign_min_donation', true) ? get_post_meta($post_id, 'campaign_min_donation', true)  : 0;   
        $goal = get_post_meta($post_id, 'campaign_target_amount', true) ? get_post_meta($post_id, 'campaign_target_amount', true) : 0;
        $goal_cents = $goal * CHAMA_CURRENCY_MINIMUM;
        $display_goal = chama_get_formatted_price($goal_cents);
        $show_goal = get_post_meta($post_id, 'campaign_show_target_amount', true);        
        $current_total_funder = get_post_meta($post_id, 'campaign_current_total_funders', true) ? get_post_meta($post_id, 'campaign_current_total_funders', true) : 0;
        $starting_amount = get_post_meta($post_id, 'campaign_starting_amount', true) ? get_post_meta($post_id, 'campaign_starting_amount', true) : 0;
        $starting_amount_cents = $starting_amount * CHAMA_CURRENCY_MINIMUM;
        $display_starting_amount = chama_get_formatted_price($starting_amount_cents);
        $current_total_backers = get_post_meta($post_id, 'campaign_current_total_backers', true) ? get_post_meta($post_id, 'campaign_current_total_backers', true) : 0;
        $current_total_amount = get_post_meta($post_id, 'campaign_current_total_amount', true) ? get_post_meta($post_id, 'campaign_current_total_amount', true) : 0;
        $current_total_amount_cents = $current_total_amount * CHAMA_CURRENCY_MINIMUM;
        $display_current_total_amount = $current_total_amount ? chama_get_formatted_price($current_total_amount_cents) : $display_starting_amount;
        $percentage_of_goal = (int)(($current_total_amount / $goal) * 100);
        $progress_bar_percentage = $percentage_of_goal > 100 ? 100 : $percentage_of_goal;
        $end_date = get_post_meta($post_id, 'campaign_end_date', true);
        $days_left = '';
        $view_more = get_query_var('view_more') !== '' && get_query_var('view_more');
        $show_description = get_query_var('show_description') !== '' && get_query_var('show_description');
        $show_share = get_query_var('show_share') !== '' && get_query_var('show_share');
    	
        $current_url        = home_url(chama_get_current_request_uri());
        $social_url = $social_title = $facebook_url = $x_url = $whatsapp_url = $linkedin_url = '' ;
        //setup payment link
        $payment_data = array('amount' => $minimum_amount, 'type' => 'crowdfunding', 'campaign-id'  => $post_id, 'description' => $title);
        $encoded_payment_data = base64_encode(serialize($payment_data));
        $payment_link = add_query_arg(
            array(
                'meta' => $encoded_payment_data,
            ),
            CHAMA_PAYMENT_URL
        );
        $require_login = get_option('chama_require_login');
        if ($require_login) {
                         if (!is_user_logged_in()) {
                            $payment_link = add_query_arg(
                                 array(
                                     'redirect_url' => urlencode($payment_link),
                                 ),
                                 CHAMA_LOGIN_URL
                             );
                         } 
                     }
    
        if ($end_date) {
            $current_date_time = time(); // or your date as well
            $end_date_time = strtotime($end_date);
            //$date_diff = $end_date_time - $current_date_time;
            // $days_left =  round($date_diff / (60 * 60 * 24));
            $days_left = ceil(($end_date_time - $current_date_time) / 86400);
            $days_left_display = $days_left === 1 ? sprintf("<b>%s</b> day left.", $days_left) : sprintf("<b>%s</b> days left.", $days_left);
        }

        ?>
								

		<h4 class="chama-item-title"><?php echo esc_html($title); ?></h4>
        <?php if( $show_description): ?>
        <?php echo esc_html($content); ?>
        <?php endif;?>
		<div class="chama-above-progress-bar">
		    <span class="chama-campaign-current-total"><?php echo esc_html($display_current_total_amount) ?></span>
		    <span class="chama-campaign-current-backers"><b><?php echo  esc_html($current_total_backers); ?></b> backers</span>
		</div>
		<div class="chama-progress-bar">
		  <div class="chama-progress" style="width:<?php echo esc_attr($progress_bar_percentage);?>%"></div>
		</div>
		<div class="chama-below-progress-bar">
		<span class="chama-progress-goal">
		        <?php echo esc_html($percentage_of_goal);?>% of<?php echo $show_goal ? ' ' . esc_html($display_goal) : ''; ?> goal
		</span>
		<span class="chama-days-to-go">
		<?php echo wp_kses($days_left_display, 'chama'); ?>
		</span>
		</div>
        <?php if( $show_share): ?>
            <div id="chama-campaign-share-buttons" class="chama-share-buttons"><strong>Share: </strong><?php $templates->get_template_part('content', 'socialsharebuttons'); ?></div>
            <?php else:?>
            <a href="<?php echo esc_url(CHAMA_CROWDFUNDING_URL . basename(get_permalink($post_id))); ?>" class="chama-button chama-button-medium chama-block chama-crowdfunding-button chama-button-inverse chama-button-inverse chama-view-campaign" title="View this campaign">View this campaign</a>
            <?php endif;?>
       
		<a href="<?php echo esc_url($payment_link) ?>" class="chama-button chama-button-medium chama-block chama-crowdfunding-button" title="Back this campaign">Back this campaign</a>
        <?php if( $view_more): ?>
<div class="chama-view-more"><a href="<?php echo esc_url(CHAMA_CROWDFUNDING_URL); ?>">View More</a></div>
<?php endif;?>


		              
		        




