<?php
/**
 * Widget: Course Search
 *
 * Widget to show course 
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */
if(!class_exists( 'Trainify_Course_Search_Widget' ) ):

	class Trainify_Course_Search_Widget extends TrainifyFramework_Master_Widget{

		/**
		 * Register widget with WordPress.
		 */
		public function __construct() {
			$widget_ops = array(
				'classname'   => 'trainify_course_search_widget tff-full-widget',
				'description' => esc_html__( 'Add video block from widget on frontend widgetarea.', 'trainify' )
			);
			parent::__construct( 'trainify_course_search_widget', esc_html__( 'TF - Course Search', '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__('Search 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_description' => esc_html__( 'Enter your sub title', 'trainify' ),
									'trainify_framework_widget_field_type'  => 'text'
								),
								'background_image'         => array(
									'trainify_framework_widget_field_name'     => 'background_image',
									'trainify_framework_widget_field_title'    => esc_html__( 'Background Image', 'trainify' ),
									'trainify_framework_widget_field_type'     => 'upload',
									'trainify_framework_widget_description'    => esc_html__('Please add a background image for this section.', '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'] : '';
			$background_image = isset($instance['background_image']) ? $instance['background_image'] : '';
			trainify_framework_before_widget($args);

			$title_tag_name = 'h4';
			$main_widget_areas = array(
				'trainify_home_content_area',
				'trainify_home_middle_section_area',
				'trainify_home_bottom_section_area',
			);
			$widget_area_id = ($args['id']) ? $args['id'] : '';
			if(in_array($widget_area_id, $main_widget_areas)){
				$title_tag_name = 'h2';
			}
			?>
			<div class="course-search" style="background-image:url('<?php echo esc_url($background_image); ?>'); background-size: cover;">
				<div class="container">
					<div class="section-wrap">
						<div class="section-heading">
							<<?php echo esc_attr($title_tag_name); ?> class="section-title"><span><?php echo esc_html($title); ?></span></<?php echo esc_attr($title_tag_name); ?>>
							<h4 class="section-sub_title"><?php echo esc_html($sub_title); ?></h4>
						</div>
						<div class="section-content-wrap">
							<form action="<?php echo esc_url(home_url()); ?>">
								<input type="hidden" name="post_type" value="lp_course"/>
								<span>
									<?php
									$course_args = array(
										'id'		=> '',
										'name'		=> 'course_category',
										'hide_empty' => false,
										'taxonomy' => 'course_category',
										'show_option_all' => esc_html__( 'All Courses', 'trainify' )
									);
									wp_dropdown_categories($course_args);
									?>
								</span>
								<span>
									<input type="text" class="course_keyword" placeholder="<?php esc_attr_e( 'Courses Keyword', 'trainify'); ?>" name="s">
								</span>
								<span><input type="submit" value="<?php esc_attr_e( 'Search Courses', 'trainify' ); ?>"></span>
							</form>
						</div>
					</div>
				</div>
			</div>
			<?php
			trainify_framework_after_widget($args);
		}

	}

endif;