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

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

$view_counter = isset($campoal_options['conikal_view_counter_field']) ? $campoal_options['conikal_view_counter_field'] : 1;
$updates_per_page = isset($campoal_options['conikal_updates_per_page_field']) ? $campoal_options['conikal_updates_per_page_field'] : 5;
$similar = isset($campoal_options['conikal_similar_field']) ? $campoal_options['conikal_similar_field'] : 0;
$petition_comment = isset($campoal_options['conikal_petition_comment_field']) ? $campoal_options['conikal_petition_comment_field'] : 1;
$external_link = isset($campoal_options['conikal_external_link_field']) ? $campoal_options['conikal_external_link_field'] : 0;
$editor_style = isset($campoal_options['conikal_editor_style_field']) ? $campoal_options['conikal_editor_style_field'] : 'markdown';
$petition_date = isset($campoal_options['conikal_petition_date_field']) ? $campoal_options['conikal_petition_date_field'] : 1;

$update_id              = $post->ID;
$update_title           = get_the_title($update_id);
$update_content         = get_the_content($update_id);
$update_excerpt         = conikal_get_excerpt_by_id($update_id);
$update_date            = get_the_date('', $update_id);
$update_type            = get_post_meta($update_id, 'update_type', true);
$update_media           = get_post_meta($update_id, 'update_media', true);
$update_gallery         = get_post_meta($update_id, 'update_gallery', true);
$update_images          = explode('~~~', $update_gallery);
$update_thumb_id        = get_post_thumbnail_id($update_id);
$update_thumbnail       = wp_get_attachment_image_src($update_thumb_id, 'thumbnail', true);
$update_thumbnail_large = wp_get_attachment_image_src($update_thumb_id, 'large', true);
$update_video           = get_post_meta($update_id, 'update_video', true);
$update_thumb           = get_post_meta($update_id, 'update_thumb', true);
$update_time 		    = human_time_diff(get_the_time('U', $update_id), current_time('timestamp'));

$update_author_name            = conikal_get_author_name($post->post_author);
$update_author_link            = get_author_posts_url($post->post_author, get_the_author_meta('user_nicename'));
$update_author_avatar          = conikal_get_avatar_url( $post->post_author, array('size' => 64) );
$update_author_avatar_retina   = conikal_get_avatar_url( $post->post_author, array('size' => 128) );

$graph_title            = get_post_meta($update_id, 'update_graph_title', true);
$graph_description      =  get_post_meta($update_id, 'update_graph_description', true);
$graph_image            =  get_post_meta($update_id, 'update_graph_image', true);
$graph_site             =  get_post_meta($update_id, 'update_graph_site', true);

// get petition of update
$petition_id            = get_post_meta($update_id, 'update_post_id', true);
$post_type              = get_post_type($petition_id);           
$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_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;
$time 				    = human_time_diff(get_the_time('U', $petition_id), current_time('timestamp'));

$author_id              = get_post_field('post_author', $petition_id);
$author_name            = conikal_get_author_name($author_id);
$author_link            = get_author_posts_url($author_id, get_the_author_meta('user_nicename'));
$author_avatar          = conikal_get_avatar_url( $author_id, array('size' => 64) );
$author_avatar_retina   = conikal_get_avatar_url( $author_id, array('size' => 128) );

$user_country   = get_user_meta($author_id, 'user_country', true);
$user_city      = get_user_meta($author_id, 'user_city', true);
$user_state     = get_user_meta($author_id, 'user_state', true);

// get form option
$form_id = get_post_meta($petition_id, 'petition_contribute', true);
$contribute_approve = get_post_status($form_id);
if ($contribute_approve != 'publish') {
    $form_id = '';
}

// get thumbnail of give form
if ($post_type === 'give_forms') {
    $give_prefix        = '_give_';

    $form_id            = get_post_meta($update_id, 'update_post_id', true);
    $gallery            = get_post_meta($form_id, $give_prefix . 'gallery', true);
    $images             = conikal_get_gallery_images($gallery);
    $thumb_id           = get_post_thumbnail_id($form_id);
    $thumbnail          = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
    $video              = get_post_meta($form_id, $give_prefix . 'video', true);
    $thumb_placeholder  = get_post_meta($form_id, $give_prefix . 'thumb', true);
    $thumb              = get_post_meta($form_id, $give_prefix . 'thumb_small', true);
    $reveal_label 		= get_post_meta($form_id, $give_prefix . 'reveal_label', true);
}

