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

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

$section = isset($_GET['section']) ? sanitize_text_field($_GET['section']) : '';
if ($section == 'edit_citation') :
    get_template_part('templates/dashboard/edit-citation');
else :
    $citation_fact_score = isset($campoal_options['conikal_citation_fact_score_field']) ? $campoal_options['conikal_citation_fact_score_field'] : 'all';
    $view_counter = isset($campoal_options['conikal_view_counter_field']) ? $campoal_options['conikal_view_counter_field'] : 1;
    $citations_per_page = isset($campoal_options['conikal_citations_per_page_field']) ? $campoal_options['conikal_citations_per_page_field'] : 5;
    $similar = isset($campoal_options['conikal_similar_field']) ? $campoal_options['conikal_similar_field'] : 0;
    $citation_comment = isset($campoal_options['conikal_citation_comment_field']) ? $campoal_options['conikal_citation_comment_field'] : 1;
    $external_link          = isset($campoal_options['conikal_external_link_field']) ? $campoal_options['conikal_external_link_field'] : 0;

    $citation_id = $post->ID;
    $citation_title = get_the_title($citation_id);
    $citation_content = get_the_content($citation_id);
    $citation_excerpt = conikal_get_excerpt_by_id($citation_id);
    $citation_date = get_the_date('', $citation_id);
    $citation_status = get_post_status($citation_id);
    $citation_opinion = get_post_meta($citation_id, 'citation_opinion', true);
    $reference =  get_post_meta($citation_id, 'citation_reference', true);
    $reference_title = get_post_meta($citation_id, 'citation_reference_title', true);
    $reference_description =  get_post_meta($citation_id, 'citation_reference_description', true);
    $reference_image =  get_post_meta($citation_id, 'citation_reference_image', true);
    $citation_media = get_post_meta($citation_id, 'citation_media', true);
    $citation_gallery = get_post_meta($citation_id, 'citation_gallery', true);
    $citation_images = explode('~~~', $citation_gallery);
    $citation_thumb_id = get_post_thumbnail_id($citation_id);
    $citation_thumbnail = wp_get_attachment_image_src($citation_thumb_id, 'thumbnail', true);
    $citation_thumbnail_large = wp_get_attachment_image_src($citation_thumb_id, 'large', true);
    $citation_video = get_post_meta($citation_id, 'citation_video', true);
    $citation_thumb = get_post_meta($citation_id, 'citation_thumb', true);

    // get petition of update
    $petition_id = get_post_meta($citation_id, 'citation_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;

    $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 supporters
    $sign = get_user_meta($current_user->ID, 'petition_sign', true);

    // get citation thumbnail
    if(has_post_thumbnail()) {
        $thumbnail = wp_get_attachment_image_src($citation_thumb_id, 'thumbnail', true);
        $thumbnail = $thumbnail[0];
    } elseif ($citation_thumb) {
        $thumbnail = $citation_thumb;
    } elseif ($citation_gallery) {
        $thumbnail = $citation_images[1];
    } elseif ($reference_image) {
        $thumbnail = $reference_image;
    } else {
        $thumbnail = '';
    }

    // check citation are voted or not
    $votes = get_post_meta($citation_id, 'citation_vote', true);
    $vote_score = get_post_meta($citation_id, 'citation_vote_score', true);
    if (is_array($votes) && array_key_exists($current_user->ID, $votes)) {
        $vote_type = $votes[$current_user->ID];
    } else {
        $vote_type = false;
    }

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

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

    // 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 = '';
    }

    // 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($citation_id);
?>

