<?php


if ( ! class_exists( 'moral_magazine_Post_Filter' ) ) :

    class Moral_Magazine_Post_Filter extends WP_Widget {
        /**
         * Sets up the widgets name etc
         */
        public function __construct() {
            $mt_widget_posts_filter = array(
                'classname'   => 'hero-banner-sidebar',
                'description' => esc_html__( 'Retrive Posts Filter.', 'moral-magazine' ),
                );
            parent::__construct( 'moral_magazine_Post_Filter', esc_html__( 'MT : Posts Filter', 'moral-magazine' ), $mt_widget_posts_filter );
        }

        /**
         * Outputs the content of the widget
         *
         * @param array $args
         * @param array $instance
         */
        public function widget( $args, $instance ) {
            
            $mt_category[] = isset( $instance['category_one'] ) ? $instance['category_one'] : ''; 
            $mt_category[] = isset( $instance['category_two'] ) ? $instance['category_two'] : ''; 
            $mt_category[] = isset( $instance['category_three'] ) ? $instance['category_three'] : ''; 
            ?>
            <div class="hero-banner-sidebar">
                <div class="sidebar-tab">
                    <ul class="tabs section-header">
                        <?php 
                        $i = 1 ;
                        foreach ( $mt_category as $value ) :
                            ?>
                        <li data-tab="<?php echo esc_attr( $value ) ;?>" class="<?php echo $i == 1 ? 'active': '' ?>">
                            <h2 class="section-title">
                                <?php echo esc_html( strtoupper( get_cat_name( $value ) ) ) ; ?>
                            </h2>
                        </li>
                        <?php $i++; endforeach; ?>
                    </ul>
                </div>
                <div class="sidebar-post">
                    <?php
                        $i = 1 ; 
                        foreach ( $mt_category as $value ) {
                    ?>
                    <div id="<?php echo esc_attr( $value ) ?>" class="tab-content <?php echo $i == 1 ? 'active': '' ?>">
                        <?php 
                            $cat_id = $value;
                            $args = array(
                                'post_type'         => 'post',
                                'posts_per_page'    => 5,
                                'cat'               => absint( $cat_id ),
                                );
                            $query = new WP_Query( $args );
                            if ( $query->have_posts() ) : 
                                while ( $query->have_posts() ) : $query->the_post(); 

                            $image = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'post-thumbnail' ) : '';
                        ?>
                        <article>
                            <div class="sidebar-post-wrapper">
                                <div class="featured-image">
                                    <img src="<?php echo esc_url($image); ?>" alt="<?php the_title(); ?>">
                                </div>
                                <div class="entry-container">
                                    <header class="entry-header">
                                        <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                                    </header>
                                    <div class="entry-meta">
                                        <?php moral_magazine_posted_on( get_the_id() ); ?>
                                        
                                        <span class="min-read"><?php echo moral_magazine_time_interval( get_the_content( get_the_id() ) ); echo esc_html__(' min read', 'moral-magazine'); ?></span>
                                    </div>
                                </div><!-- .entry-container -->
                            </div>
                        </article>
                        <?php endwhile;
                            endif;
                            wp_reset_postdata();
                        ?>
                    </div>
                    <?php
                          $i++;  
                        }
                    ?> 
                </div> 
            </div>         

        <?php
           
            
        }

        /**
         * Outputs the options form on admin
         *
         * @param array $instance The widget options
         */
        public function form( $instance ) {
        ?>
           <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'category_one' ) ); ?>"><?php esc_html_e( 'Select Category One', 'moral-magazine' ); ?></label>
                <select id="<?php echo esc_attr( $this->get_field_id('category_one') ); ?>" name="<?php echo $this->get_field_name('category_one'); ?>" class="widefat" style="width:100%;">

                    <?php 
                    $categories = moral_magazine_get_post_cat_choices();
                    foreach($categories as $category => $value) { ?>
                    <option value="<?php echo absint( $category ); ?>" <?php selected( absint( $instance['category_one'] ), $category ); ?>><?php echo esc_html( $value ); ?></option>
                    <?php } ?>      
                </select>
            </p>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'category_two' ) ); ?>"><?php esc_html_e( 'Select Category Two', 'moral-magazine' ); ?></label>
                <select id="<?php echo esc_attr( $this->get_field_id('category_two') ); ?>" name="<?php echo $this->get_field_name('category_two'); ?>" class="widefat" style="width:100%;">

                    <?php 
                    $categories = moral_magazine_get_post_cat_choices();
                    foreach($categories as $category => $value) { ?>
                    <option value="<?php echo absint( $category ); ?>" <?php selected( absint( $instance['category_two'] ), $category ); ?>><?php echo esc_html( $value ); ?></option>
                    <?php } ?>      
                </select>
            </p>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'category_three' ) ); ?>"><?php esc_html_e( 'Select Category Three', 'moral-magazine' ); ?></label>
                <select id="<?php echo esc_attr( $this->get_field_id('category_three') ); ?>" name="<?php echo $this->get_field_name('category_three'); ?>" class="widefat" style="width:100%;">

                    <?php 
                    $categories = moral_magazine_get_post_cat_choices();
                    foreach($categories as $category => $value) { ?>
                    <option value="<?php echo absint( $category ); ?>" <?php selected( absint( $instance['category_three'] ), $category ); ?>><?php echo esc_html( $value ); ?></option>
                    <?php } ?>      
                </select>
            </p>
        <?php }

        /**
        * Processing widget options on save
        *
        * @param array $new_instance The new options
        * @param array $old_instance The previous options
        */
       function update( $new_instance, $old_instance ) {
            $instance = array();
            $instance['category_one'] = ( !empty( $new_instance['category_one'] ) ) ? absint( $new_instance['category_one'] ) : '';
            $instance['category_two'] = ( !empty( $new_instance['category_two'] ) ) ? absint( $new_instance['category_two'] ) : '';
            $instance['category_three'] = ( !empty( $new_instance['category_three'] ) ) ? absint( $new_instance['category_three'] ) : '';
            return $instance;
    }
    }
    endif;