// get fundraising mode setting
$fundraising_mode = conikal_fundraising_mode_setting($petition_id);

// get petition citation status
$citation = conikal_citation_status($petition_id);

// get type title update
$update_type_title = conikal_get_update_type($update_type);

// Show 404 page and exit if Give class is not exist
if ($post_type === 'give_forms' && !class_exists('Give')) {
    get_template_part( 404 ); 
    exit();
}

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

// lazy placeholder
$lazy_placeholder_medium = conikal_lazy_load_placeholder('thumbnail');
$lazy_placeholder = conikal_lazy_load_placeholder('thumbnail-small');

// Class used to render markdown content into HTML
$Parsedown = new Parsedown();

// set view
conikal_set_post_views($update_id);

// lazy placeholder
$lazy_placeholder_avatar = conikal_lazy_load_placeholder('avatar');
?>

<div id="wrapper" class="wrapper read update">
    <!-- 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>
                            <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_medium) ?>" alt="<?php echo esc_attr($title) ?>">
                            </div>
                            <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>
                                    <?php if ($post_type != 'give_forms') { ?>
                                        <span><?php echo ' · ' . sprintf($supporter_number->label, esc_html(conikal_format_number('%!,0i', $sign_num, true))); ?></span>
                                    <?php } ?>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                <div class="sixteen wide column mobile only back-to-petition">
                    <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>

    <div class="ui container">
        <div class="ui relaxed grid">
            <div class="sixteen wide mobile nine wide tablet ten wide computer column" id="content<?php echo esc_attr($petition_id) ?>">
                <!-- UPDATE CONTENT -->
                <div id="main-petition">
                    <div class="ui basic vertical segment">
                        <div class="ui huge header update-title">
                            <div class="content">
                                <div class="sub header"><i class="<?php echo esc_attr($update_type_title['icon']) ?>"></i><?php echo esc_html($update_type_title['text']) ?></div>
                                <h1 class="entry-title"><?php the_title() ?></h1>
                            </div>
                        </div>

                        <!-- THUMBNAIL UPDATE -->
                        <?php
                            if (has_post_thumbnail($update_id)) { ?>
                                <img class="ui fluid rounded image lazy" data-src="<?php echo esc_url($update_thumbnail_large[0]) ?>" src="<?php echo esc_url($lazy_placeholder_medium) ?>" alt="<?php echo esc_attr($title) ?>">
                            <?php } elseif ($update_video) { ?>
                                <div class="ui embed media-embed" id="media-embed"></div>
                                <input type="hidden" class="video-url" id="video-url" value="<?php echo esc_attr($update_video) ?>">
                                <input type="hidden" class="thumb-url"id="thumb-url" value="<?php echo esc_attr($thumb_placeholder) ?>">
                            <?php } elseif ($update_gallery) { ?>
                                <img class="ui fluid rounded image" data-src="<?php echo esc_url($update_images[1]) ?>" src="<?php echo esc_url($lazy_placeholder_medium) ?>" alt="<?php echo esc_attr($title) ?>">
                        <?php } ?>

                        <!-- VIEW COUNTER -->
                        <div class="ui basic vertical segment">
                            <div class="ui grid">
                                <div class="eleven wide column update-date">
                                    <a class="truncate" href="<?php echo esc_url($update_author_link) ?>" data-bjax>
                                        <img class="ui avatar image lazy" data-src="<?php echo esc_url($update_author_avatar); ?>" data-retina="<?php echo esc_url($update_author_avatar_retina); ?>" src="<?php echo esc_url($lazy_placeholder_avatar); ?>" alt="<?php echo esc_attr($update_author_name) ?>">
                                        <strong>
                                            <?php echo esc_html($update_author_name) ?>
                                        </strong>
                                        <span class="text grey">
                                                <?php echo sprintf(esc_html__('post this %s ago', 'campoal'), esc_html($update_time)) ?>
                                        </span>
                                    </a>
                                </div>
                                <div class="five wide right aligned column update-view">
                                    <?php if ($view_counter == 1 && function_exists('conikal_get_post_views')) { ?>
                                        <div class="text grey">
                                            <i class="eye icon"></i>
                                            <?php echo conikal_format_number('%!,0i', (int) conikal_get_post_views($update_id), true) . esc_html__(' view', 'campoal') ?></div>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>

                        <!-- MAIN CONTENT UPDATE -->
                        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                            <div class="entry-content">
                                <?php if ($editor_style == 'markdown') {
                                    echo wp_kses_post($Parsedown->text(get_the_content()));
                                } else {
                                    the_content();
                                } ?>
                                <?php echo apply_filters( 'the_content', '') ?>
                                <?php //the_content(); ?>
                                <div class="clearfix"></div>
                                <?php wp_link_pages( array(
                                    'before'      => '<div class="page-links">',
                                    'after'       => '</div>',
                                    'link_before' => '<span>',
                                    'link_after'  => '</span>',
                                    'pagelink'    => '%',
                                    'separator'   => '',
                                ) ); ?>
                            </div>

                            <!-- MEDIA LINK -->
                            <?php if ($update_media) { ?>
                            <div class="update-link-section">
                                <a href="<?php echo esc_url($update_media) ?>" target="<?php echo esc_attr($external_link != 0 ? '_blank' : '_self') ?>">
                                    <div class="ui secondary segment">
                                        <div class="ui grid">
                                            <div class="sixteen wide mobil four wide computer column">
                                                <!-- THUMBNAIL UPDATE -->
                                                <?php
                                                    if ($graph_image) {?>
                                                        <img class="ui fluid rounded image" src="<?php echo esc_url($graph_image) ?>" alt="<?php echo esc_attr($update_title) ?>">
                                                    <?php } else if ($update_gallery) { 
                                                        if (has_post_thumbnail($update_id)) { ?>
                                                        <img class="ui fluid rounded image" src="<?php echo esc_url($update_thumbnail[0]) ?>" alt="<?php echo esc_attr($update_title) ?>">
                                                        <?php } elseif ($update_gallery) { ?>
                                                        <img class="ui fluid rounded image" src="<?php echo esc_url($update_images[0]) ?>" alt="<?php echo esc_attr($update_title) ?>">
                                                        <?php } ?>
                                                    <?php } elseif ($update_video) { ?>
                                                        <div class="ui embed media-embed" id="media-embed"></div>
                                                        <input type="hidden" class="video-url" id="video-url" value="<?php echo esc_attr($update_video) ?>">
                                                        <?php if (!wp_is_mobile()) { ?>
                                                            <input type="hidden" class="thumb-url" id="thumb-url" value="<?php echo esc_attr($update_thumb) ?>">
                                                        <?php } ?>
                                                <?php } ?>
                                            </div>
                                            <div class="sixteen wide mobile twelve wide computer column">
                                                <h3 class="ui header">
                                                    <?php echo esc_html($graph_title ? $graph_title : $update_title) ?>
                                                </h3>
                                                <p class="text grey"><?php echo esc_html($graph_description ? $graph_description : $update_excerpt) ?></p>
                                                <p><?php echo esc_html($graph_site ? $graph_site : wp_parse_url($update_media, 1)) ?></p>
                                            </div>
                                        </div>
                                    </div>
                                </a>
                            </div>
                            <?php } ?>

                            <div class="ui vertical basic segment update-data-section">
                                <?php if ($petition_date == 1) : ?>
                                    <div class="ui right floated tiny header"><?php echo esc_html($update_date) ?></div>
                                <?php endif; ?>
                                <div class="clearfix"></div>
                            </div>
                        </div>

                        <div class="update-updates-section">
                            <div id="updates<?php echo esc_attr($petition_id) ?>" class="update-list" data-id="<?php echo esc_attr($petition_id) ?>" data-load="0">
                                <h2 class="ui dividing header"><?php esc_html_e('Updates', 'campoal') ?></h2>
                                <div id="update-list<?php echo esc_attr($petition_id) ?>"></div>
                                <div class="ui hidden divider"></div>
                                <div class="ui basic fluid button updates-more" id="updates-more<?php echo esc_attr($petition_id) ?>" data-offset="<?php echo esc_attr($updates_per_page) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><i class="long arrow down icon"></i><?php esc_html_e('More updates...', 'campoal') ?></div>
                            </div>

                            <!-- AUTHOR -->
                            <div class="ui secondary segment give-author-content">
                                <div class="ui small header">
                                    <img class="ui circular image lazy" data-src="<?php echo esc_url($author_avatar); ?>" data-retina="<?php echo esc_url($author_avatar); ?>" src="<?php echo esc_url($lazy_placeholder_avatar); ?>" alt="<?php echo esc_attr($author_name) ?>">
                                    <div class="content">
                                        <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' )); ?>" class="header truncate" data-bjax>
                                            <?php echo esc_html($author_name) ?>
                                        </a>
                                        <div class="sub header truncate">
                                            <strong>
                                                <?php if ($post_type == 'give_forms') {
                                                    esc_html_e('Started this project', 'campoal');
                                                } else {
                                                    esc_html_e('Started this campaign', 'campoal');
                                                } ?>
                                            </strong>
                                            <span class="text grey">
                                                <?php echo sprintf(esc_html__('%s ago', 'campoal'), esc_html($time)) ?>
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- COMMENT UPDATE -->
                <?php if(comments_open() && $petition_comment == 1) { ?>
                    <div class="update-comment-section">
                        <?php comments_template(); ?>
                    </div>
                <?php } ?>

            </div>
            <div class="sixteen wide mobile seven wide tablet six wide computer column <?php echo esc_attr($post_type != 'give_forms' ? 'tablet computer only' : ''); ?>">
                <!-- SIGN AND SHARE PETITION -->
                <?php if ($post_type != 'give_forms') {
                if (!wp_is_mobile()) {
                        // get sign petition section template
                        if (class_exists('Give') && $fundraising_mode == 1 && $form_id) {
                            get_template_part('templates/donate-section');
                        } else {
                            $sign_show_all = isset($campoal_options['conikal_sign_show_all_field']) ? $campoal_options['conikal_sign_show_all_field'] : 0;
                            get_template_part('templates/sign-share', ($sign_show_all == 1 ? 'all' : ''));
                        }
                    }
                } ?>

                <?php if ($post_type == 'give_forms') { ?>
                    <?php if (!wp_is_mobile()) : ?>
                        <?php get_template_part('templates/content/part/give-contribute'); ?>
                    <?php endif; ?>
                <?php } ?>
            </div>
        </div>
    </div>
</div>

<?php if ($post_type != 'give_forms') { 
    if (wp_is_mobile()) {
        if (class_exists('Give') && $fundraising_mode == 1 && $form_id != '') {
            get_template_part('templates/donate-section', 'mobile');
        } else {
            // get sign petition section template
            $sign_show_all = isset($campoal_options['conikal_sign_show_all_field']) ? $campoal_options['conikal_sign_show_all_field'] : 0;
            $mobile_sign_style = isset($campoal_options['conikal_mobile_sign_style_field']) ? $campoal_options['conikal_mobile_sign_style_field'] : 'button';
            get_template_part('templates/sign-share-mobile', ($sign_show_all == 1 && $mobile_sign_style == 'sidebar' ? 'all' : ''));
        }
    }
} ?>

<?php if ($post_type == 'give_forms') { ?>
    <!-- DONATION MOBILE BUTTON -->
	<?php if (!wp_is_mobile()) : ?>
		<?php get_template_part('templates/content/part/give', 'button'); ?>
	<?php else : ?>
		<?php get_template_part('templates/content/part/give-contribute', 'mobile'); ?>
	<?php endif; ?>
<?php } ?>

<?php wp_nonce_field('update_petition_ajax_nonce', 'securityUpdate', true); ?>