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

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

// Check user is loggedin or not, if not redirect to login page
conikal_redirect_login();

// common donation form 
$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;

$item_per_page_setting = isset($campoal_options['conikal_item_per_page_field']) ? $campoal_options['conikal_item_per_page_field'] : '';
$item_per_page = $item_per_page_setting != '' ? $item_per_page_setting : 20;

$type = isset($_GET['type']) ? sanitize_text_field($_GET['type']) : 'my_contributions';

// user dashboard link
$user_dashboard_page = isset($campoal_options['conikal_page_dashboard_user_field']) ? $campoal_options['conikal_page_dashboard_user_field'] : 0;
$user_dashboard_page = ($user_dashboard_page ? get_permalink($user_dashboard_page) : '');

// get contributions
if ($type == 'my_contributions') {
	$contributions = give_get_users_donations($current_user->ID, $item_per_page, true, 'any');
	$total = give_get_donation_stats_by_user($current_user->ID);
	$total = array_key_exists('purchases', $total) ? $total['purchases'] : 0;
	$pages = ceil($total / $item_per_page); 
} else {
	$my_donation_forms = conikal_give_donations_by_author($current_user->ID, -1);

	if ($my_donation_forms->have_posts()) {
		$contribution_ids = array();
		while( $my_donation_forms->have_posts() ) {
			$my_donation_forms->the_post();
			array_push($contribution_ids, get_the_ID());
		}

		$args = array(
	        'give_forms' => $contribution_ids,
	        'order' => 'DESC'
	    );

	    $contributions = give_get_payments( $args );

	    // paination calulate
		$total = count( $contributions ); //total items in array    
		$limit = $item_per_page; //per page    
		$pages = ceil( $total / $limit ); //calculate total pages
		$paged = max($paged, 1); //get 1 page when $_GET['page'] <= 0
		$paged = min($paged, $pages); //get last page when $_GET['page'] > $totalPages
		$offset = ($paged - 1) * $limit;
		if( $offset < 0 ) $offset = 0;

	    $contributions = array_slice( $contributions, $offset, $limit );
	} else {
		$contributions = false;
	}
}

$lazy_placeholder_avatar = conikal_lazy_load_placeholder('avatar');
?>
<div class="ui hidden divider"></div>
<div class="ui middle aligned grid">
	<div class="ten wide column">
		<h1 class="ui header submit-petition-title">
	        <div class="content">
	        	<?php esc_html_e('Donations', 'campoal'); ?>
	        </div>
	    </h1>
	</div>
	<div class="six wide right aligned column">
		<button class="ui icon basic button" id="dashboard-sidebar-btn"><i class="sidebar icon"></i></button>
	</div>
</div>
<div class="ui hidden divider"></div>

<div class="ui pointing secondary menu">
  	<a href="<?php echo esc_url(add_query_arg(array('section' => 'contributions', 'type' => 'my_contributions'), $user_dashboard_page)) ?>" class="item<?php echo esc_attr($type == 'my_contributions' ? ' active' : '') ?>" data-tab="followers"><?php esc_html_e('My Donations', 'campoal') ?></a>
  	<?php if (empty($common_donation_form) && empty($common_form_id)) { ?>
  		<a href="<?php echo esc_url(add_query_arg(array('section' => 'contributions', 'type' => 'supporter_contributions'), $user_dashboard_page)) ?>" class="item<?php echo esc_attr($type == 'supporter_contributions' ? ' active' : '') ?>" data-tab="following"><?php esc_html_e("Supporter's Donations", 'campoal') ?></a>
	<?php } ?>
</div>

