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

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

$give_prefix        = '_give_';

$fundraising_mode = isset($campoal_options['conikal_fundraising_mode_field']) ? $campoal_options['conikal_fundraising_mode_field'] : 0;
$sign_without_login = isset($campoal_options['conikal_sign_petition_without_login_field']) ? $campoal_options['conikal_sign_petition_without_login_field'] : 0;
$passing_button_link = isset($campoal_options['conikal_passing_button_link_field']) ? $campoal_options['conikal_passing_button_link_field'] : 0;
$default_accordion = isset($campoal_options['conikal_default_accordion_sign_field']) ? $campoal_options['conikal_default_accordion_sign_field'] : 'share';
$hashtag = isset($campoal_options['conikal_social_hastag_field']) ? $campoal_options['conikal_social_hastag_field'] : '';
$fb_app_id = isset($campoal_options['conikal_fb_id_field']) ? $campoal_options['conikal_fb_id_field'] : '';

$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;
}

$title              = get_the_title($petition_id);
$link               = get_permalink($petition_id);
$topics             = wp_get_post_terms($petition_id, 'petition_topics');
$heading            = get_post_meta($petition_id, 'petition_heading', true);
$supporter_number   = conikal_get_supporter_donors_number($petition_id);
$goal               = $supporter_number->goal;
$sign_num           = $supporter_number->sign;

// get topics hastag
$topics_hashtag = '';
if ($topics) {
    foreach ($topics as $topic) {
        $topics_hashtag .= sprintf(esc_html__('# %s, ', 'campoal'), $topic->name);
    }
    $hashtag = $topics_hashtag;
}

// get form option
$form_id            = conikal_get_petition_form_id($petition_id);
$reveal_label       = get_post_meta($form_id, '_give_reveal_label', true);

// check user has purchases
$user_contribute    = conikal_user_purchased_donation($form_id);

// get post thumbnail
$thumbnail 			= conikal_get_petition_thumbnail($petition_id, 'thumbnail', false);
$lazy_placeholder   = conikal_lazy_load_placeholder('thumbnail-square');

$goal_option        = get_post_meta($form_id, $give_prefix . 'goal_option', true);
$raise_number       = conikal_get_supporter_donors_number($form_id, 'donor', true);

$give_attrs['id']           = $form_id;
$give_attrs['show_goal']    = false;

// Get share counter 
$share_counter = conikal_get_share_counter($petition_id);
?>

