<?php

namespace core_next;
class userInfo extends \WP_Widget
{
    function __construct()
    {
        $img_html = Theme::getWidgetIcon();
        parent::__construct(
            'core_next_user_info_widget',
            $img_html . 'CoreNext文章作者信息',
            array(
                'description' => '请放到文章页面，显示当前作者信息'
            )
        );
    }

    function form($instance)
    {
        $on_top = !empty($instance['on_top']) ? 'checked' : '';
        $number = absint(isset($instance['number']) ? $instance['number'] : 6);
        ?>
        <div class="widget-item">
            <div>显示动态数量</div>
            <input class="widefat" name="<?php echo esc_attr($this->get_field_name('number')); ?>"
                   type="text" value="<?php echo esc_attr($number); ?>"/>
        </div>
        <div class="widget-item" style="display: flex;gap: 10px;z-index: 30">
            <div>跟随页面展示</div>
            <input class="widefat" type="checkbox" name="<?php echo $this->get_field_name('on_top'); ?>"
                <?php echo $on_top; ?> >
        </div>
        <?php
    }

    public function widget_start($args, $instance)
    {
        $on_top = !empty($instance['on_top']) ? 'checked' : '';
        $html = $args['before_widget'];
        if ($on_top !== '') {
            $html = str_replace('<div', Theme::getWidgetStickyStyle(), $html);
        }
        echo $html;
    }

    public function widget_end($args)
    {
        echo $args['after_widget'];
    }

    function widget($args, $instance)
    {
        $number = absint(isset($instance['number']) ? $instance['number'] : 6);
        $this->widget_start($args, $instance);
        $user_id = get_post_field('post_author', get_the_ID());
        $user = get_user_by('ID', $user_id);
        $data['display_name'] = $user->display_name;
        $data['avatar_url'] = Theme::getUserAvatar($user->user_email);
        $data['user_description'] = empty($user->description) ? '这个人很懒，签名都没有' : $user->description;
        $data['comment_count'] = WordPress::getCommentCount($user_id);
        $data['author_url'] = get_author_posts_url($user_id);
        $data['post_count'] = count_user_posts($user_id);
        $data['post_url'] = $data['author_url'];
        $data['comment_url'] = $data['author_url'] . '#/comment';
        $data['post_icon'] = Config::$img_url . '/icon/author_post.svg';
        $data['comment_icon'] = Config::$img_url . '/icon/author_comment.svg';
        echo '<div class="widget-user-info">';
        echo Template::getTemplateHtmlByDOMDocument('widget-user-info', $data);
        /* $post_list = WordPress::getUserLatestPosts($user_id);
         echo '<div class="user-post-list">';
         foreach ($post_list as $item) {
             $link = get_permalink($item->ID);
             print_r('<a href="' . $link . '" target="_blank">' . $item->post_title . '</a>');
         }
         echo '</div></div>';*/
        $active_list = WordPress::getAuthorActivity($number, $user_id);
        if (count($active_list) > 0) {
            print_r('<div class="new-title">最近动态</div>');
        }
        print_r('<div class="active-list">');
        foreach ($active_list as $item) {
            if ($item['type'] == 'comment') {
                $pre_text = '评论了:';
            } else {
                $pre_text = '发布了:';
            }
            print_r('<div class="active-item"><a href="' . $item['post_url'] . '" target="_blank">' . $pre_text . $item['post_title'] . '</a></div>');
        }
        echo '</div></div>';
        $this->widget_end($args);

    }

}