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

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

$embed_code = isset($campoal_options['conikal_embed_code_field']) ? $campoal_options['conikal_embed_code_field'] : 0;
$embed_style = isset($_GET['embed']) ? sanitize_text_field($_GET['embed']) : '';

if (!empty($embed_code) && !empty($embed_style) && $embed_style == 'card') {
    get_template_part('templates/embed/embed-card');
    exit();
}

$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;
$custom_fields_mode = isset($campoal_options['conikal_custom_fields_mode_field']) ? $campoal_options['conikal_custom_fields_mode_field'] : 0;
$custom_fields_front_end = isset($campoal_options['conikal_custom_fields_front_end_field']) ? $campoal_options['conikal_custom_fields_front_end_field'] : 0;
$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';

while(have_posts()) : the_post();
    $petition_id = get_the_ID();
    $title = get_the_title($petition_id);
    $sendinblue_list = get_post_meta($petition_id, 'petition_sendinblue_list', true);

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

    // create new and update sendinblue list
    if ( empty($sendinblue_list) || (isset($_GET['action']) && $_GET['action'] == 'updated') ) {
        conikal_sendinblue_list($petition_id, $sendinblue_list, $title);
    }
    
    // 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 = '';
    }
?>

<article id="post-<?php the_ID(); ?>" <?php post_class('wrapper read petition'); ?>>
    <!-- TAB CONTENT -->
    <?php get_template_part('templates/content/part/petition', 'manage'); ?>

    <div class="ui container">
        <!-- ALERT PEITITION -->
        <?php get_template_part('templates/content/part/petition', 'alert'); ?>

        <?php if (function_exists('conikal_ads_banner')) { 
            conikal_ads_banner('conikal_single_petition_banner_1_field');
        } ?>

        <!-- TITLE AND AUTHOR OF PETITION -->
        <?php get_template_part('templates/content/part/petition', 'title'); ?>

        <div class="ui relaxed grid">
            <div class="sixteen wide mobile sixteen wide tablet ten wide computer column petition-content" id="content<?php echo esc_attr($petition_id) ?>">

                <!-- TAB CONTENT -->
                <?php get_template_part('templates/content/part/petition', 'tab'); ?>

                <!-- PETITION CONTENT -->
                <div class="main-petition" id="main-petition">
                    <div class="ui basic vertical segment" id="story<?php echo esc_attr($petition_id) ?>">
                        <!-- THUMBNAIL OF PETITION -->
                        <?php get_template_part('templates/content/part/petition', 'thumbnail'); ?>

                        <!-- PROGRESS BAR -->
                        <?php get_template_part('templates/content/part/petition', 'progress'); ?>

                        <!-- LOCATION AND VIEW -->
                        <?php get_template_part('templates/content/part/petition', 'location'); ?>

                        <!-- MAIN CONTENT PETITION -->
                        <?php get_template_part('templates/content/part/petition', 'content'); ?>

                        <!-- CUSTOM FIELDS -->
                        <?php if (class_exists('ACF') && $custom_fields_mode && $custom_fields_front_end) {
                            get_template_part('templates/content/part/petition', 'custom-fields'); 
                        } ?>

                        <!-- TOPICS AND DATE -->
                        <?php get_template_part('templates/content/part/petition', 'topics'); ?>

                        <!-- ADS BANNER -->
                        <?php if (function_exists('conikal_ads_banner')) {
                            conikal_ads_banner('conikal_single_petition_banner_2_field');
                        } ?>

                        <!-- SUPPORTERS AND DONORS -->
                        <?php get_template_part('templates/content/part/petition', 'supporters'); ?>
                    </div>

                        <!-- LETTER TO -->
                        <?php get_template_part('templates/content/part/petition', 'letter'); ?>

                        <!-- CITTION LIST -->
                        <?php get_template_part('templates/content/part/petition', 'citations'); ?>

                        <!-- UPDATES -->
                        <?php get_template_part('templates/content/part/petition', 'updates'); ?>

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

                    <!-- NAVIGATION -->
                    <?php get_template_part('templates/content/part/post', 'navigation'); ?>

            </div>
            <div class="sixteen wide tablet six wide computer column">
                <!-- SIDEBAR 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>

<?php 
    // similar petition
    if($similar == 1) { 
        get_template_part('templates/content/part/petition-similar'); 
    } ?>

    <?php if (function_exists('conikal_ads_banner')) { 
        conikal_ads_banner('conikal_single_petition_banner_3_field');
    } ?>

<?php 
    // sign petition sidebar on mobile
    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
            get_template_part('templates/sign-share-mobile', ($sign_show_all == 1 && $mobile_sign_style == 'sidebar' ? 'all' : ''));
        }
    } else {
        get_template_part('templates/sign-share-tablet');
    }

    // init security code
    wp_nonce_field('bookmark_ajax_nonce', 'securityBookmark', true);
    wp_nonce_field('supporters_list_ajax_nonce', 'securitySupporters', true);
    wp_nonce_field('letter_ajax_nonce', 'securityLetter', true);
    wp_nonce_field('update_petition_ajax_nonce', 'securityUpdate', true);
    wp_nonce_field('citation_ajax_nonce', 'securityCitation', true);
    wp_nonce_field('views_ajax_nonce', 'securityViews', true);

endwhile;
wp_reset_postdata();
wp_reset_query();
?>
</article>