<div class="ui modal" id="sign-successful-modal<?php echo esc_attr($petition_id) ?>">
    <i class="close icon"></i>
    <div class="content">
        <div class="ui centered grid">
            <div class="sixteen wide mobile eight wide tablet eight wide computer left aligned column share-column">
                <div class="sign-success">
                    <div class="ui basic center aligned segment">
                        <h2 class="ui icon green header">
                            <i class="circular checkmark icon"></i>
                            <?php esc_html_e('You are signed Successfully!' ,'campoal') ?>
                        </h2>
                        <p class="font medium">
                            <?php echo sprintf(esc_html__('You’re one of %2$s people to sign this petition. Now help find %1$s more people to reach the goal.', 'campoal'), conikal_format_number('%!,0i', ( ( (int)$goal - (int)$sign_num) ) - 1 ), conikal_format_number('%!,0i', ((int)$sign_num) + 1)) ?>
                        </p>
                        <div class="ui hidden divider"></div>
                        <?php if (!empty($passing_button_link)) {
                            $pass_link = get_page_link($passing_button_link); ?>
                            <a href="<?php echo esc_url($pass_link) ?>" class="ui basic button" data-bjax><?php esc_html_e('Skip this', 'campoal') ?></a>
                        <?php } else { ?>
                            <button class="ui basic button skip-after-sign" id="skip-after-sign<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><?php esc_html_e('Skip this', 'campoal') ?></button>
                        <?php } ?>
                    </div>
                </div>
            </div>
            <div class="sixteen wide mobile eight wide tablet eight wide computer left aligned column contribute-column">
                <!-- FACEBOOK SHARE -->
                <div class="ui accordion accord-share-facebook">
                    <?php if (isset($form_id) && $form_id != '' && $reveal_label != '' && class_exists('Give')) { ?>
                    <div class="title<?php echo esc_attr($default_accordion === 'contribute' && $user_contribute == false ? ' active' : '') ?>">
                        <h3 class="ui header">
                        <i class="dropdown icon"></i>
                        <?php echo sprintf(esc_html__('%s to campaign', 'campoal'), $reveal_label) ?>
                        </h3>
                    </div>
                    <div class="content<?php echo esc_attr($default_accordion === 'contribute' && $user_contribute == false ? ' active' : '') ?>">
                        <?php if ($fundraising_mode == 0) : ?>
                            <?php if (give_is_setting_enabled( $goal_option )) : ?>
                                <div class="ui tiny header">
                                    <?php echo sprintf( esc_html__('%1$s of %2$s raised', 'campoal'), esc_html($raise_number->sign_format), esc_html($raise_number->goal_format) ) ?>
                                </div>
                                <div class="ui tiny primary progress petition-goal" data-value="<?php echo esc_attr($raise_number->sign) ?>" data-total="<?php echo esc_attr($raise_number->goal) ?>">
                                    <div class="bar">
                                        <div class="progress"></div>
                                    </div>
                                </div>
                            <?php endif; ?>
                        <?php endif; ?>
                        <?php echo give_form_shortcode($give_attrs) ?>
                    </div>
                    <?php } ?>

                    <div class="title<?php echo esc_attr($default_accordion === 'share' || $form_id === '' || $user_contribute == true ? ' active' : '') ?>">
                        <h3 class="ui header">
                        <i class="dropdown icon"></i>
                        <?php esc_html_e('Share this campaign', 'campoal') ?>
                        </h3>
                    </div>
                    <div class="content<?php echo esc_attr($default_accordion === 'share' || $form_id === '' || $user_contribute == true ? ' active' : '') ?>">
                        <div class="ui form">
                            <div class="field">
                                <div class="ui pointing below fluid basic large label">
                                    <textarea name="sign-comment" id="sign-comment<?php echo esc_attr($petition_id) ?>" class="sign-comment<?php echo esc_attr(wp_is_mobile() ? ' ui large input' : '') ?>" rows="3" placeholder="<?php esc_attr_e('Add a personal message', 'campoal') ?>" data-id="<?php echo esc_attr($petition_id) ?>"></textarea>
                                    <div class="ui horizontal divider">
                                        <i class="share alternate icon"></i>
                                        <span id="share-number<?php echo esc_attr($petition_id) ?>">
                                            <?php echo esc_html( conikal_format_number('%!,0i', $share_counter['total'], true) ) ?>
                                        </span>
                                        <?php esc_html_e('Shares', 'campoal') ?>
                                    </div>
                                    <div class="ui grid">
                                        <div class="four wide column">
                                            <img class="ui fluid rounded image lazy" id="thumbnail-share<?php echo esc_attr($petition_id) ?>" data-src="<?php echo esc_url($thumbnail) ?>" src="<?php echo esc_url($lazy_placeholder) ?>" alt="<?php echo esc_attr($title) ?>">
                                        </div>
                                        <div class="twelve wide column ui form">
                                        <?php if (get_the_author_meta( 'ID' ) == $current_user->ID || current_user_can('editor') || current_user_can('administrator')) { ?>
                                            <h3 class="heading-sharing"><?php echo esc_html($heading != '' ? $heading : $title) ?></h3>
                                            <div class="edit-heading field display none">
                                                <textarea id="custom-heading" rows="3"><?php echo esc_textarea($heading != '' ? $heading : $title) ?></textarea>
                                            </div>
                                            <?php wp_nonce_field('custom_heading_ajax_nonce', 'securityHeading', true); ?>
                                        <?php } else { ?>
                                            <h3><?php echo esc_html($heading != '' ? $heading : $title) ?></h3>
                                        <?php } ?>
                                        </div>
                                    </div>
                                </div>
                                <!-- PRIMARY SHARE BUTTON -->
                                <?php get_template_part('templates/sign-share/share-button'); ?>
                            </div>
                        </div>

                        <div class="ui hidden divider"></div>
                        <!-- SOCIAL SHARE BUTTON -->
                        <?php get_template_part('templates/social-buttons'); ?>

                        <!-- SHORT URL -->
                        <?php get_template_part('templates/sign-share/short-link'); ?>
                        <input type="hidden" id="hashtag<?php echo esc_attr($petition_id) ?>" name="hashtag" value="<?php echo esc_attr($hashtag) ?>">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>