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

// 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) : '');

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

if ($type == 'petition') {
    $bookmarks = conikal_bookmark_petitions($current_user->ID, $item_per_page);
    $user_bookmarks = get_user_meta($current_user->ID, 'petition_bookmark', true);
} else {
    $bookmarks = conikal_bookmark_project($current_user->ID, $item_per_page);
    $user_bookmarks = get_user_meta($current_user->ID, 'project_bookmark', true);
}

if ($bookmarks) {
    $total = $bookmarks->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('Bookmarks', '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' => 'bookmarks', 'type' => 'petition'), $user_dashboard_page)) ?>" class="item<?php echo esc_attr($type == 'petition' ? ' active' : '') ?>" data-tab="petition"><?php esc_html_e('Petitions', 'campoal') ?></a>
    <?php if (class_exists('Give')) { ?>
		<a href="<?php echo esc_url(add_query_arg(array('section' => 'bookmarks', 'type' => 'project'), $user_dashboard_page)) ?>" class="item<?php echo esc_attr($type == 'project' ? ' active' : '') ?>" data-tab="project"><?php esc_html_e('Projects', 'campoal') ?></a>
	<?php } ?>
</div>

<?php if($bookmarks) : ?>
<table class="ui very basic celled striped table">
  	<thead>
    <tr>
    	<th>
    		<div class="ui grid computer only">
    			<div class="sixteen wide mobile ten wide computer column"><?php esc_html_e('Campaign', 'campoal') ?></div>
                <?php if ($type == 'petition') { ?>
    			    <div class="ten wide mobile four wide computer column"><?php esc_html_e('Progress', 'campoal') ?></div>
                <?php } else { ?>
                    <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>
                <?php } ?>
    			<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( $bookmarks->have_posts() ) {
            $bookmarks->the_post();

            if ($type == 'petition') {
                $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);
                $comments           = wp_count_comments($id);
                $supporter_number   = conikal_get_supporter_donors_number($id);
                $goal               = $supporter_number->goal;
                $sign               = $supporter_number->sign;
                $updates            = get_post_meta($id, 'petition_update', 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;
                }
            } else {
                $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);
            
                // 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 ten 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 if ($type == 'petition') : ?>
                                    <?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 } ?>
                                <?php else : ?>
                                    <?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); ?>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
                <?php if ($type == 'petition') { ?>
                    <div class="ten wide mobile four 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>
                <?php } else { ?>
                    <div class="five wide mobile two wide computer center aligned column">
                        <h5 class="ui primary header">
                            <?php echo esc_html($total_incom) ?>
                        </h5>
                    </div>
                    <div class="five wide mobile two wide computer center aligned column">
                        <h5 class="ui primary header">
                            <?php echo esc_html($fundraising_goal) ?>
                        </h5>
                    </div>
                <?php } ?>
                <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 if(in_array($id, $user_bookmarks) === false) {
                        print '<a href="javascript:void(0)" class="bookmark-popup bookmarkPetition bookmarkBtn" id="bookmark-' . esc_attr($id) . '" data-id="' . esc_attr($id) . '" data-type="' . esc_attr($type) . '" data-content="' . esc_attr__('Save this campaign', 'campoal') . '" data-position="top center"><i class="bookmark outline icon"></i></a>';
                    } else {
                        print '<a href="javascript:void(0)" class="bookmark-popup bookmarkedPetition bookmarkBtn" id="bookmark-' . esc_attr($id) . '" data-id="' . esc_attr($id) . '" data-type="' . esc_attr($type) . '" data-content="' . esc_attr__('Unsave this campaign', 'campoal') . '" data-position="top center" ><i class="bookmark icon"></i></a>';
                    } ?>
                </div>
            </div>
        </td>
    </tr>

    <?php } ?>
    </tbody>
    <tfoot>
        <tr>
            <th colspan="5">
            <!-- PAGINATION -->
            <?php conikal_pagination_buttons(_n('Petition', 'Petitions', $total, 'campoal'), $bookmarks->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 bookmarked yet.", 'campoal'));
endif; ?>
<?php wp_nonce_field('bookmark_ajax_nonce', 'securityBookmark', true); ?>