<?php
class PZY_Latest_Posts_Widget extends WP_Widget {
    public function widget($args, $instance) {
        $count = !empty($instance['count']) ? absint($instance['count']) : 5;
        $show_categories = isset($instance['show_categories']) ? (bool) $instance['show_categories'] : false;
        $show_excerpt = isset($instance['show_excerpt']) ? (bool) $instance['show_excerpt'] : false;

        echo $args['before_widget'];
        if (!empty($instance['title'])) {
            echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
        }

        $query_args = array(
            'posts_per_page' => $count,
            'post_status' => 'publish',
        );
        $latest_posts = new WP_Query($query_args);

        if ($latest_posts->have_posts()) {
            echo '<ul class="pzy_latest_posts_block">';
            while ($latest_posts->have_posts()) {
                $latest_posts->the_post();
                echo '<li>';
                echo '<div class="pzy-img-box"><img src="' . get_the_post_thumbnail_url(get_the_ID(),[80,80]) . '" alt="' . get_the_title() . '"/></div>';
                
                echo '<div class="pzy-content-wrapper">';
                echo '<a class="pzy-post-title" href="' . get_permalink() . '">' . get_the_title() . '</a>';
                if ($show_categories) {
                    echo '<div class="pzy-post-categories">' . get_the_category_list(' | ') . '</div>';
                }
                if ($show_excerpt) {
                    echo '<div class="pzy-post-excerpt">' . get_the_excerpt() . '</div>';
                }
                echo '</div>';

                echo '</li>';
            }
            echo '</ul>';
            wp_reset_postdata();
        }

        echo $args['after_widget'];
    }

    public function form($instance) {
        $title = !empty($instance['title']) ? $instance['title'] : __('دسته بندی مقالات', 'pezeshkyar');
        $count = !empty($instance['count']) ? absint($instance['count']) : 5;
        $show_categories = isset($instance['show_categories']) ? (bool) $instance['show_categories'] : false;
        $show_excerpt = isset($instance['show_excerpt']) ? (bool) $instance['show_excerpt'] : false;
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'عنوان:', 'pezeshkyar' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('چند مقاله نشان داده شود؟', 'pezeshkyar'); ?></label>
            <input class="tiny-text" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="number" step="1" min="1" value="<?php echo esc_attr($count); ?>" size="3" />
        </p>
        <p>
            <input class="checkbox" type="checkbox" <?php checked($show_categories); ?> id="<?php echo $this->get_field_id('show_categories'); ?>" name="<?php echo $this->get_field_name('show_categories'); ?>" />
            <label for="<?php echo $this->get_field_id('show_categories'); ?>"><?php _e('نمایش دسته بندی', 'pezeshkyar'); ?></label>
        </p>
        <p>
            <input class="checkbox" type="checkbox" <?php checked($show_excerpt); ?> id="<?php echo $this->get_field_id('show_excerpt'); ?>" name="<?php echo $this->get_field_name('show_excerpt'); ?>" />
            <label for="<?php echo $this->get_field_id('show_excerpt'); ?>"><?php _e('نمایش چکیده', 'pezeshkyar'); ?></label>
        </p>
        <?php
    }

    public function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['count'] = absint($new_instance['count']);
        $instance['show_categories'] = isset($new_instance['show_categories']) ? (bool) $new_instance['show_categories'] : false;
        $instance['show_excerpt'] = isset($new_instance['show_excerpt']) ? (bool) $new_instance['show_excerpt'] : false;
        return $instance;
    }
}
