<?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;

$signed_petitions = conikal_signed_petitions($current_user->ID, $item_per_page);
if($signed_petitions) {
    $total = $signed_petitions->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('Signed Petitions', '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($signed_petitions) : ?>
<table class="ui very basic celled striped table">
  	<thead>
    <tr>
    	<th>
    		<div class="ui grid computer only">
    			<div class="sixteen wide mobile eleven wide computer column"><?php esc_html_e('Campaign', 'campoal') ?></div>
    			<div class="ten wide mobile three wide computer column"><?php esc_html_e('Progress', '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"><?php esc_html_e('Action', 'campoal') ?></div>
    		</div>	
    	</th>
  	</tr>
 	</thead>
  	<tbody>
  	<?php
  		while( $signed_petitions->have_posts() ) : $signed_petitions->the_post();
            $id = get_the_ID();
            $approved = get_post_status($id);
            $link = get_permalink($id);
            $title = get_the_title($id);
            $category =  wp_get_post_terms($id, 'petition_category', true);
            $excerpt = conikal_get_excerpt_by_id($id);
            $comments = wp_count_comments($id);
            $gallery = get_post_meta($id, 'petition_gallery', true);
            $images = conikal_get_gallery_images($gallery);
            $address = get_post_meta($id, 'petition_address', true);
            $city = get_post_meta($id, 'petition_city', true);
            $state = get_post_meta($id, 'petition_state', true);
            $neighborhood = get_post_meta($id, 'petition_neighborhood', true);
            $zip = get_post_meta($id, 'petition_zip', true);
            $country = get_post_meta($id, 'petition_country', true);
            $lat = get_post_meta($id, 'petition_lat', true);
            $lng = get_post_meta($id, 'petition_lng', true);
            $receiver = get_post_meta($id, 'petition_receiver', true);
            $receiver = explode(',', $receiver);
            $position = get_post_meta($id, 'petition_position', true);
            $position = explode(',', $position);
            $supporter_number = conikal_get_supporter_donors_number($id);
            $goal = $supporter_number->goal;
            $sign = $supporter_number->sign;
            $updates = get_post_meta($id, 'petition_update', true);
            $thumb = get_post_meta($id, 'petition_thumb_small', true);
            $status = get_post_meta($id, 'petition_status', true);

            // get post thumbnail
            $thumbnail = conikal_get_petition_thumbnail($id);
            $lazy_placeholder = conikal_lazy_load_placeholder('thumbnail-small');

            // petition status label
            switch ($status) {
            	case 0:
                    $status_label = esc_html__('Open', 'campoal');
                    $status_color = 'primary';
                    if ($sign >= $goal) {
                        $status_label = esc_html__('Victory', 'campoal');
                        $status_color = 'status-victory';
                    }
                    break;

                case 1:
                    $status_label = esc_html__('Victory', 'campoal');
                    $status_color = 'status-victory';
                    break;
            	
            	default:
            		$status_label = esc_html__('Close', 'campoal');
            		$status_color = 'grey';
            		break;
            }
    ?>

    <tr>
    	<td>
	    	<div class="ui middle aligned grid">
	    		<div class="sixteen wide mobile eleven 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($supporter_number->label, esc_html(conikal_format_number('%!,0i', $sign, true))); ?>
                                <?php echo ' · <i class="comments icon"></i>' . conikal_format_number('%!,0i', $comments->approved, true) . ' ' . esc_html__('comments', 'campoal'); ?>
                                <?php if($category && !wp_is_mobile()) { ?>
                                <a href="<?php echo get_term_link($category[0]->term_id) ?>" data-bjax>
                                    <?php echo ' · <i class="tag icon"></i>' . esc_html($category[0]->name); ?>
                                </a>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
	    		</div>
	    		<div class="ten wide mobile three wide computer left aligned column">
	    			<div class="ui tiny indicating primary progress petition-goal" data-value="<?php echo esc_attr($sign) ?>" data-total="<?php echo esc_attr($status == '1' ? $sign : $goal) ?>">
                        <div class="bar">
                            <div class="progress"></div>
                        </div>
                    </div>
	    		</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">
                    <a href="<?php echo esc_url($link) ?>" data-bjax><i class="eye icon"></i></a>
	    		</div>
	    	</div>
    	</td>
    </tr>
    <?php endwhile; ?>
    </tbody>
    <tfoot>
        <tr>
            <th colspan="5">
            <!-- PAGINATION -->
            <?php conikal_pagination_buttons(_n('Petition', 'Petitions', $total, 'campoal'), $signed_petitions->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 signed yet.", 'campoal'));
endif; ?>