<?php
/**
 * Postmag widget Coursel
 * @package Postmagthemes
 * @subpackage Postmag
 */
if ( ! class_exists( 'Postmag_Coursel_Widget' ) ) :
	class Postmag_Coursel_Widget extends WP_Widget{  
		function __construct() {
			parent::__construct(
				'coursel_widget', // Base ID
				__( 'Built- Carousel', 'postmag' ), // Name
				array( 'description' => __( 'Displays Carousel', 'postmag' ),
				)
			);
		}
		private function defaults() {
			$defaults = array( 
				'NoofListings' 		=> 2,
				'categoryListings'  => '',
				'categryorformat'   => 3,
				'postformat'	    => '',
			);
			return $defaults;
		}
		public function form( $instance) {
			$instance = wp_parse_args( (array) $instance, $this->defaults() );
			$NoofListings = $instance[ 'NoofListings' ];
			$categoryListings = $instance[ 'categoryListings' ];
			$categryorformat = $instance[ 'categryorformat' ];
			$postformat = $instance[ 'postformat' ];
			// validating the user input for number of post to show
			if ( $NoofListings == 0 || strlen( $NoofListings ) > 2 ) {
				$NoofListings = 1;
			}
			?>
			<!-- CODE FOR CHOSING FORMAT -->
			<label><?php esc_attr_e( 'A. Choose Type for display:', 'postmag' ); ?></label><br>
			<input id = "<?php echo absint( $this -> get_field_id( 'categryorformat' ) ); ?>" name = "<?php echo esc_attr( $this -> get_field_name( 'categryorformat' ) ); ?>" type = "radio" value = "1"
			<?php checked( 1, absint( $categryorformat )); ?> size = "1" />
			<label><?php esc_attr_e( 'Choose Format type display: (Image or Video if created)', 'postmag' ); ?></label>
			<?php
                $postmag_postformat = array(
					'show_option_none'   => __('Select post by format','postmag'),
                    'orderby'            => 'name',
                    'order'              => 'asc',
                    'show_count'         => 1,
                    'hide_empty'         => 1,
                    'echo'               => 1,
                    'selected'           => $postformat,
                    'hierarchical'       => 1,
                    'name'               => esc_html( $this->get_field_name('postformat') ),
                    'id'                 => absint( $this->get_field_id('postformat') ),
                    'class'              => 'widefat',
                    'taxonomy'           => 'post_format',
					'value_field'	     => 'name',
                );
                wp_dropdown_categories($postmag_postformat);
            ?>
			<!-- CODE FOR CHOSING RECENT POST -->
			<input id = "<?php echo absint( $this -> get_field_id( 'categryorformat' ));?>" name = "<?php echo esc_attr( $this -> get_field_name( 'categryorformat' ));?>" type = "radio"  value = "3"
			<?php checked( 3, absint( $categryorformat ) ); ?> size = "1" />
			<label><?php esc_attr_e( 'All formats type display:', 'postmag' ); ?></label>
			<br />
			<!-- CODE FOR CHOSING CATEGORY -->
			<input id = "<?php echo absint( $this -> get_field_id( 'categryorformat' ) ); ?>" name = "<?php echo esc_attr( $this -> get_field_name( 'categryorformat' ) ); ?>" type = "radio"  value = "2" 
			<?php checked( 2, absint( $categryorformat ) ); ?> size = "1" />
			<label><?php esc_attr_e( 'Choose Category type display:', 'postmag' ); ?></label>
		    <?php
                $postmag_categoryListings = array(
					'show_option_none'   => __( 'Select post by category','postmag' ),
					'option_none_value'  => __( 'none', 'postmag' ),
                    'orderby'            => 'name',
                    'order'              => 'asc',
                    'show_count'         => 1,
                    'hide_empty'         => 1,
                    'echo'               => 1,
                    'selected'           => $categoryListings,
                    'hierarchical'       => 1,
                    'name'               => esc_html( $this->get_field_name('categoryListings') ),
                    'id'                 => absint( $this->get_field_id('categoryListings') ),
                    'class'              => 'widefat',
                    'taxonomy'           => 'category',
					'value_field'	     => 'name',
                );
                wp_dropdown_categories($postmag_categoryListings);
            ?>
			<!-- CODE FOR NUMBER OF POST TO SHOW -->
			<label><?php esc_attr_e( 'B. Number of posts to display: (1 - 99)', 'postmag' ); ?></label>
			<input id = "<?php echo absint( $this -> get_field_id( 'NoofListings' ) );?>" name = "<?php echo esc_attr( $this -> get_field_name( 'NoofListings' ) );?>" type = "number" value = "<?php echo absint( $NoofListings );?>" size = "2" min="1" max="99" class = "widefat" />
			<?php	
		}
		public function update( $new_instance, $old_instance) {
			$instance = $old_instance;
			$instance[ 'NoofListings' ] = absint( ( $new_instance[ 'NoofListings' ] ) );
			$instance['categoryListings'] = ( isset( $new_instance['categoryListings'] ) ) ? sanitize_text_field( $new_instance['categoryListings'] ) : '';
			$instance[ 'categryorformat' ] = absint( ( $new_instance[ 'categryorformat' ] ) );
			$instance[ 'postformat' ] = ( isset( $new_instance['postformat'] ) ) ? sanitize_text_field( $new_instance[ 'postformat' ]) : '';
			return $instance;
		}
		public function widget( $args, $instance) {
			$instance = wp_parse_args( (array) $instance, $this->defaults() );
			$NoofListings = $instance[ 'NoofListings' ];
			$categoryListings = $instance[ 'categoryListings' ];
			$categryorformat = $instance[ 'categryorformat' ];
			$postformat = $instance[ 'postformat' ];
			echo $args['before_widget'];
			$this->getRealtyListings( $NoofListings, $categoryListings,$categryorformat,$postformat);
			echo $args['after_widget'];
		}
		private function getRealtyListings( $NoofListings, $categoryListings,$categryorformat,$postformat) {
			?>
			<div id = "demo" class = "carousel slide marginb15" data-ride = "carousel" data-interval= "50000" >
				<!-- The slideshow -->
				<div class = "carousel-inner" >  
					<?php
					// this is just initilize $args to fix issue in customizer view
					$args = array(
						'post_type' 	 => 'post',
						'posts_per_page' => absint( $NoofListings ),
					);
					if ( absint( $categryorformat ) == 1 ) {
						$args = array(
							'post_format' => 'post-format-'.esc_html( $postformat ),
							'posts_per_page' => absint( $NoofListings ),
						);
					}
					if ( absint( $categryorformat ) == 2 ) {
						$args = array(
							'category_name' => esc_html( $categoryListings ),
							'posts_per_page' => absint( $NoofListings ),
						);	
					}
					if ( absint( $categryorformat ) == 3 ) {
						$args = array(
							'post_type' => 'post',
							'posts_per_page' => absint( $NoofListings ),
						);
					}
					$loop = new WP_Query( $args );
					$i = 0;
					if ( $loop->have_posts() ) {
						while ( $loop-> have_posts() ) {
							$i++;
							$loop->the_post();
					?>
							<div class = "carousel-item myrow4 <?php if ( $i == 1 ) {echo 'active';} ?>" >
								<div class = "catagoryline">
									<?php the_category( ', ' ); ?>
								</div>
								<?php get_template_part( 'nocontent-coursel' ); ?>
							</div>
						<?php
						}
					}
						?>   
				</div>
				<!-- Left and right controls -->
				<a class = "carousel-control-prev" href = "<?php echo esc_url( '#demo' ); ?>" data-slide = "prev" >
					<span class = "carousel-control-prev-icon" ></span>
				</a>
				<a class = "carousel-control-next" href = "<?php echo esc_url( '#demo' ); ?>" data-slide = "next" >
					<span class = "carousel-control-next-icon" ></span>
				</a>
			</div>
			<?php
		}
	} //end class Coursel_Widget
endif;