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

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

// Check user is loggedin or not, if not redirect to login page
conikal_redirect_login();

// common donation form 
$common_donation_form  	= isset($campoal_options['conikal_common_donation_form_field']) ? $campoal_options['conikal_common_donation_form_field'] : 0;
$common_form_id     	= isset($campoal_options['conikal_common_form_id_field']) ? $campoal_options['conikal_common_form_id_field'] : 0;

$number_sign_change_goal    = isset($campoal_options['conikal_number_sign_change_field']) ? $campoal_options['conikal_number_sign_change_field'] : 0;
$receiver_step              = isset($campoal_options['conikal_receiver_step_field']) ? $campoal_options['conikal_receiver_step_field'] : 1;
$visitors_statictis         = isset($campoal_options['conikal_visitors_statictis_field']) ? $campoal_options['conikal_visitors_statictis_field'] : 1;
$auto_increase_goal         = isset($campoal_options['conikal_auto_increase_goal_field']) ? $campoal_options['conikal_auto_increase_goal_field'] : 0;
$auto_increase_goal_default = isset($campoal_options['conikal_auto_increase_goal_d_field']) ? $campoal_options['conikal_auto_increase_goal_d_field'] : '';
$page_dashboard_petition    = isset($campoal_options['conikal_page_dashboard_petition_field']) ? $campoal_options['conikal_page_dashboard_petition_field'] : 0;
$petition_comment           = isset($campoal_options['conikal_petition_comment_field']) ? $campoal_options['conikal_petition_comment_field'] : 1;
$page_dashboard_petition    = $page_dashboard_petition ? get_permalink($page_dashboard_petition) : '';
$supporters_list_page       = isset($campoal_options['conikal_page_supporters_list_field']) ? $campoal_options['conikal_page_supporters_list_field'] : 0;
$supporters_list_page       = $supporters_list_page ? get_permalink($supporters_list_page) : '';

// placeholder lazy load avatar
$lazy_placeholder_avatar    = conikal_lazy_load_placeholder('avatar');
?>
<?php 
    $edit_id = isset($_GET['id']) ? sanitize_text_field($_GET['id']) : '';
    if ($edit_id) {
        $prefix                 = 'petition_';
        $post_type              = get_post_type($edit_id);
        if ($post_type == 'give_forms') {
            $prefix             = '_give_';
        }

        $link                   = get_permalink($edit_id);
        $title                  = get_the_title($edit_id);
        $content                = get_the_content($edit_id);
        $comments               = wp_count_comments($edit_id);
        $address                = get_post_meta($edit_id, $prefix . 'address', true);
        $city                   = get_post_meta($edit_id, $prefix . 'city', true);
        $state                  = get_post_meta($edit_id, $prefix . 'state', true);
        $neighborhood           = get_post_meta($edit_id, $prefix . 'neighborhood', true);
        $zip                    = get_post_meta($edit_id, $prefix . 'zip', true);
        $country                = get_post_meta($edit_id, $prefix . 'country', true);
        $lat                    = get_post_meta($edit_id, $prefix . 'lat', true);
        $lng                    = get_post_meta($edit_id, $prefix . 'lng', true);
        $decisionmakers         = get_post_meta($edit_id, $prefix . 'decisionmakers', true);
        $decisionmakers         = array_unique(explode(',', $decisionmakers));
        $receiver               = get_post_meta($edit_id, $prefix . 'receiver', true);
        $receiver               = explode(',', $receiver);
        $position               = get_post_meta($edit_id, $prefix . 'position', true);
        $position               = explode(',', $position);
        $updates                = get_post_meta($edit_id, $prefix . 'update', true);
        $thumb                  = get_post_meta($edit_id, $prefix . 'thumb', true);
        $letter                 = get_post_meta($edit_id, $prefix . 'letter', true);
        $status                 = get_post_meta($edit_id, $prefix . 'status', true);
        $co_authors             = get_post_meta($edit_id, $prefix . 'authors', true);
        $co_authors_number      = !empty($co_authors) ? count($co_authors) : 1;
        $increase_percent       = get_post_meta($edit_id, $prefix . 'increase', true);
        $increase_percent       =  $increase_percent ? $increase_percent : $auto_increase_goal_default;
        $post_type              = get_post_type($edit_id);
        $vistors_count          = conikal_get_post_views($edit_id, 'visitors', false);
        $page_views             = conikal_get_post_views($edit_id, 'views', false);
        $video_players          = conikal_get_post_views($edit_id, 'players', false);

        // get author
        $author_ID              = get_post_field( 'post_author', $edit_id );
        $author_name            = get_the_author_meta('display_name', $author_ID);
        $author_link            = get_author_posts_url($author_ID, get_the_author_meta('user_nicename'));
        $author_avatar          = conikal_get_avatar_url($author_ID, array('size' => 64));
        $author_avatar_retina   = conikal_get_avatar_url($author_ID, array('size' => 128));

        // get fundraising mode setting
        $fundraising_mode_apply     = isset($campoal_options['conikal_fundraising_mode_apply_field']) ? $campoal_options['conikal_fundraising_mode_apply_field'] : 'global';
        $petition_fundraising_mode  = get_post_meta($edit_id, 'petition_fundraising_mode', true);
        $fundraising_mode           = $fundraising_mode = conikal_fundraising_mode_setting($edit_id);

        // get petition citation status
        $citation_apply             = isset($campoal_options['conikal_citation_apply_field']) ? $campoal_options['conikal_citation_apply_field'] : 'single';
        $petition_citation          = get_post_meta($edit_id, 'petition_citation_status', true);
        $citation_mode              = isset($campoal_options['conikal_citation_field']) ? $campoal_options['conikal_citation_field'] : 1;

        // get form option
        $form_id                    = get_post_meta($edit_id, 'petition_contribute', true);
        $contribute_approve         = get_post_status($form_id);

        if ($contribute_approve != 'publish') {
            $form_id = '';
        }

        // get supporter number
        if ($post_type == 'petition') {
            $supporter_number   = conikal_get_supporter_donors_number($edit_id, 'supporter', true);
            $goal               = $supporter_number->goal;
            $sign               = $supporter_number->sign;
        } else {
            $sign               = give_get_form_sales_stats( $edit_id );
        }
    }

    $post_author_id = get_post_field( 'post_author', $edit_id );

    // Get share counter 
    $share_counter = conikal_get_share_counter($edit_id, 'total');