<div id="wrapper" class="wrapper read petition">
    <!-- 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) ?>" 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 ten wide tablet ten wide computer column" id="content<?php echo esc_attr($petition_id) ?>">
                <!-- UPDATE CONTENT -->
                <div id="main-petition">
                    <div class="ui basic secondary segment">
                        <div class="ui grid">
                            <div class="sixteen wide mobile sixteen wide tablet four wide computer column citation-thumbnail">
                                <a href="">
                                <img class="ui fluid rounded image lazy" data-src="<?php echo esc_url($thumbnail) ?>" src="<?php echo esc_url($lazy_placeholder) ?>">
                                </a>
                            </div>
                            <div class="thirteen wide mobile fourteen tablet ten wide computer column">
                                <div class="ui medium header">
                                    <div class="content">
                                    <span class="ui tiny label <?php echo esc_attr($citation_opinion == 'yes' ? 'citation-label yes' : 'citation-label no') ?>">
                                        <?php echo esc_html($citation_opinion == 'yes' ? esc_html__('Agree', 'campoal') : esc_html__('Disagree', 'campoal')) ?>
                                    </span>
                                    <a href="<?php echo esc_url($reference) ?>" target="<?php echo esc_attr($external_link != 0 ? '_blank' : '_self') ?>">
                                        <?php the_title() ?>
                                    </a> 
                                    <?php if ($citation_status == 'pending') { ?>
                                        <span class="ui primary tiny label"><?php esc_html_e('Pending', 'campoal') ?></span>
                                    <?php } ?>
                                        <div class="sub header"><a href="<?php echo esc_url($reference) ?>" class="text grey" target="<?php echo esc_attr($external_link != 0 ? '_blank' : '_self') ?>"><?php echo esc_html($reference ? wp_parse_url($reference, 1) : '') ?> <i class="external alternate icon"></i></a></div>
                                    </div>
                                </div>
                                <p><?php esc_html_e('Posted by', 'campoal') ?> 
                                    <a href="<?php echo esc_url($author_link) ?>" data-bjax><?php echo esc_html($author_name) ?></a>
                                    <?php echo esc_html__(' at ', 'campoal') . esc_html($citation_date) ?>

                                    <?php if ($view_counter == 1 && function_exists('conikal_get_post_views')) { ?>
                                        · <span class="text grey"><?php echo conikal_format_number('%!,0i', (int) conikal_get_post_views($citation_id), true) . esc_html__(' view', 'campoal') ?></span>
                                    <?php } ?>
                                </p>
                            </div>
                            <div class="three wide mobile two tablet two wide computer center aligned column">
                                <div class="ui vertical icon mini buttons">
                                    <button class="citation-vote citation-vote-<?php echo esc_attr($citation_id) ?> up ui <?php echo esc_attr($vote_type == 'up' ? 'primary ' : '') ?>button" id="citation-vote-up-<?php echo esc_attr($citation_id) ?>" data-id="<?php echo esc_attr($citation_id) ?>"><i class="angle up icon"></i></button>
                                    <button class="ui button" id="citation-vote-num-<?php echo esc_attr($citation_id) ?>"><?php echo esc_html(!empty($vote_score) ? $vote_score : 0) ?></button>
                                    <button class="citation-vote citation-vote-<?php echo esc_attr($citation_id) ?> down ui <?php echo esc_attr($vote_type == 'down' ? 'primary ' : '') ?>button" id="citation-vote-down-<?php echo esc_attr($citation_id) ?>" data-id="<?php echo esc_attr($citation_id) ?>"><i class="angle down icon"></i></button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="ui basic vertical segment">
                        <!-- MAIN CONTENT -->
                        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                            <div class="entry-content">
                                <?php echo wp_kses_post($Parsedown->text(get_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>
                        </div>
                    </div>
                </div>

                <!-- COMMENT UPDATE -->
                <?php if(comments_open() && $citation_comment == 1) {
                        comments_template();
                    } ?>

                <!-- CITTION LIST -->
                <div class="petition-citation-section">
                    <div id="citations<?php echo esc_attr($petition_id) ?>" class="citation-list" data-id="<?php echo esc_attr($petition_id) ?>" data-load="0">
                        <h2 class="ui <?php echo esc_attr(!in_array($citation_fact_score, array('list', 'all')) ? 'dividing ' : '') ?>header"><?php esc_html_e('Citations', 'campoal') ?></h2>
                        <?php if ( in_array($citation_fact_score, array('list', 'all')) ) :
                            $citation_opinion = conikal_citation_fact_score($petition_id);  ?>
                            <div class="ui tiny progress fact-score-status-<?php echo esc_attr(intval($citation_opinion->score) >= 50 ? 'agree' : 'disagree') ?> citation-opinion-progress" id="citation-opinion-progress" data-value="<?php echo esc_attr($citation_opinion->yes) ?>" data-total="<?php echo esc_attr($citation_opinion->total) ?>">
                                <div class="bar">
                                    <div class="progress"></div>
                                </div>
                                <div class="label"><?php esc_html_e('Fact score: ', 'campoal') ?> <span id="citation-fact-score"><?php echo esc_html($citation_opinion->score) ?></span><?php esc_html_e('%', 'campoal') ?></div>
                            </div>
                        <?php endif; ?>
                        <div id="citation-list<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"></div>
                        <div class="ui hidden divider"></div>
                        <div class="ui basic fluid button citations-more" id="citations-more<?php echo esc_attr($petition_id) ?>" data-offset="<?php echo esc_attr($citations_per_page) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><i class="long arrow down icon"></i><?php esc_html_e('More Citation...', 'campoal') ?></div>
                    </div>
                </div>

            </div>
            <div class="six wide tablet six wide computer column tablet computer only" >
                <!-- SIGN AND SHARE PETITION -->
                <?php 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' : ''));
                    }
                } ?>
            </div>
        </div>
    </div>
</div>

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