<?php
/**
 * Plugin Name: Tabbed Posts (thumbs) – SEM GURU
 * Description: Widget com abas Popular / Recent / Comments com miniaturas, no estilo do Dazzling.
 * Version: 1.0.0
 * Author: SEM GURU
 */

if ( ! defined( 'ABSPATH' ) ) exit;

class SG_Tabbed_Posts_Thumbs extends WP_Widget {

  public function __construct() {
    parent::__construct(
      'sg_tabbed_posts_thumbs',
      'Tabbed Posts (thumbs) – SEM GURU',
      array( 'description' => 'Popular / Recent / Comments com miniaturas (usa Imagem Destacada e Avatares).' )
    );

    // CSS mínimo embutido (pra ser arquivo único)
    add_action('wp_head', function () {
      ?>
      <style id="sg-tabbed-thumbs-css">
        .sg-tabs { margin:0; padding:0; }
        .sg-tabs .nav-tabs { border-bottom:1px solid #eee; margin:0; }
        .sg-tabs .nav-tabs > li > a { text-transform:uppercase; font-size:12px; padding:8px 12px; }
        .sg-tabs .tab-content { border:1px solid #E8E8E8; border-top:none; padding:10px; }
        .sg-tabs ul.sg-list { list-style:none; margin:0; padding:0; }
        .sg-tabs ul.sg-list li { display:flex; gap:10px; align-items:center; border-bottom:1px solid #f0f0f0; padding:6px 0; }
        .sg-tabs ul.sg-list li:last-child { border-bottom:0; }
        .sg-tabs .sg-thumb img { width:60px; height:60px; object-fit:cover; display:block; }
        .sg-tabs .sg-no-thumb { width:60px; height:60px; background:#eee; display:block; }
        .sg-tabs .sg-entry { line-height:1.3; min-width:0; }
        .sg-tabs .sg-entry a.sg-title { display:block; color:#333; font-weight:600; }
        .sg-tabs .sg-entry a.sg-title:hover { color:#1FA67A; }
        .sg-tabs .sg-meta { font-size:12px; color:#999; margin-top:2px; }
        .sg-tabs .sg-comment { display:flex; gap:10px; align-items:center; }
        .sg-tabs .sg-comment .avatar { width:40px; height:40px; border-radius:50%; }
        .sg-tabs .sg-comment .sg-centry { line-height:1.2; }
        .sg-tabs .sg-comment .sg-cname { font-weight:600; color:#333; }
        .sg-tabs .sg-comment .sg-cexcerpt { font-size:12px; color:#666; display:block; margin-top:2px; overflow:hidden; text-overflow:ellipsis; }
      </style>
      <?php
    });
  }

  public function form( $instance ) {
    $popular_title = isset($instance['popular_title']) ? $instance['popular_title'] : 'POPULAR';
    $recent_title  = isset($instance['recent_title'])  ? $instance['recent_title']  : 'RECENT';
    $comments_title= isset($instance['comments_title'])? $instance['comments_title']: 'COMMENTS';
    $count         = isset($instance['count']) ? absint($instance['count']) : 5;
    ?>
    <p><label>Título (Popular):<br>
      <input class="widefat" name="<?php echo esc_attr($this->get_field_name('popular_title')); ?>" type="text" value="<?php echo esc_attr($popular_title); ?>"></label></p>
    <p><label>Título (Recent):<br>
      <input class="widefat" name="<?php echo esc_attr($this->get_field_name('recent_title')); ?>" type="text" value="<?php echo esc_attr($recent_title); ?>"></label></p>
    <p><label>Título (Comments):<br>
      <input class="widefat" name="<?php echo esc_attr($this->get_field_name('comments_title')); ?>" type="text" value="<?php echo esc_attr($comments_title); ?>"></label></p>
    <p><label>Quantidade por aba:<br>
      <input name="<?php echo esc_attr($this->get_field_name('count')); ?>" type="number" min="1" max="20" value="<?php echo esc_attr($count); ?>" style="width:90px"></label></p>
    <?php
  }

  public function update( $new, $old ) {
    $out = array();
    $out['popular_title']  = sanitize_text_field( $new['popular_title'] );
    $out['recent_title']   = sanitize_text_field( $new['recent_title'] );
    $out['comments_title'] = sanitize_text_field( $new['comments_title'] );
    $out['count']          = absint( $new['count'] );
    return $out;
  }

  public function widget( $args, $instance ) {
    $popular_title  = !empty($instance['popular_title'])  ? $instance['popular_title']  : 'POPULAR';
    $recent_title   = !empty($instance['recent_title'])   ? $instance['recent_title']   : 'RECENT';
    $comments_title = !empty($instance['comments_title']) ? $instance['comments_title'] : 'COMMENTS';
    $count          = !empty($instance['count'])          ? absint($instance['count'])  : 5;

    echo $args['before_widget'];

    // Nav das abas (Bootstrap 3 do Dazzling já cuida do JS)
    ?>
    <div class="sg-tabs">
      <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active">
          <a href="#sg-popular-<?php echo esc_attr($this->number); ?>" aria-controls="sg-popular" role="tab" data-toggle="tab"><?php echo esc_html($popular_title); ?></a>
        </li>
        <li role="presentation">
          <a href="#sg-recent-<?php echo esc_attr($this->number); ?>" aria-controls="sg-recent" role="tab" data-toggle="tab"><?php echo esc_html($recent_title); ?></a>
        </li>
        <li role="presentation">
          <a href="#sg-comments-<?php echo esc_attr($this->number); ?>" aria-controls="sg-comments" role="tab" data-toggle="tab"><?php echo esc_html($comments_title); ?></a>
        </li>
      </ul>

      <div class="tab-content">
        <!-- POPULAR -->
        <div role="tabpanel" class="tab-pane active" id="sg-popular-<?php echo esc_attr($this->number); ?>">
          <ul class="sg-list">
            <?php
            $q = new WP_Query(array(
              'posts_per_page' => $count,
              'orderby' => 'comment_count',
              'order' => 'DESC',
              'ignore_sticky_posts' => 1,
              'post_status' => 'publish'
            ));
            while ($q->have_posts()) : $q->the_post(); ?>
              <li>
                <a class="sg-thumb" href="<?php the_permalink(); ?>">
                  <?php if ( has_post_thumbnail() ) {
                    the_post_thumbnail('thumbnail', array('loading'=>'lazy'));
                  } else {
                    echo '<span class="sg-no-thumb"></span>';
                  } ?>
                </a>
                <div class="sg-entry">
                  <a class="sg-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                  <div class="sg-meta"><?php echo esc_html( get_the_date() ); ?></div>
                </div>
              </li>
            <?php endwhile; wp_reset_postdata(); ?>
          </ul>
        </div>

        <!-- RECENT -->
        <div role="tabpanel" class="tab-pane" id="sg-recent-<?php echo esc_attr($this->number); ?>">
          <ul class="sg-list">
            <?php
            $q = new WP_Query(array(
              'posts_per_page' => $count,
              'ignore_sticky_posts' => 1,
              'post_status' => 'publish'
            ));
            while ($q->have_posts()) : $q->the_post(); ?>
              <li>
                <a class="sg-thumb" href="<?php the_permalink(); ?>">
                  <?php if ( has_post_thumbnail() ) {
                    the_post_thumbnail('thumbnail', array('loading'=>'lazy'));
                  } else {
                    echo '<span class="sg-no-thumb"></span>';
                  } ?>
                </a>
                <div class="sg-entry">
                  <a class="sg-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                  <div class="sg-meta"><?php echo esc_html( get_the_date() ); ?></div>
                </div>
              </li>
            <?php endwhile; wp_reset_postdata(); ?>
          </ul>
        </div>

        <!-- COMMENTS -->
        <div role="tabpanel" class="tab-pane" id="sg-comments-<?php echo esc_attr($this->number); ?>">
          <ul class="sg-list">
            <?php
            $comments = get_comments( array(
              'number'  => $count,
              'status'  => 'approve',
              'type'    => 'comment'
            ));
            foreach ( $comments as $c ) :
              $link = get_comment_link( $c );
              $author = get_comment_author( $c );
              $excerpt = wp_trim_words( wp_strip_all_tags( $c->comment_content ), 16, '…' );
              $post_title = get_the_title( $c->comment_post_ID );
              ?>
              <li>
                <div class="sg-comment">
                  <?php echo get_avatar( $c, 40 ); ?>
                  <div class="sg-centry">
                    <span class="sg-cname"><?php echo esc_html( $author ); ?></span>
                    <span class="sg-cexcerpt">
                      <a href="<?php echo esc_url( $link ); ?>">
                        <?php echo esc_html( $excerpt ); ?>
                      </a>
                    </span>
                    <div class="sg-meta"><?php echo esc_html( $post_title ); ?></div>
                  </div>
                </div>
              </li>
            <?php endforeach; ?>
          </ul>
        </div>

      </div>
    </div>
    <?php

    echo $args['after_widget'];
  }
}

add_action( 'widgets_init', function () {
  register_widget( 'SG_Tabbed_Posts_Thumbs' );
});
