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

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

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

$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;
$page_dashboard_petition    = isset($campoal_options['conikal_page_dashboard_petition_field']) ? $campoal_options['conikal_page_dashboard_petition_field'] : 0;
$page_dashboard_petition    = $page_dashboard_petition ? get_permalink($page_dashboard_petition) : '';

$my_fundraisings = conikal_search_give_forms(false, $current_user->ID, $item_per_page);
if($my_fundraisings->have_posts()) {
    $total = $my_fundraisings->found_posts;
} else {
    $total = 0;
}

?>
<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('My Fundraising', '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>

<?php if($my_fundraisings->have_posts()) : ?>
<table class="ui very basic celled striped table">
  	<thead>
    <tr>
    	<th>
    		<div class="ui grid computer only">
    			<div class="sixteen wide mobile nine wide computer column"><?php esc_html_e('Campaign', 'campoal') ?></div>
    			<div class="seven wide mobile two wide computer center aligned column"><?php esc_html_e('Raised', 'campoal') ?></div>
                <div class="seven wide mobile two wide computer center aligned column"><?php esc_html_e('Goal', 'campoal') ?></div>
    			<div class="three wide mobile one wide computer center aligned column"><?php esc_html_e('Status', 'campoal') ?></div>
    			<div class="three wide mobile one wide computer center aligned column"></div>
    			<div class="three wide mobile one wide computer center aligned column"><?php esc_html_e('Action', 'campoal') ?></div>
    		</div>	
    	</th>
  	</tr>
 	</thead>
  	<tbody>
  	<?php
  		while( $my_fundraisings->have_posts() ) : $my_fundraisings->the_post();

            $give_prefix        = '_give_';

            $form_id            = get_the_ID();
            $petition_id        = get_post_meta($form_id, $give_prefix . 'petition_id', true);
            $approved           = get_post_status($form_id);
            $backers            = give_get_form_sales_stats( $form_id );
            $link               = get_permalink($form_id);
            $title              = get_the_title($form_id);
            $category           = wp_get_post_terms($form_id, 'give_forms_category', true);
            $content            = get_post_meta($form_id, $give_prefix . 'form_content', true);
            $excerpt            = conikal_get_excerpt_by_id($form_id, 12, $content);
        
            // lazy placeholder
            $thumbnail                  = conikal_get_petition_thumbnail($form_id, 'conikal-petition-thumbnail', false);
            $lazy_placeholder           = conikal_lazy_load_placeholder('thumbnail-small');

            $post_views                 = conikal_format_number('%!,0i', (int) conikal_get_post_views($form_id), true);
            $goal_option                = get_post_meta($form_id, $give_prefix . 'goal_option', true);

            // use petition thumbnail if donation form doesn't exist thumbnail
            if (!$thumbnail && !empty($petition_id)) {
                $thumbnail          = conikal_get_petition_thumbnail($petition_id, 'conikal-petition-thumbnail', false);
                if(!is_array($category) || !isset($category[0])) {
                    $category           = wp_get_post_terms($petition_id, 'petition_category', true);
                }
            }

            $fundraising_goal = '-';
            if (give_is_setting_enabled( $goal_option )) {
                $goal_stats       = give_goal_progress_stats( $form_id );
                $fundraising_goal = give_currency_filter( give_format_amount( $goal_stats['raw_goal'], array( 'sanitize' => false ) ) );
            }
            
            $total_incom = give_get_form_earnings_stats( $form_id );
            $total_incom = give_currency_filter( give_format_amount( $total_incom, array( 'sanitize' => false ) ) );

            // petition approve label
            switch ($approved) {
            	case 'publish':
            		$approved_label = esc_html__('Publish', 'campoal');
            		$approved_color = 'green';
            		break;

            	case 'pending':
            		$approved_label = esc_html__('Pending', 'campoal');
            		$approved_color = 'yellow';
            		break;
            	
            	default:
            		$approved_label = esc_html__('Closed', 'campoal');
            		$approved_color = 'grey';
            		break;
            }

            // petition status label
            $status_label = esc_html__('Open', 'campoal');
            $status_color = 'primary';
            if (give_is_setting_enabled( $goal_option )) {
                if ($goal_stats['raw_goal'] != 0 && $goal_stats['raw_actual'] >= $goal_stats['raw_goal']) {
                    $status_label = esc_html__('Victory', 'campoal');
                    $status_color = 'green';
                }
            }
    ?>

    <tr>
    	<td>
	    	<div class="ui middle aligned grid">
	    		<div class="sixteen wide mobile nine wide computer column">
	    			<div class="ui grid">
	    				<div class="sixteen wide mobile two wide computer column">
			        		<img class="ui fluid rounded image lazy" data-src="<?php echo esc_url($thumbnail) ?>" src="<?php echo esc_url($lazy_placeholder) ?>" alt="<?php echo esc_attr($title) ?>">
	    				</div>
	    				<div class="sixteen wide mobile fourteen wide computer middle aligned column">
	    					<h3 class="ui header">
	    						<a href="<?php echo esc_url($link) ?>" data-bjax><?php echo esc_html($title) ?></a>
	    					</h3>
                            <div class="sub header text grey">
                                <?php echo conikal_custom_icon('supporter') . sprintf(esc_html__('%s backers', 'campoal'), conikal_format_number('%!,0i', $backers, true)); ?>
                                <?php if(is_array($category) && isset($category[0]) && !wp_is_mobile()) { ?>
                                <a href="<?php echo get_term_link($category[0]->term_id) ?>" data-bjax>
                                    <?php echo ' · ' . esc_html($category[0]->name); ?>
                                </a>
                                <?php } ?>
                                <?php echo ' · ' . sprintf(esc_html__('%s views', 'campoal'), $post_views); ?>
                            </div>
	    				</div>
	    			</div>
	    		</div>
                <div class="seven wide mobile two wide computer center aligned column">
	    			<h5 class="ui primary header">
	    				<?php echo esc_html($total_incom) ?>
	    			</h5>
                </div>
                <div class="seven wide mobile two wide computer center aligned column">
                    <h5 class="ui primary header">
                        <?php echo esc_html($fundraising_goal) ?>
                    </h5>
                </div>
	    		<div class="three wide mobile one wide computer center aligned column">
	    			<span class="ui <?php echo esc_attr($approved_color) ?> empty circular label status-label" data-inverted="" data-content="<?php echo esc_attr($approved_label) ?>" data-position="top center"></span>
	    		</div>
	    		<div class="three wide mobile one wide computer center aligned column">
	    			<span class="ui <?php echo esc_attr($status_color) ?> empty circular label status-label" data-inverted="" data-content="<?php echo esc_attr($status_label) ?>" data-position="top center"></span>
	    		</div>
	    		<div class="three wide mobile one wide computer center aligned column">
	    			<?php
			        // edit petition link
                    $page_contribute = add_query_arg(array('section' => 'contribute', 'id' => $form_id), $page_dashboard_petition);
			        if ($page_contribute) { ?>
			            <a href="<?php echo esc_url($page_contribute); ?>" data-bjax><i class="write icon"></i></a>
			        <?php } ?>
	    		</div>
	    	</div>
    	</td>
    </tr>
    <?php endwhile; ?>
  	</tbody>
        <tfoot>
            <tr>
                <th colspan="5">
                <!-- PAGINATION -->
                <?php conikal_pagination_buttons(_n('Petition', 'Petitions', $total, 'campoal'), $my_fundraisings->max_num_pages, $total, $item_per_page) ?>
                </th>
            </tr>
        </tfoot>
</table>
<?php else :
    // display not found post notice
    conikal_not_found_post(__('No campaigns found.', 'campoal'), __("We're sorry, but no campaigns have been started yet.", 'campoal'));
endif; ?>