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

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

$give_prefix        = '_give_';

$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'] : '';
$share_comment_box = isset($campoal_options['conikal_share_comment_box_field']) ? $campoal_options['conikal_share_comment_box_field'] : 0;
$mobile_sign_style = isset($campoal_options['conikal_mobile_sign_style_field']) ? $campoal_options['conikal_mobile_sign_style_field'] : 'button';
$sign_show_all = isset($campoal_options['conikal_sign_show_all_field']) ? $campoal_options['conikal_sign_show_all_field'] : 0;
$custom_heading_template = isset($campoal_options['conikal_default_heading_template_field']) ? $campoal_options['conikal_default_heading_template_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;
}

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

// get petition citation status
$citation           = conikal_citation_status($petition_id);
$title              = get_the_title($petition_id);
$link               = get_permalink($petition_id);
$heading            = get_post_meta($petition_id, 'petition_heading', true);
$heading_display    = conikal_custom_heading_title($petition_id);
$topics             = wp_get_post_terms($petition_id, 'petition_topics');
$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 form option
$form_id            = conikal_get_petition_form_id($petition_id);
$reveal_label       = get_post_meta($form_id, '_give_reveal_label', true);

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

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

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

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

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

	<!-- FACEBOOK SHARE -->
    <?php if ($sign_show_all == 1) { ?>
        <div class="ui accordion accord-contribute-share" id="accord-contribute-share<?php echo esc_attr($petition_id) ?>">
            <div class="title"></div>
            <div class="content">
    <?php } ?>
    <div class="<?php echo esc_attr($sign_show_all == 0 ? 'ui ' : '' ) ?>accordion accord-share-facebook">
        <?php if (isset($form_id) && !empty($form_id) && $reveal_label != '' && class_exists('Give') && $sign_show_all == 0) { ?>
        <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 || $reveal_label == '' || $sign_show_all == 1 ? ' 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 || $reveal_label == '' || $sign_show_all == 1 ? ' active' : '') ?>">
            <div class="ui form">
                <div class="field">
                    <div class="ui pointing below fluid basic large label">
                        <?php if ($share_comment_box) : ?>
                        <textarea name="signComment" id="sign-comment<?php echo esc_attr($petition_id) ?>" class="sign-comment" 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', (!empty($share_counter['total']) ? $share_counter['total'] : 0), true) ) ?>
                            </span>
                            <?php esc_html_e('Shares', 'campoal') ?>
                        </div>
                        <?php endif; ?>
                        <div class="ui grid">
                            <div class="four wide column">
                                <img class="ui fluid rounded image lazy" 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" id="heading-sharing<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><?php echo esc_html($heading != '' ? $heading_display : $title) ?></h3>
                                <div class="edit-heading field display none" id="edit-heading<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>">
                                    <textarea class="custom-heading" id="custom-heading<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>" rows="3"><?php echo esc_textarea($heading != '' ? $heading : $title) ?></textarea>
                                </div>
                                <?php wp_nonce_field('custom_heading_ajax_nonce', 'securityHeading', true); ?>
                            <?php } else { ?>
                                <h4><?php echo esc_html($heading != '' ? $heading_display : $title) ?></h4>
                            <?php } ?>
                            </div>
                        </div>
                    </div>
                    <!-- PRIMARY SHARE BUTTON -->
                    <?php get_template_part('templates/sign-share/share-button'); ?>
                </div>
                <?php if (is_user_logged_in()) {
                if ($citation && $sign_show_all == 0) { ?>
                    <?php get_template_part('templates/citation-opinion', 'add'); ?>
                <?php } else if ($citation && $sign_show_all == 1 && (in_array($petition_id, $sign) === true || (int)$sign_num >= (int)$goal || (int)$status != 0)) { ?>
                    <?php get_template_part('templates/citation-opinion', 'add'); ?>
                <?php }
                } ?>
            </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'); ?>
        </div>
    </div>
    <?php if ($sign_show_all == 1) { ?>
            </div>  
        </div>
        <div class="field">
            <button class="ui primary fluid big button open-share-box" id="open-share-box<?php echo esc_attr($petition_id) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><?php echo conikal_custom_icon('share') ?><?php esc_html_e('Share the Petition', 'campoal') ?></button>
        </div>
    <?php } ?>
    <input type="hidden" id="hashtag<?php echo esc_attr($petition_id) ?>" name="hashtag" value="<?php echo esc_attr($hashtag) ?>">