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

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

$updates_per_page   = isset($campoal_options['conikal_updates_per_page_field']) ? $campoal_options['conikal_updates_per_page_field'] : 5;

$petition_id        = $post->ID;
$decisionmakers     = get_post_meta($petition_id, 'petition_decisionmakers', true);
$decisionmakers     = array_unique(explode(',', $decisionmakers));
$time               = human_time_diff(get_the_time('U', $petition_id), current_time('timestamp'));
$avatar             = conikal_get_avatar_url( $post->post_author, array('size' => 64) );
$avatar_retina      = conikal_get_avatar_url( $post->post_author, array('size' => 128) );
$author_name        = conikal_get_author_name($post->post_author);

// lazy placeholder
$lazy_placeholder_avatar = conikal_lazy_load_placeholder('avatar');
?>

    <div class="petition-updates-section">
        <?php if ( (get_the_author_meta( 'ID' ) == $current_user->ID || current_user_can('editor') || current_user_can('administrator') || in_array($current_user->ID, $decisionmakers) || conikal_co_author_permission($petition_id)) && is_user_logged_in() ) { ?>
            <div class="ui secondary segment">
                <div class="ui grid">
                    <div class="sixteen wide mobile six wide tablet five wide computer column">
                        <?php
                        $page_dashboard_petition = isset($campoal_options['conikal_page_dashboard_petition_field']) ? $campoal_options['conikal_page_dashboard_petition_field'] : 0;
                        $page_dashboard_petition = $page_dashboard_petition ? get_permalink($page_dashboard_petition) : '';
                        $params_link = array('section' => 'add_update', 'id' => $petition_id);
                        if ( in_array($current_user->ID, $decisionmakers) && !current_user_can('editor') && !current_user_can('administrator') ) {
                            $params_link['type'] = 'responsive';
                        } else {
                            $params_link['type'] = 'update';
                        }
                        $add_update_link = add_query_arg($params_link, $page_dashboard_petition);
                        if ($add_update_link) { ?>
                            <a class="ui large primary fluid button" href="<?php echo esc_url($add_update_link); ?>" data-bjax>
                                <?php (in_array($current_user->ID, $decisionmakers) && !current_user_can('editor') && !current_user_can('administrator') ? esc_html_e('Response', 'campoal') : esc_html_e('Post Update', 'campoal')) ?>        
                            </a>
                        <?php } ?>
                    </div>
                    <div class="sixteen wide mobile ten wide tablet eleven wide computer column">
                        <?php if (in_array($current_user->ID, $decisionmakers) && !current_user_can('editor') && !current_user_can('administrator')) { ?>
                            <span class="text grey"><?php esc_html_e('This petition is sent to you, let you clearly respond the problems stated  in this petition for supporters of petition.', 'campoal') ?></span>
                        <?php } else { ?>
                            <span class="text grey"><?php esc_html_e('Keep your supporters engaged with a news update. Every update you post will be sent as a separate email to signers of your petition.', 'campoal') ?></span>
                        <?php } ?>
                    </div>
                </div>
            </div>
        <?php } ?>
        <div id="updates<?php echo esc_attr($petition_id) ?>" class="update-list" data-id="<?php echo esc_attr($petition_id) ?>" data-load="0">
            <h2 class="ui dividing header"><?php esc_html_e('Updates', 'campoal') ?></h2>
            <div id="update-list<?php echo esc_attr($petition_id) ?>"></div>
            <div class="ui hidden divider"></div>
            <div class="ui basic fluid button updates-more" id="updates-more<?php echo esc_attr($petition_id) ?>" data-offset="<?php echo esc_attr($updates_per_page) ?>" data-id="<?php echo esc_attr($petition_id) ?>"><i class="long arrow down icon"></i><?php esc_html_e('More updates...', 'campoal') ?></div>
        </div>

        <!-- AUTHOR -->
        <div class="ui secondary segment petition-author-content">
            <div class="ui small header">
                <img class="ui circular image lazy" data-src="<?php echo esc_url($avatar); ?>" data-retina="<?php echo esc_url($avatar_retina); ?>" src="<?php echo esc_url($lazy_placeholder_avatar); ?>" alt="<?php echo esc_attr($author_name) ?>">
                <div class="content">
                    <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' )); ?>" class="header truncate" data-bjax>
                        <?php echo esc_html($author_name) ?>
                    </a>
                    <div class="sub header truncate">
                        <strong><?php esc_html_e('Started this petition', 'campoal') ?></strong>
                        <span class="text grey"><?php echo sprintf(esc_html__('%s ago', 'campoal'), esc_html($time)) ?></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>