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

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

$update_posts = conikal_update_petition($post->ID);
$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) : '';

if($update_posts) {
    $total_p = $update_posts->found_posts;
} else {
    $total_p = 0;
}
?>
<div id="update-list" class="update-list">
    <?php
    if ($update_posts->have_posts()) : 
        while ( $update_posts->have_posts() ) :
            $update_posts->the_post();
            $update_id = get_the_ID();
            $link = get_permalink($update_id);
            $title = get_the_title($update_id);
            $excerpt = conikal_get_excerpt_by_id($update_id);
            $date = get_the_date('', $update_id);
            $time = human_time_diff(get_the_time('U', $update_id), current_time('timestamp'));
            $type =  get_post_meta($update_id, 'update_type', true);
            $petition_id =  get_post_meta($update_id, 'update_post_id', true);
            $media = get_post_meta($update_id, 'update_media', true);
            $gallery = get_post_meta($update_id, 'update_gallery', true);
            $images = conikal_get_gallery_images($gallery);
            $thumb_id = get_post_thumbnail_id($update_id);
            $thumbnail = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
            $thumb_video = get_post_meta($id, 'update_thumb_small', true);
            $comments = wp_count_comments($update_id);

            $content_post = get_post($update_id);
            $content = $content_post->post_content;
            $content = apply_filters('the_content', $content);
            $content = str_replace(']]>', ']]&gt;', $content);

            $update_type_title = conikal_get_update_type($type);
        ?>

            <?php if($type == 'victory') {  ?>
                <div class="ui fluid card" id="update-<?php echo esc_attr($update_id) ?>">
                    <div class="content">
                         <?php if ( get_the_author_meta( 'ID' ) == $current_user->ID ) { ?>
                            <div class="right floated meta">
                                <div class="ui icon top right pointing dropdown edit-update">
                                    <i class="angle down icon"></i>
                                    <div class="menu">
                                        <?php
                                        $edit_update_link = add_query_arg(array('section' => 'add_update', 'id' => $petition_id, 'update_id' => $update_id), $page_dashboard_petition);
                                        if ($edit_update_link) { ?>
                                            <a class="item" href="<?php echo esc_url($edit_update_link); ?>" data-bjax><?php esc_html_e('Edit', 'campoal') ?></a>
                                        <?php } ?>

                                        <a href="javascript:void(0)" class="item delete-update" data-id="<?php echo esc_attr($update_id) ?>"><?php esc_html_e('Delete', 'campoal') ?></a>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                        <div class="ui center aligned huge icon header victory-update-title">
                            <?php echo conikal_custom_icon('victory_inverse') ?>
                            <div class="content">
                                <a href="<?php echo esc_url($link) ?>" data-bjax><?php echo esc_html($title) ?></a>
                                <div class="sub header"><?php echo esc_html($date) ?></div>
                            </div>
                        </div>
                        <div class="description font medium">
                            <?php echo esc_html($excerpt) ?>
                        </div>
                    </div>
                </div>
            <?php } else { ?>
            <div class="ui fluid card" id="update-<?php echo esc_attr($update_id) ?>">
                <div class="content">
                    <?php if ( get_the_author_meta( 'ID' ) == $current_user->ID || current_user_can('editor') || current_user_can('administrator') || conikal_co_author_permission($petition_id) && is_user_logged_in() ) { ?>
                        <div class="right floated meta">
                            <div class="ui icon top right pointing dropdown edit-update">
                                <i class="angle down icon"></i>
                                <div class="menu">
                                    <?php
                                    $edit_update_link = add_query_arg(array('section' => 'add_update', 'id' => $petition_id, 'update_id' => $update_id), $page_dashboard_petition);
                                    if ($edit_update_link) { ?>
                                        <a class="item" href="<?php echo esc_url($edit_update_link); ?>" data-bjax><?php esc_html_e('Edit', 'campoal') ?></a>
                                    <?php } ?>
                                    
                                    <a href="javascript:void(0)" class="item delete-update" data-id="<?php echo esc_attr($update_id) ?>"><?php esc_html_e('Delete', 'campoal') ?></a>
                                </div>
                            </div>
                        </div>
                    <?php } ?>
                    <div class="meta">
                        <i class="<?php echo esc_attr($update_type_title['icon']) ?>"></i><?php echo esc_html($update_type_title['text']) ?>
                        <div class="ui divider"></div>
                    </div>
                    <div class="description">
                        <div class="ui grid">
                            <div class="sixteen wide mobile sixteen tablet thirteen wide computer column">
                                <div class="ui header">
                                    <a href="<?php echo esc_url($link) ?>" data-bjax>
                                        <?php echo esc_html($title) ?>
                                    </a>
                                </div>
                                <p class="text grey"><?php echo esc_html($excerpt) ?></p>
                            </div>
                            <div class="three wide column computer only">
                                <a href="<?php echo esc_url($link) ?>" data-bjax>
                                    <?php if(has_post_thumbnail($update_id)) { ?>
                                        <img class="ui fluid image" src="<?php echo esc_url($thumbnail[0]) ?>" alt="<?php echo esc_attr($title) ?>">
                                    <?php } elseif ($gallery) { ?>
                                        <img class="ui fluid image" src="<?php echo esc_url($images[1]) ?>" alt="<?php echo esc_attr($title) ?>">
                                    <?php } elseif ($thumb_video) { ?>
                                        <img class="ui fluid image" src="<?php echo esc_url($thumb_video) ?>" alt="<?php echo esc_attr($title) ?>">
                                    <?php } ?>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="extra content">
                    <?php if($comments->approved != 0) { ?>
                        <span>
                            <i class="comments icon"></i>
                            <?php echo esc_html($comments->approved) . ' ' . esc_html__('comments', 'campoal'); ?>
                        </span>
                    <?php } ?>
                    <span class="right floated">
                        <?php echo sprintf(esc_html__('%s ago', 'campoal'), esc_html($time)) ?>
                    </span>
                </div>
            </div>
            <?php } ?>
    <?php endwhile;
        wp_reset_postdata();
        wp_reset_query();
    endif; ?>
</div>
<?php if ($total_p > $updates_per_page) { ?>
    <br/>
    <div class="ui basic fluid button" id="updates-more" data-offset="<?php echo esc_attr($updates_per_page) ?>"><i class="long arrow down icon"></i><?php esc_html_e('More updates...', 'campoal') ?></div>
<?php }

wp_nonce_field('update_petition_ajax_nonce', 'securityUpdate', true); ?>