?>
<?php if ( $edit_id != '' && ($post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator') || conikal_co_author_permission($edit_id)) ) { ?>
    <div class="ui container" id="dashboard-petition">
        <div class="ui hidden divider"></div>
        <div class="ui raised padded segment">
            <div class="ui four small statistics">
                <div class="statistic">
                    <div class="value">
                        <?php echo esc_html( conikal_format_number('%!,0i', $page_views, true) ); ?>
                    </div>
                    <div class="label">
                        <?php echo esc_html( _n('Page View', 'Page Views', $page_views, 'campoal') ) ?>
                    </div>
                </div>
                <div class="statistic">
                    <div class="value">
                        <?php echo esc_html( conikal_format_number('%!.0i', (class_exists('Give') && $fundraising_mode && $form_id ? give_get_form_sales_stats( $form_id ) : $sign), true) ); ?>
                    </div>
                    <div class="label">
                        <?php echo esc_html( $fundraising_mode == 1 && !empty($form_id) ? _n('Donor', 'Donors', give_get_form_sales_stats( $form_id ), 'campoal') : _n( 'Supporter', 'Supporters', $sign, 'campoal') ) ?>
                    </div>
                </div>
                <div class="statistic">
                    <div class="value">
                        <?php echo esc_html( $petition_comment == 1 ? conikal_format_number('%!.0i', $comments->approved, true) : $vistors_count ); ?>
                    </div>
                    <div class="label">
                        <?php echo esc_html($petition_comment == 1 ? _n( 'Comment', 'Comments', $comments->approved, 'campoal') : _n( 'Visitor', 'Visitors', $vistors_count, 'campoal') ) ?>
                    </div>
                </div>
                <div class="statistic">
                    <div class="value">
                        <?php echo esc_html( conikal_format_number('%!.0i', $share_counter, true) ); ?>
                    </div>
                    <div class="label">
                        <?php echo esc_html( _n( 'Share', 'Shares', $share_counter, 'campoal') ) ?>
                    </div>
                </div>
            </div>

            <?php if ($post_type == 'petition') : ?>
                <div class="ui section divider"></div>
                <div class="ui grid">
                    <div class="sixteen wide mobile eight wide tablet eight wide computer center aligned column">
                        <div class="ui form">
                            <div class="inline field">
                                <label><?php esc_html_e('Set new goals', 'campoal') ?></label>
                                <div class="ui action <?php echo esc_attr($sign >= $number_sign_change_goal || current_user_can('editor') || current_user_can('administrator') ? '' : 'disabled ') ?>input">
                                <input type="number" id="new-goal" placeholder="<?php esc_attr_e('Enter a new goal', 'campoal') ?>" value="<?php echo esc_attr(isset($goal) ? $goal : '') ?>">  
                                <button class="ui primary <?php echo esc_attr($sign >= $number_sign_change_goal || current_user_can('editor') || current_user_can('administrator') ? '' : 'disabled ') ?>button" id="set-goal"><?php esc_html_e('Save', 'campoal') ?></button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php if ($auto_increase_goal == 1) { ?>
                    <div class="sixteen wide mobile eight wide tablet eight wide computer center aligned column">
                        <div class="ui form">
                            <div class="inline field">
                                <label><?php esc_html_e('Auto increase goal', 'campoal') ?></label>
                                <div class="ui action input increase-goal-selection">
                                    <select name="increase_percent" id="increase-percent" class="ui dropdown">
                                        <?php
                                        $options_percent = array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200, 300, 500);
                                        foreach ($options_percent as $percent) { ?>
                                            <option value="<?php echo esc_attr($percent) ?>" <?php echo esc_attr($percent == $increase_percent ? 'selected="selected"' : '') ?>><?php echo esc_html($percent != 0 ? $percent . __('%', 'campoal') : __('No Increases', 'campoal')) ?></option>
                                        <?php } ?>
                                    </select>
                                    <button class="ui primary <?php echo esc_attr(current_user_can('editor') || current_user_can('administrator') || conikal_co_author_permission($edit_id) ? '' : 'disabled ') ?>button" id="set-auto-increase"><?php esc_html_e('Save', 'campoal') ?></button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php } ?>
                </div>
            <?php endif; ?>
            <?php if ($post_type == 'petition') : ?>
                <div class="ui section divider"></div>
                <div class="ui grid">
                    <div class="sixteen wide mobile eight wide tablet eight wide computer center aligned column">
                        <div class="ui buttons">
                            <?php if ($citation_mode == 1 || $citation_apply == 'single' || $fundraising_mode == 1 || $fundraising_mode_apply == 'single') : ?>
                                <div class="ui primary floating labeled icon dropdown button petition-settings" id="petition-settings">
                                    <i class="cog icon"></i>
                                    <span class="text"><?php esc_html_e('Settings', 'campoal') ?></span>
                                    <div class="ui petition-settings popup top left transition hidden text align left">
                                        <div class="ui form">
                                            <?php if ($citation_mode == 1 || $citation_apply == 'single') : ?>
                                            <div class="field">
                                                <div class="ui toggle <?php echo esc_attr($petition_citation == 1 ? 'checked ' : '') ?>checkbox">
                                                    <input type="checkbox" 
                                                    tabindex="0" 
                                                    class="petition-setting hidden" 
                                                    name="petition-citation" 
                                                    id="petition_citation_status" 
                                                    data-status="<?php echo esc_attr($petition_citation == 1 ? 0 : 1) ?>" 
                                                    data-id="<?php echo esc_attr($edit_id) ?>" 
                                                    data-setting="petition_citation_status" 
                                                    <?php echo esc_attr($petition_citation == 1 ? 'checked ' : '') ?>>
                                                        <label><?php esc_html_e('Show citation', 'campoal') ?></label>
                                                </div>
                                            </div>
                                            <?php endif; ?>
                                            <?php if (empty($common_donation_form) && empty($common_form_id)) :
                                            if ($fundraising_mode == 1 || $fundraising_mode_apply == 'single') : ?>
                                            <div class="field">
                                                <div class="ui toggle <?php echo esc_attr($petition_fundraising_mode == 1 ? 'checked ' : '') ?>checkbox">
                                                    <input type="checkbox" 
                                                    tabindex="0" 
                                                    class="petition-setting hidden"
                                                    name="petition-citation" 
                                                    id="petition_fundraising_mode" 
                                                    data-status="<?php echo esc_attr($petition_fundraising_mode == 1 ? 0 : 1) ?>" 
                                                    data-id="<?php echo esc_attr($edit_id) ?>" 
                                                    data-setting="petition_fundraising_mode" 
                                                    <?php echo esc_attr($petition_fundraising_mode == 1 ? 'checked ' : '') ?>>
                                                    <label><?php esc_html_e('Fundraising mode', 'campoal') ?></label>
                                                </div>
                                            </div>
                                            <?php endif;
                                            endif; ?>
                                        </div>
                                    </div>
                                </div>
                            <?php endif; ?>
                            <?php
                            // generate download signature link
                            $export_type = array(
                                'csv_signature' => array('export_id' => $edit_id, 'download' => 'csv', 'reason' => 'no'),
                                'csv_reason' => array('export_id' => $edit_id, 'download' => 'csv', 'reason' => 'yes'),
                                'pdf_signature' => array('export_id' => $edit_id, 'download' => 'pdf', 'reason' => 'no'),
                                'pdf_reason' => array('export_id' => $edit_id, 'download' => 'pdf', 'reason' => 'yes'),
                            );
                            
                            $download_csv_signature = add_query_arg($export_type['csv_signature'], $page_dashboard_petition);
                            $download_csv_reason = add_query_arg($export_type['csv_reason'], $page_dashboard_petition);
                            $download_pdf_signature = add_query_arg($export_type['pdf_signature'], $page_dashboard_petition);
                            $download_pdf_reason = add_query_arg($export_type['pdf_reason'], $page_dashboard_petition);
                            ?>

                            <!-- Export supporter to CSV and PDF button -->
                            <div class="ui primary bottom pointing labeled icon dropdown button export-signature-btn">
                                <i class="cloud download icon"></i>
                                <span class="text"><?php esc_html_e('Export signatures', 'campoal') ?></span>
                                <div class="menu">
                                    <a href="<?php echo esc_url($download_csv_signature); ?>" class="item">
                                        <i class="file excel icon"></i>
                                        <?php esc_html_e('CSV signatures', 'campoal') ?>
                                    </a>
                                    <a href="<?php echo esc_url($download_csv_reason); ?>" class="item">
                                        <i class="file excel icon"></i>
                                        <?php esc_html_e('CSV comments', 'campoal') ?>
                                    </a>
                                    <a href="<?php echo esc_url($download_pdf_signature); ?>" class="item">
                                        <i class="file pdf icon"></i>
                                        <?php esc_html_e('PDF signatures', 'campoal') ?>
                                    </a>
                                    <a href="<?php echo esc_url($download_pdf_reason); ?>" class="item">
                                        <i class="file pdf icon"></i>
                                        <?php esc_html_e('PDF comments', 'campoal') ?>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="sixteen wide mobile eight wide tablet eight wide computer center aligned column">
                        <?php if (!$status || $status == '0') { ?>
                            <div class="ui buttons">
                                <!-- Declare victory button -->
                                <?php if ($post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator')) :
                                        $declare_victory_link = add_query_arg(array('section' => 'add_update', 'id' => $edit_id, 'type' => 'victory', 'status' => '1'), $page_dashboard_petition);
                                        if ($page_dashboard_petition) { ?>
                                        <a class="ui primary labeled icon button" href="<?php echo esc_url($declare_victory_link); ?>" data-bjax><i class="flag checkered icon"></i><?php esc_html_e('Declare victory', 'campoal') ?></a>
                                <?php }
                                endif; ?>

                                <!-- Close petition button -->
                                <?php if ($post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator')) { ?>
                                    <button class="ui labeled icon button" id="close-petition"><i class="close icon"></i><?php esc_html_e('Close petition', 'campoal') ?></button>
                                <?php } ?>
                            </div>
                        <?php } else { ?>
                            <!-- Open petition button -->
                            <?php if (!$post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator')) { ?>
                                <button class="ui primary labeled icon button" id="reopen-petition"><i class="redo icon"></i><?php esc_html_e('Reopen Petition', 'campoal') ?></button>
                            <?php } ?>
                        <?php } ?>
                    </div>
                </div>
            <?php endif; ?>
            <div class="ui section divider"></div>
            <div class="ui grid">
                <div class="two column row">
                    <div class="left floated column">
                        <h1 class="ui header"><?php esc_html_e('Supporters Statistics', 'campoal') ?></h1>
                    </div>
                    <div class="right floated right aligned column">
                    <div class="ui selection dropdown supporter-chart-date">
                        <input type="hidden" name="supporter_chart_date" id="supporter_chart_date" value="7">
                        <span class="text"><?php esc_html_e('Last 7 Days', 'campoal') ?></span>
                        <i class="dropdown icon"></i>
                        <div class="menu">
                            <div class="item" data-value="7"><?php esc_html_e('Last 7 Days', 'campoal') ?></div>
                            <div class="item" data-value="14"><?php esc_html_e('Last 14 Days', 'campoal') ?></div>
                            <div class="item" data-value="28"><?php esc_html_e('Last Month', 'campoal') ?></div>
                            <div class="item" data-value="90"><?php esc_html_e('Last 3 Month', 'campoal') ?></div>
                            <div class="item" data-value="180"><?php esc_html_e('Last 6 Month', 'campoal') ?></div>
                            <div class="item" data-value="360"><?php esc_html_e('Last Year', 'campoal') ?></div>
                        </div>
                    </div>
                    <div class="ui icon input supporter-chart-date-range">
                        <i class="calendar icon"></i>
                        <input type="text" name="supporter_chart_date_range" id="supporter_chart_date_range" value="">
                        <input type="hidden" name="supporter_chart_date_range_start" id="supporter_chart_date_range_start" value="">
                        <input type="hidden" name="supporter_chart_date_range_end" id="supporter_chart_date_range_end" value="">
                        <button class="display none" id="supporter_chart_date_apply"></button>
                    </div>
                    </div>
                </div>
            </div>

            <div class="ui hidden divider"></div>

            <canvas id="supportersChart" width="1000" height="380"></canvas>
        </div>
        <?php if ($current_user->show_visitor_statistics != 0 || $current_user->show_visitor_statistics == '' || $visitors_statictis == 1 || current_user_can('editor') || current_user_can('administrator')) : ?>
        <div class="ui raised padded segment">
            <div class="ui grid">
                <div class="two column row">
                    <div class="left floated column">
                        <h1 class="ui header"><?php esc_html_e('Visitors Statistics', 'campoal') ?></h1>
                    </div>
                    <div class="right floated right aligned column">
                        <div class="ui selection dropdown visitor-chart-date">
                            <input type="hidden" name="visitor_chart_date" id="visitor_chart_date" value="7">
                            <span class="text"><?php esc_html_e('Last 7 Days', 'campoal') ?></span>
                            <i class="dropdown icon"></i>
                            <div class="menu">
                                <div class="item" data-value="7"><?php esc_html_e('Last 7 Days', 'campoal') ?></div>
                                <div class="item" data-value="14"><?php esc_html_e('Last 14 Days', 'campoal') ?></div>
                                <div class="item" data-value="30"><?php esc_html_e('Last Month', 'campoal') ?></div>
                                <div class="item" data-value="90"><?php esc_html_e('Last 3 Month', 'campoal') ?></div>
                                <div class="item" data-value="180"><?php esc_html_e('Last 6 Month', 'campoal') ?></div>
                                <div class="item" data-value="360"><?php esc_html_e('Last Year', 'campoal') ?></div>
                            </div>
                        </div>
                        <div class="ui icon input visitor-chart-date-range">
                            <i class="calendar icon"></i>
                            <input type="text" name="visitor_chart_date_range" id="visitor_chart_date_range" value="">
                            <input type="hidden" name="visitor_chart_date_range_start" id="visitor_chart_date_range_start" value="">
                            <input type="hidden" name="visitor_chart_date_range_end" id="visitor_chart_date_range_end" value="">
                            <button class="display none" id="visitor_chart_date_apply"></button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="ui hidden divider"></div>

            <canvas id="viewsChart" width="1000" height="380"></canvas>

            <div class="ui section divider"></div>

            <div class="ui grid">
                <div class="sixteen wide mobile wide eight wide tablet eight wide computer column">
                    <h2 class="ui dividing header widget-title"><?php echo esc_html( conikal_format_number('%!,0i', $vistors_count, true) . ' ' . _n( 'Visitor', 'Visitors', $vistors_count, 'campoal') ); ?></h2>
                    <div class="ui relaxed divided list" id="users-visitors-list"></div>
                    <a class="ui basic fluid button" href="javascript:void(0)" id="load-more-visitors" data-type="visitors" data-paged="2">
                        <?php esc_html_e('View more', 'campoal') ?>
                    </a>
                </div>
                <div class="sixteen wide mobile wide eight wide tablet eight wide computer column">
                    <h2 class="ui dividing header widget-title"><?php echo esc_html( conikal_format_number('%!.0i', $sign) ) . ' ' . _n( 'supporter', 'supporters', $sign, 'campoal'); ?></h2>
                    <div class="ui relaxed divided list" id="supporters-list"></div>
                    <a class="ui basic fluid button" href="<?php echo esc_url( add_query_arg(array('type' => 'supporter', 'id' => $edit_id), $supporters_list_page) ) ?>" data-bjax>
                        <?php esc_html_e('All supporters', 'campoal') ?>
                    </a>
                </div>
            </div>
        </div>
    <?php endif; ?>
        <!-- Decision makers -->
        <?php 
        $all_decisionmakers = conikal_get_decistion_makers($edit_id);
        $decisionmakers_number = !empty($all_decisionmakers) ? count($all_decisionmakers) : 1;
        if(!empty($all_decisionmakers) && $receiver_step == 1) { ?>
        <div class="ui padded secondary segment">
            <div class="ui dividing header widget-title"><?php echo esc_html( _n('Decision Maker', 'Decision Makers', $decisionmakers_number, 'campoal') ); ?></div>
            <div class="ui four column stackable grid">
                <?php
                    foreach ($all_decisionmakers as $decisionmakers) {
                        $decisionmakers = (object) $decisionmakers;
                     ?>
                    <div class="column">
                        <div class="ui fluid card">
                            <div class="content">
                                <img class="right floated mini ui circular image lazy" data-src="<?php echo esc_url($decisionmakers->avatar) ?>" data-retina="<?php echo esc_url($decisionmakers->avatar_retina) ?>" src="<?php echo esc_url($lazy_placeholder_avatar) ?>">
                                <div class="header truncate">
                                    <?php if ($decisionmakers->user_id) { ?>
                                        <a href="<?php echo esc_url($decisionmakers->url) ?>" data-bjax><?php echo esc_html($decisionmakers->name) ?></a>
                                    <?php } else {
                                        echo esc_html($decisionmakers->name);
                                    } ?>
                                </div>
                                <div class="meta truncate">
                                    <?php echo esc_html($decisionmakers->position) ?>
                                </div>
                            </div>
                            <div class="extra content">
                                <div class="ui primary small fluid button invite-decision-response" data-name="<?php echo esc_attr($decisionmakers->name) ?>" data-email="<?php echo esc_attr($decisionmakers->email) ?>"><i class="paper plane icon"></i><?php esc_html_e('Invite Response', 'campoal') ?></div>
                            </div>
                      </div>
                    </div>
                <?php } ?>
            </div>
        </div>
        <?php } ?>

        <!-- Multi author -->
        <div class="ui padded secondary segment">
            <div class="ui dividing header widget-title"><?php echo esc_html( _n('Author Team', 'Authors Team', $co_authors_number, 'campoal') ) ?></div>
            <div class="ui four column stackable grid">
                <div class="column">
                    <div class="ui fluid card">
                        <div class="content">
                            <img class="right floated mini ui circular image lazy" data-src="<?php echo esc_url($author_avatar) ?>" data-retina="<?php echo esc_url($author_avatar_retina) ?>" src="<?php echo esc_url($lazy_placeholder_avatar) ?>">
                            <div class="header truncate">
                                <a href="<?php echo get_author_posts_url($author_ID)  ?>" data-bjax><?php echo esc_html($author_name) ?></a>
                            </div>
                            <div class="meta">
                                <?php echo esc_html__('Author', 'campoal') ?>
                            </div>
                        </div>
                        <div class="extra content">
                            <button class="ui small fluid button"><i class="checkmark icon"></i><?php esc_html_e('Campaign Owner', 'campoal') ?></button>
                        </div>
                    </div>
                </div>
                <?php 
                if ($co_authors) {
                    foreach ($co_authors as $id) {
                        $user = get_userdata($id);
                        if ($user) {
                            $user_id            = $user->ID;
                            $user_login         = $user->user_login;
                            $user_name          = $user->display_name;
                            $user_avatar        = conikal_get_avatar_url( $user_id, array('size' => 64) );
                            $user_avatar_retina = conikal_get_avatar_url( $user_id, array('size' => 128) );
                ?>
                    <div class="column" id="co-author-<?php echo esc_attr($user_login) ?>">
                        <div class="ui fluid card">
                            <div class="content">
                                <img class="right floated mini ui circular image lazy" data-src="<?php echo esc_url($user_avatar) ?>" data-src="<?php echo esc_url($user_avatar_retina) ?>" src="<?php echo esc_url($lazy_placeholder_avatar) ?>">
                                <div class="header">
                                    <a href="<?php echo get_author_posts_url($user_id)  ?>" data-bjax><?php echo esc_html($user_name) ?></a>
                                </div>
                                <div class="meta">
                                    <?php echo esc_html__('Co-Author', 'campoal') ?>
                                </div>
                            </div>
                            <div class="extra content">
                            <?php if ($post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator') || ($current_user->user_login == $user_login)) { ?>
                                <button class="ui primary small fluid button remove-co-author" data-id="<?php echo esc_attr($edit_id) ?>" data-author="<?php echo esc_attr($user_login) ?>" data-name="<?php echo esc_attr($user_name) ?>"><i class="remove icon"></i><?php esc_html_e('Remove Author', 'campoal') ?></button>
                            <?php } else { ?>
                                <button class="ui small fluid button"><i class="remove icon"></i><?php esc_html_e('Remove Author', 'campoal') ?></button>
                            <?php } ?>
                            </div>
                        </div>
                    </div>
                <?php   }
                    }
                } ?>
                <?php if ($post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator')) { ?>
                <div class="column">
                    <div class="box-upload">
                        <div class="ui basic center aligned segment">
                            <div class="add-author-placeholder">
                                <button class="ui small basic button add-co-author"><i class="plus icon"></i><?php esc_html_e('Add Co-Author', 'campoal') ?></button>
                            </div>
                        </div>
                    </div>
                </div>
                <?php } ?>
            </div>
        </div>

        <div class="ui grid">
            <div class="sixteen wide mobile wide eight wide tablet eight wide computer column">
                <div class="ui raised padded segment">
                <h2 class="ui dividing header widget-title"><?php echo esc_html( conikal_format_number('%!,0i', $video_players, true) . ' ' . _n( 'Video Viewer', 'Video Viewers', $video_players, 'campoal') ); ?></h2>
                    <div class="ui relaxed divided list" id="users-players-list"></div>
                    <a class="ui basic fluid button" href="javascript:void(0)" id="load-more-players" data-type="players" data-paged="2">
                        <?php esc_html_e('View more', 'campoal') ?>
                    </a>
                </div>
            </div>
            <div class="sixteen wide mobile wide eight wide tablet eight wide computer column">
                <div class="ui raised padded segment">
                    <canvas id="regionsChart" width="1000" height="970"></canvas>
                </div>
            </div>
        </div>
        <?php if ($sign > 0) { ?>
        <div class="ui grid">
            <div class="sixteen wide mobile wide eight wide tablet eight wide computer column">
                <div class="ui raised padded segment">
                    <canvas id="genderChart" width="1000" height="700"></canvas>
                </div>
            </div>
            <div class="sixteen wide mobile wide eight wide tablet eight wide computer column">
                <div class="ui raised padded segment">
                    <canvas id="ageChart" width="1000" height="700"></canvas>
                </div>
            </div>
        </div>
        <?php } ?>
    </div>

<div class="ui small modal" id="invite-modal">
    <i class="close icon"></i>
    <div class="content">
        <div class="ui header"><?php esc_html_e('Invitation letter', 'campoal') ?></div>
        <div id="invitation-response" class="respon-message"></div>
        <div class="ui form">
            <input type="hidden" name="user_email" id="user_email" value="<?php echo esc_attr($current_user->user_email) ?>">
            <input type="hidden" name="invite_name" id="invite_name" value="<?php echo esc_attr($current_user->display_name) ?>">
            <input type="hidden" name="invite_title" id="invite_title" value="<?php echo esc_attr($title) ?>">
            <input type="hidden" name="invite_decision_name" id="invite_decision_name" value="">
            <div class="field">
                <label><?php esc_html_e('Subject', 'campoal') ?></label>
                <input type="text" name="invite_subject" id="invite_subject" value="<?php echo esc_attr__('Invite Response for', 'campoal') . ' ' . esc_html($title) ?>" class="ui fluid input">
            </div>
            <div class="field">
                <label><?php esc_html_e("Decision Maker's Email", 'campoal') ?></label>
                <input type="text" name="invite_email" id="invite_email" value="" class="ui fluid input">
            </div>
            <label><?php esc_html_e('Content', 'campoal') ?></label>
            <div class="field">
                <textarea class="auto-height font medium" id="invite_message">
                    <?php echo esc_textarea(isset($letter) ? str_replace(array("<br />","<br>","<br/>", "<p>", "</p>"), "\n", $letter) : $title) ?>
                </textarea>
                <input type="hidden" name="invite_link" id="invite_link" value="<?php echo esc_url($link) ?>">
            </div>
        </div>
        <div class="ui hidden divider"></div>
        <div class="ui grid">
            <div class="sixteen right aligned column">
                <div class="ui cancel button" id="invita-cancel"><?php esc_html_e('Cancel', 'campoal') ?></div>
                <div class="ui positive right labeled icon primary button" id="send-invite" data-id="<?php echo esc_attr($edit_id) ?>"><?php esc_html_e('Send', 'campoal') ?><i class="paper plane icon"></i></div>
            </div>
        </div>
    </div>
</div>

<div class="ui small modal" id="add-co-author-modal">
    <i class="close icon"></i>
    <div class="ui header"><?php esc_html_e('Add new Co-Author', 'campoal') ?></div>
    <div class="content">
        <div class="description">
            <p class="font medium">
            <?php esc_html_e('By adding new co-authors, the members in your team will have permission to control the campaign including: Editing, updating, downloading signatures.', 'campoal') ?>
            </p>
            <div id="add-co-author-response" class="respon-message"></div>
            <div class="ui form">
                <div class="field">
                    <label><?php esc_html_e('Co-Author', 'campoal') ?></label>
                    <input type="text" name="co_author" id="co_author" placeholder="<?php esc_attr_e('Username or Email', 'campoal') ?>">
                </div>
            </div>
        </div>
        <div class="ui hidden divider"></div>
        <div class="ui grid">
            <div class="sixteen right aligned column">
                <div class="ui cancel button" id="add-co-author-cancel"><?php esc_html_e('Cancel', 'campoal') ?></div>
                <div class="ui positive right labeled icon primary button" id="add-co-author-btn" data-id="<?php echo esc_attr($edit_id) ?>"><?php esc_html_e('Add Co-Author', 'campoal') ?><i class="plus icon"></i></div>
            </div>
        </div>
    </div>
</div>

<div class="ui small modal" id="remove-co-author-confirm">
    <i class="close icon"></i>
    <div class="content">
        <div class="ui header"><?php esc_html_e('Are you sure remove co-author', 'campoal') ?> <span id="co-author-name"></span></div>
    </div>
    <div class="actions">
        <div class="ui cancel button"><?php esc_html_e('No', 'campoal') ?></div>
        <div class="ui positive right labeled icon button" id="remove-co-author-btn" data-id="<?php echo esc_attr($edit_id) ?>" data-author=""><?php esc_html_e('Remove Co-Author', 'campoal') ?><i class="checkmark icon"></i></div>
    </div>
</div>

<div class="ui small modal" id="set-goal-confirm">
    <i class="close icon"></i>
    <div class="content">
        <div class="ui header"><?php esc_html_e('Are you sure change the goal is', 'campoal') ?> <span id="goal-number"></span> <?php esc_html_e('signatures', 'campoal') ?></div>
    </div>
    <div class="actions">
        <div class="ui cancel button"><?php esc_html_e('No', 'campoal') ?></div>
        <div class="ui positive right labeled icon button" id="approve-goal" data-id="<?php echo esc_attr($edit_id) ?>"><?php esc_html_e('Yes', 'campoal') ?><i class="checkmark icon"></i></div>
    </div>
</div>

<div class="ui small modal" id="set-auto-increase-confirm">
    <i class="close icon"></i>
    <div class="content">
        <div class="ui header"><?php esc_html_e('Are you sure change the auto increase goal percent?', 'campoal') ?></div>
    </div>
    <div class="actions">
        <div class="ui cancel button"><?php esc_html_e('No', 'campoal') ?></div>
        <div class="ui positive right labeled icon button" id="set-auto-increase-btn" data-id="<?php echo esc_attr($edit_id) ?>"><?php esc_html_e('Yes', 'campoal') ?><i class="checkmark icon"></i></div>
    </div>
</div>

<div class="ui small modal" id="reopen-confirm">
    <i class="close icon"></i>
    <div class="content">
        <div class="ui header"><?php esc_html_e('Are you sure reopen this petition', 'campoal') ?></div>
    </div>
    <div class="actions">
        <div class="ui cancel button"><?php esc_html_e('No', 'campoal') ?></div>
        <div class="ui positive right labeled icon button" id="approve-reopen" data-id="<?php echo esc_attr($edit_id) ?>"><?php esc_html_e('Yes', 'campoal') ?><i class="checkmark icon"></i></div>
    </div>
</div>

<div class="ui small modal" id="close-confirm">
    <i class="close icon"></i>
    <div class="content">
        <div class="ui header"><?php esc_html_e('Are you sure close this petition', 'campoal') ?></div>
    </div>
    <div class="actions">
        <div class="ui cancel button"><?php esc_html_e('No', 'campoal') ?></div>
        <div class="ui positive right labeled icon button" id="approve-close" data-id="<?php echo esc_attr($edit_id) ?>"><?php esc_html_e('Yes', 'campoal') ?><i class="checkmark icon"></i></div>
    </div>
</div>

<input type="hidden" name="petition_id" id="petition_id" value="<?php echo esc_attr($edit_id) ?>">
<?php wp_nonce_field('dashboard_petition_ajax_nonce', 'securityDashboard', true); ?>
<?php wp_nonce_field('invitation_ajax_nonce', 'securityInvitation', true); ?>
<?php } else { ?>
    <?php get_template_part('404') ?>
<?php } ?>