<?php
/**
 * Widget: Block Partner
 *
 * Widget show the block posts from selected category in different layouts.
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */
if(!class_exists( 'Trainify_Course_Listing_Widget' ) ):

	class Trainify_Course_Listing_Widget extends TrainifyFramework_Master_Widget{

		/**
		 * Register widget with WordPress.
		 */
		public function __construct() {
			$widget_ops = array(
				'classname'   => 'trainify_course_listing_widget tff-full-widget',
				'description' => esc_html__( 'Display courses with beautiful layout.', 'trainify' )
			);
			parent::__construct( 'trainify_course_listing_widget', esc_html__( 'TF - Course Listing', 'trainify' ), $widget_ops );
		}

		/**
		 * Helper function that holds widget fields
		 * Array is used in update and form functions
		 */
		public function widget_fields( $instance = array() ) {

			$fields = array(
				'trainify_framework_widget_tab'       => array(
					'trainify_framework_widget_field_name'     => 'trainify_framework_widget_tab',
					'trainify_framework_widget_field_title'    => esc_html__( 'General', 'trainify' ),
					'trainify_framework_widget_field_default'  => 'general',
					'trainify_framework_widget_field_type'     => 'tabgroup',
					'trainify_framework_widget_field_options'  => array(
						'general'=>array(
							'trainify_framework_widget_field_title'=>esc_html__('General', 'trainify'),
							'trainify_framework_widget_field_options'=> array(
								'title' => array(
									'trainify_framework_widget_field_name'        => 'title',
									'trainify_framework_widget_field_title'       => esc_html__( 'Section Title', 'trainify' ),
									'trainify_framework_widget_field_default'  => esc_html__('Our Courses', 'trainify'),
									'trainify_framework_widget_field_description' => esc_html__( 'Enter your block title', 'trainify' ),
									'trainify_framework_widget_field_type'  => 'text'
								),
								'sub_title'         => array(
									'trainify_framework_widget_field_name'     => 'sub_title',
									'trainify_framework_widget_field_title'    => esc_html__( 'Sub Title', 'trainify' ),
									'trainify_framework_widget_field_type'     => 'text',
									'trainify_framework_widget_description'    => esc_html__('Please add shortcode of contact form here.', 'trainify'),
								),
								'background_color'         => array(
									'trainify_framework_widget_field_name'     => 'background_color',
									'trainify_framework_widget_field_title'    => esc_html__( 'Background color', 'trainify' ),
									'trainify_framework_widget_field_type'     => 'color',
									'trainify_framework_widget_description'    => esc_html__('Please add a background color for this section.', 'trainify'),
								),
								'no_of_courses'         => array(
									'trainify_framework_widget_field_name'     => 'no_of_courses',
									'trainify_framework_widget_field_title'    => esc_html__( 'No of courses', 'trainify' ),
									'trainify_framework_widget_field_default'  => 3,
									'trainify_framework_widget_field_type'     => 'number',
									'trainify_framework_widget_description'    => esc_html__('Please choose no of courses.', 'trainify'),
								),
							),
						),
					)
				)
			);

			$widget_fields_key = 'fields_'.$this->id_base;
			$widgets_fields = apply_filters( $widget_fields_key, $fields );
			return $widgets_fields;

		}

		/**
		 * Front-end display of widget.
		 *
		 * @see WP_Widget::widget()
		 *
		 * @param array $args Widget arguments.
		 * @param array $instance Saved values from database.
		 */
		public function widget( $args, $instance ) {

			extract( $args );
			if ( empty( $instance ) ) {
				return;
			}
			$title  = isset( $instance['title'] ) ? $instance['title'] : '';
			$title 	= apply_filters( 'widget_title', $title, $instance, $this->id_base );
			$sub_title  = isset( $instance['sub_title'] ) ? $instance['sub_title'] : '';
			$no_of_courses  = isset( $instance['no_of_courses'] ) ? absint( $instance['no_of_courses'] ) : 3;
			$background_color = isset($instance['background_color']) ? $instance['background_color'] : '';
			trainify_framework_before_widget($args);
			$course_args = array(
				'post_type' => 'lp_course',
				'post_status' => 'publish',
				'posts_per_page' => absint($no_of_courses)
			);
			$course_result = new WP_Query($course_args);
			?>
			<div class="course-section" style="background-color:<?php echo esc_attr($background_color); ?>;">
				<div class="tff-container">
					<div class="section-wrap">
						<?php if($title || $sub_title): ?>
							<div class="section-heading">
								<h4 class="section-sub_title"><?php echo esc_html($sub_title); ?></h4>
								<h2 class="section-title"><?php echo esc_html($title); ?></h2>
							</div>
							<?php
						endif;
						if( $course_result->have_posts() ) :

							do_action( 'learn-press/before-courses-loop' );

							learn_press_begin_courses_loop();

							while ( $course_result->have_posts() ) : $course_result->the_post();

								learn_press_get_template_part( 'content', 'course' );

							endwhile;

							learn_press_end_courses_loop();

							do_action( 'learn_press_after_courses_loop' );

							wp_reset_postdata();

						else:
							learn_press_display_message( esc_html__( 'No course found.', 'trainify' ), 'error' );
						endif;
						?>
					</div>
				</div>
			</div>
			<?php
			trainify_framework_after_widget($args);

	    }

	}

endif;