<?php if ($contributions) : ?>
	<table class="ui very basic celled very padded table">
	  	<thead>
	    <tr>
	    	<th>
	    		<div class="ui grid computer only">
	    			<div class="sixteen wide mobile four wide computer column"><?php esc_html_e('User', 'campoal') ?></div>
	    			<div class="sixteen wide mobile seven wide computer column"><?php esc_html_e('Campaign', 'campoal') ?></div>
	    			<div class="eight wide mobile two wide computer column"><?php esc_html_e('Amount', 'campoal') ?></div>
	    			<div class="four wide mobile two wide computer column"><?php esc_html_e('Status', 'campoal') ?></div>
	    			<div class="four wide mobile one wide computer center aligned column"><?php esc_html_e('Receipt', 'campoal') ?></div>
	    		</div>	
	    	</th>
	  	</tr>
	 	</thead>
	  	<tbody>
		<?php foreach ($contributions as $contribution) {
			$payment    	= new Give_Payment( $contribution->ID );
			$gateway 		= give_get_payment_gateway(  $payment->ID );
			$payment_date   = strtotime( $payment->date );
			$receipt_url 	= esc_url(add_query_arg(array('section' => 'donation-receipt', 'donation_id' => $contribution->ID, 'payment_id' => give_get_payment_key( $payment->ID )), $user_dashboard_page));

			// get petition link
			$petition = conikal_give_petition_by_form_id($payment->form_id);
			if ($petition->have_posts()) {
				$petition = $petition->posts[0];
				$petition_link = get_permalink($petition->ID);
			} else {
				$petition_link = get_permalink($payment->form_id);
			}

			// get user data
			$user = get_user_by('ID', $payment->user_id);
        	if ($user) {
                $user_id 			= $user->ID;
	            $user_name 			= $user->display_name;
	            $author_url 		= get_author_posts_url($user_id);
	            $user_avatar 		= conikal_get_avatar_url( $user_id, array('size' => 64) );
				$user_avatar_retina = conikal_get_avatar_url( $user_id, array('size' => 128) );
	        } else {
	        	$author_url = '';
	        	$user_name 			= $payment->first_name . ' ' . $payment->last_name;
	        	$user_avatar 		= conikal_get_avatar_url( 0, array('size' => 64) );
				$user_avatar_retina = conikal_get_avatar_url( 0, array('size' => 128) );
	        }
		?>
		<tr>
	    	<td>
		    	<div class="ui middle aligned grid">
		    		<div class="sixteen wide mobile four wide computer column">
		    			<h4 class="ui image header">
					        <img class="ui mini circular image lazy" data-src="<?php echo esc_url($user_avatar); ?>" data-retina="<?php echo esc_url($user_avatar_retina); ?>" src="<?php echo esc_url($lazy_placeholder_avatar); ?>" alt="<?php echo esc_attr($user_name) ?>">
					        <div class="content">
					            <a href="<?php echo esc_url($author_url); ?>" class="header" data-bjax><?php echo esc_html($user_name) ?></a>
					            <div class="sub header">
					            	<?php echo date_i18n( give_date_format(), $payment_date ); ?>
					         	</div>
					        </div>
				      	</h4>
		    		</div>
		    		<div class="sixteen wide mobile seven wide computer column">
		    			<h4 class="ui header">
    						<a href="<?php echo esc_url($petition_link) ?>" data-bjax><?php echo esc_html($payment->form_title) ?></a>
    					</h4>
		    		</div>
		    		<div class="eight wide mobile two wide computer column">
		    			<h5 class="ui primary header">
						  	<?php echo give_currency_filter( give_format_amount( $payment->total, array( 'sanitize' => false ) ) ); ?>
						  	<div class="sub header text grey"><?php echo esc_html__('via', 'campoal') . ' ' . give_get_gateway_admin_label( $gateway ); ?></div>
						</h5>
		    		</div>
		    		<div class="four wide mobile two wide computer column">
						<?php echo esc_html($payment->status_nicename) ?>
		    		</div>
		    		<div class="four wide mobile one wide computer center aligned column">
		    			<a href="<?php echo esc_url($receipt_url) ?>"><i class="eye icon"></i></a>
		    		</div>
		    	</div>
	    	</td>
	    </tr>
		<?php } ?>

		</tbody>
	  	<tfoot>
	    	<tr>
				<th colspan="5">
				<!-- PAGINATION -->
				<?php conikal_pagination_buttons(_n('Donation', 'Donations', $total, 'campoal'), $pages, $total, $item_per_page) ?>
	    		</th>
			</tr>
		</tfoot>
	</table>
<?php else :
    // display not found post notice
    conikal_not_found_post(__('No contributions found.', 'campoal'), __("We're sorry, but no contributions has been done yet.", 'campoal'));
endif; ?>