<?php
/*
Template Name: Supporters list
*/

/**
 * @package WordPress
 * @subpackage Campoal
 */

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

get_header();

$supporter_per_page_setting = isset($campoal_options['conikal_supporter_per_page_field']) ? $campoal_options['conikal_supporter_per_page_field'] : '';
$supporter_per_page = $supporter_per_page_setting != '' ? $supporter_per_page_setting : 12;

$hidden_amount_contribute = isset($campoal_options['conikal_amount_contribute_field']) ? $campoal_options['conikal_amount_contribute_field'] : 0;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
?>
<div id="wrapper" class="wrapper petition">
<?php 
if (isset($_GET['id']) && $_GET['id'] != '') {
    $petition_id 	= isset($_GET['id']) ? sanitize_text_field($_GET['id']) : '';
    $type 			= isset($_GET['type']) ? sanitize_text_field($_GET['type']) : '';

    // get petition of update
    $title 				= get_the_title($petition_id);
    $link 				= get_permalink($petition_id);
    $gallery 			= get_post_meta($petition_id, 'petition_gallery', true);
    $images 			= conikal_get_gallery_images($gallery);
    $thumb_id 			= get_post_thumbnail_id($petition_id);
    $thumbnail 			= wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
    $thumbnail_small 	= wp_get_attachment_image_src($thumb_id, 'conikal-petition-small', true);
    $thumb_placeholder 	= get_post_meta($petition_id, 'petition_thumb', true);
    $thumb 				= get_post_meta($petition_id, 'petition_thumb_small', true);
    $supporter_number 	= conikal_get_supporter_donors_number($petition_id);
    $goal 				= $supporter_number->goal;
    $sign_num 			= $supporter_number->sign;

    $author_id 			= get_post_field('post_author', $petition_id);
    $author_name 		= conikal_get_author_name($author_id);

    // get form id
	if (get_post_type($petition_id) == 'petition') {
		$form_id = get_post_meta($petition_id, 'petition_contribute', true);
		$contribute_approve = get_post_status($form_id);
		if ($contribute_approve === 'pending') {
			$form_id = '';
		}
	} else {
		$form_id = $petition_id;
	}

    // get post thumbnail
    $thumbnail_small = conikal_get_petition_thumbnail($petition_id, 'conikal-petition-small', 'thumbnail');

    // lazy placeholder
    $lazy_placeholder_avatar = conikal_lazy_load_placeholder('avatar');
    $lazy_placeholder = conikal_lazy_load_placeholder('thumbnail');
?>
	<!-- BACK PETITION -->
    <div class="ui secondary vertical segment">
        <div class="ui container">
            <div class="ui grid">
                <div class="sixteen wide column tablet computer only">
                    <div class="ui link items">
                        <a class="item" href="<?php echo esc_url($link) ?>" data-bjax>
							<?php if (!empty($thumb_id)) : ?>
								<div class="ui tiny image">
									<img class="ui fluid rounded image lazy" data-src="<?php echo esc_url($thumbnail_small) ?>" src="<?php echo esc_url($lazy_placeholder) ?>" alt="<?php echo esc_attr($title) ?>">
								</div>
							<?php endif; ?>
                            <div class="content">
                                <div class="header"><?php echo esc_html($title) ?></div>
                                <div class="meta">
                                    <?php esc_html_e('by', 'campoal') ?> <strong><?php echo esc_html($author_name) ?></strong>
                                    <span><?php if ($type == 'donor') {
										echo ' · '  . esc_html(conikal_format_number('%!,0i', give_get_form_sales_stats( $form_id ))) . ' ' . esc_html__('Backers', 'campoal');
									} else {
										echo ' · ' . sprintf($supporter_number->label, esc_html(conikal_format_number('%!,0i', $sign_num, true)));
									} ?></span>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                <div class="sixteen wide column mobile only">
                    <div class="ui link items">
                        <a class="item" href="<?php echo esc_url($link) ?>" title="<?php echo esc_attr($title) ?>" data-bjax>
                            <div class="content">
                                <div class="header"><i class="chevron left icon"></i><?php echo esc_html($title) ?></div>
                            </div>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="ui hidden divider"></div>

	<!-- SUPPORTER LIST CONTENT -->
	<?php get_template_part('templates/dashboard/supporters-list'); ?>
<?php
} else {
	get_template_part('404');
}
wp_nonce_field('follow_ajax_nonce', 'securityFollow', true);
?>
</div>


<?php get_footer(); ?>
