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

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

$prompt_action          = isset($campoal_options['conikal_prompt_action_field']) ? $campoal_options['conikal_prompt_action_field'] : 1;
$sign_cta_text          = isset($campoal_options['conikal_sign_cta_field']) ? $campoal_options['conikal_sign_cta_field'] : '';
$citation_fact_score    = isset($campoal_options['conikal_citation_fact_score_field']) ? $campoal_options['conikal_citation_fact_score_field'] : 'all';

$post_type = get_post_type($post->ID);
if ($post_type != 'petition') {
    $petition_id    = get_post_meta($post->ID, $post_type . '_post_id', true);
} else {
    $petition_id    = $post->ID;
}

$link               = get_permalink($petition_id);
$supporter_number   = conikal_get_supporter_donors_number($petition_id);
$goal               = $supporter_number->goal;
$sign_num           = $supporter_number->sign;
$status             = get_post_meta($petition_id, 'petition_status', true);

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

// get supporters
$sign               = get_user_meta($current_user->ID, 'petition_sign', true);
$sign               = $sign != '' ? $sign : array();
?>

<div class="ui sticky sign-sticky" id="sign-sticky<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"s>
    <div class="ui basic vertical segment sign-section">
        <?php get_template_part('templates/sign-share/progress'); ?>
        <div class="ui fifted divider"></div>
        <?php get_template_part('templates/sign-share/donate'); ?>

        <div class="spead-campaign-buttons display none" id="spead-campaign-buttons<?php echo esc_attr($petition_id) ?>">
            <?php get_template_part('templates/sign-share/share'); ?>

            <?php if(is_user_logged_in()) {
                    if(in_array($petition_id, $sign) === false && (int)$sign_num < (int)$goal && (int)$status == 0) { ?>
                <div class="ui accordion accord-sign-parent" id="accord-sign-parent<?php echo esc_attr($petition_id) ?>">
                    <div class="title">
                        <a href="javascript:void(0);" class="ui fluid primary big button open-sign-box" id="open-sign-box<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><?php echo conikal_custom_icon('sign') ?><?php echo esc_html_e('Sign this petition', 'campoal') ?></a>
                    </div>
                    <div class="content">
                        <?php get_template_part('templates/sign-share/sign');?>
                    </div>
                </div>
            <?php }
            } else { ?>
                <div class="ui accordion accord-sign-parent" id="accord-sign-parent<?php echo esc_attr($petition_id) ?>">
                    <div class="title">
                        <a href="javascript:void(0);" class="ui fluid primary big button open-sign-box" id="open-sign-box<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><?php echo conikal_custom_icon('sign') ?><?php echo esc_html_e('Sign this petition', 'campoal') ?></a>
                    </div>
                    <div class="content">
                        <?php get_template_part('templates/guest-sign'); ?>
                    </div>
                </div>
            <?php } ?>
        </div>

        <div class="spead-campaign-cta" id="spead-campaign-cta<?php echo esc_attr($petition_id) ?>">
            <?php if ($sign_cta_text != '') { ?>
                <div class="ui vertical basic segment">
                    <?php echo wp_kses_post($sign_cta_text); ?>
                </div>
            <?php } ?>

            <?php if ($citation && ($citation_fact_score == 'all' || $citation_fact_score == 'mobile') ) :
            $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<?php echo esc_attr($petition_id) ?>" 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_attr($petition_id) ?>"><?php echo esc_html($citation_opinion->score) ?></span><?php esc_html_e('%', 'campoal') ?></div>
                </div>
            <?php endif; ?>
        </div>
        

        <?php 
        wp_nonce_field('sign_ajax_nonce', 'securitySign' . $petition_id, true);
        wp_nonce_field('share_ajax_nonce', 'securityShare' . $petition_id, true); ?>
        <input type="hidden" id="petition_id<?php echo esc_attr($petition_id) ?>" value="<?php echo esc_attr($petition_id) ?>">
        <input type="hidden" id="petition_url<?php echo esc_attr($petition_id) ?>" value="<?php echo esc_attr($link) ?>">
    </div>
</div>

<!-- ACTION AFTER SIGN -->
<?php 
    // get action after sign template
    get_template_part('templates/action-after-sign');

    if ($citation) {
        // get modal submit citation
        get_template_part('templates/citation-modal');
    }
?>

<!-- PROMPT ACTION MODAL -->
<?php if ($prompt_action && in_array($petition_id, $sign) === false && (int)$sign_num < (int)$goal && (int)$status == 0) {
    // get modal submit citation
    $prompt_action_remind = isset($_COOKIE['conikal_prompt_action_remind']) ? $_COOKIE['conikal_prompt_action_remind'] : 0;
    if ($prompt_action_remind != 1) { 
        get_template_part('templates/prompt-action');
    }
} ?>