<?php
/**
 * Widget for displaying Highlight Posts (carousel) with prev/next controls
 *
 * @package Purenote
 */

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

class Purenote_Highlights_Widget extends WP_Widget {

    public function __construct() {
        $widget_ops = array(
            'classname'                   => 'purenote_highlights_widget',
            'description'                 => __( 'Displays a highlight post carousel with image, category, title, and date.', 'purenote' ),
            'customize_selective_refresh' => true,
        );
        parent::__construct( 'purenote_highlights_widget', __( 'PureNote: Highlights', 'purenote' ), $widget_ops );
    }

    private function widget_fields() {
        $fields = array(
            'widget_title' => array(
                'widget_field_name'    => 'widget_title',
                'widget_field_title'   => __( 'Widget Title', 'purenote'),
                'widget_field_default' => __( 'Highlights', 'purenote'),
                'widget_field_type'    => 'title',
            ),
            'posts_count' => array(
                'widget_field_name'    => 'posts_count',
                'widget_field_title'   => __( 'Number of posts', 'purenote' ),
                'widget_field_default' => 5,
                'widget_field_type'    => 'number',
                'input_attr'           => array(
                    'min'  => 1,
                    'max'  => 10,
                    'step' => 1
                )
            ),
        );
        return $fields;
    }

    public function widget( $args, $instance ) {
        extract( $args );

        $widget_title = ! empty( $instance['widget_title'] ) ? $instance['widget_title'] : __( 'Highlights', 'purenote' );
        $posts_count  = ! empty( $instance['posts_count'] ) ? absint( $instance['posts_count'] ) : 5;

        $query_args = array(
            'posts_per_page'      => $posts_count,
            'ignore_sticky_posts' => true,
        );

        $highlight_query = new WP_Query( $query_args );

        if ( ! $highlight_query->have_posts() ) {
            return;
        }

        echo $before_widget;
    ?>
    <div class="carousel-posts-wrap purenote-card">
        <?php if ( ! empty( $widget_title ) ) : ?>
            <h3 class="widget-title">
                <span><?php echo wp_kses_post( $widget_title ); ?></span>
            </h3>
        <?php endif; ?>

        <div class="highlight-slider-wrapper">
    <div class="swiper-wrapper">
        <?php while ( $highlight_query->have_posts() ) : $highlight_query->the_post(); ?>
            <article class="post-item swiper-slide">
                <figure class="post-thumb-wrap">
                            <a href="<?php the_permalink(); ?>">
                                <?php if ( has_post_thumbnail() ) the_post_thumbnail( 'large' ); ?>
                                <div class="thumb-overlay"></div>
                            </a>
                            <ul class="post-categories" id="cat-wrap">
                                <?php
                                $categories = get_the_category();
                                if ( $categories ) {
                                    foreach ( $categories as $cat ) {
                                      echo '<li class="post-cat-item cat-'.esc_attr($cat->term_id).'"><a href="'.esc_url(get_category_link($cat->term_id)).'">'.esc_html($cat->name).'</a></li>';
                                    }
                                }
                                ?>
                            </ul>
                        </figure>
                        <div class="post-element">
                            <h2 class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                            <div class="post-meta">
                                <span class="post-date">
                                    <i class="fas fa-calendar-days"></i>
                                    <a href="<?php echo get_month_link(get_the_date('Y'), get_the_date('m')); ?>" rel="bookmark">
                                        <time datetime="<?php echo get_the_date('c'); ?>"><?php echo get_the_date(); ?></time>
                                    </a>
                                </span>
                            </div>
                        </div>
                    </article>
                <?php endwhile; ?>
            </div>

            <div class="custom-button-prev"><i class="fas fa-chevron-left"></i></div>
            <div class="custom-button-next"><i class="fas fa-chevron-right"></i></div>
        </div>
    </div>
    
    <?php
        echo $after_widget;
        wp_reset_postdata();
    }

    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $widget_fields = $this->widget_fields();

        foreach ( $widget_fields as $widget_field ) {
            extract( $widget_field );
            $instance[$widget_field_name] = purenote_widget_updated_field_value( $widget_field, $new_instance[$widget_field_name] );
        }
        return $instance;
    }

    public function form( $instance ) {
        $widget_fields = $this->widget_fields();

        foreach ( $widget_fields as $widget_field ) {
            extract( $widget_field );
            $widget_field_value = isset( $instance[$widget_field_name] ) ? $instance[$widget_field_name] : $widget_field_default;
            purenote_show_widget_field( $this, $widget_field, $widget_field_value );
        }
    }
}
