<?php
/**
 * EDM: Page Block
 *
 * Widget show the banner ads of different size
 *
 * @package MatrixAddons
 * @subpackage trainify
 * @since 1.0.0
 */

if(!class_exists( 'Trainify_Page_Block_Widget' ) ):

	class Trainify_Page_Block_Widget extends TrainifyFramework_Master_Widget{

		/**
		 * Register widget with WordPress.
		 */
		public function __construct() {
			$widget_ops = array(
				'classname'   => 'trainify_page_block_widget tff-full-widget',
				'description' => esc_html__( 'Call to action widget', 'trainify' )
			);
			parent::__construct( 'trainify_page_block_widget', esc_html__( 'TF - Page Block', '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(
								'block_page_id' => array(
									'trainify_framework_widget_field_name'       => 'block_page_id',
									'trainify_framework_widget_field_title'      => esc_html__( 'Choose Page', 'trainify' ),
									'trainify_framework_widget_field_default'  => '',
									'trainify_framework_widget_field_type' => 'pagelist',
								),
								'thumbnail_size' => array(
									'trainify_framework_widget_field_name'       => 'thumbnail_size',
									'trainify_framework_widget_field_title'      => esc_html__( 'Page thumbnail size', 'trainify' ),
									'trainify_framework_widget_field_default'  => 'full',
									'trainify_framework_widget_field_type' => 'select',
									'trainify_framework_widget_field_options'   => trainify_framework_get_image_sizes(),
								),
								'readmore_text' => array(
									'trainify_framework_widget_field_name'       => 'readmore_text',
									'trainify_framework_widget_field_title'      => esc_html__( 'Readmore Button Text', 'trainify' ),
									'trainify_framework_widget_field_default'  => esc_html__( 'Read More', 'trainify' ),
									'trainify_framework_widget_field_type' => 'text',
								),
								'primary_button_text' => array(
									'trainify_framework_widget_field_name'       => 'primary_button_text',
									'trainify_framework_widget_field_title'      => esc_html__( 'Primary Button Text', 'trainify' ),
									'trainify_framework_widget_field_default'  => '',
									'trainify_framework_widget_field_type' => 'text',
								),
								'primary_button_url' => array(
									'trainify_framework_widget_field_name'       => 'primary_button_url',
									'trainify_framework_widget_field_title'      => esc_html__( 'Primary Button url', 'trainify' ),
									'trainify_framework_widget_field_default'  => '#',
									'trainify_framework_widget_field_type' => 'url',
								),
								'background_color' => array(
									'trainify_framework_widget_field_name'       => 'background_color',
									'trainify_framework_widget_field_title'      => esc_html__( 'Background color', 'trainify' ),
									'trainify_framework_widget_field_default'  => '#fff',
									'trainify_framework_widget_field_type' => 'color',
								),
								'background_image' => array(
									'trainify_framework_widget_field_name'       => 'background_image',
									'trainify_framework_widget_field_title'      => esc_html__( 'Background Image', 'trainify' ),
									'trainify_framework_widget_field_default'  => '',
									'trainify_framework_widget_field_type' => 'upload',
								),
							)
						)
					)
				)

			);

			$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;
			}
           
            $block_page_id  = isset( $instance['block_page_id'] ) ? $instance['block_page_id'] : '';
            $thumbnail_size  = isset( $instance['thumbnail_size'] ) ? $instance['thumbnail_size'] : 'full';
            $readmore_text  = isset( $instance['readmore_text'] ) ? $instance['readmore_text'] : '';
            $primary_button_text  = isset( $instance['primary_button_text'] ) ? $instance['primary_button_text'] : '';
            $primary_button_url  = isset( $instance['primary_button_url'] ) ? $instance['primary_button_url'] : '';
            $background_color  = isset( $instance['background_color'] ) ? sanitize_hex_color( $instance['background_color'] ) : '';        
            $background_image  = isset( $instance['background_image'] ) ? esc_url( $instance['background_image'] ) : '';

			$page_args = array(
				'post_type' 	=> 'page',
				'post_status' 	=> 'publish',
				'posts_per_page' => 1,
			);
			if($block_page_id){
				$page_args['p'] = absint($block_page_id);
			}
			$page_result = new WP_Query($page_args);
			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="about-us-wrapper" style="background-color: <?php echo esc_attr($background_color); ?>; background-image:url(<?php echo esc_url($background_image); ?>);">
				<div class="tff-container">
					<div class="row about-content-media-wrap">
						<?php 
						if($page_result->have_posts()){
							while($page_result->have_posts()):
								$page_result->the_post();
								?>
								<div class="about-content-part <?php echo (has_post_thumbnail( get_the_ID() )) ? 'has-thumbnail' : 'no-thumbnail';  ?>">
									<div class="about-content-inner">
										<div class="about-block-title">
											<<?php echo esc_attr($title_tag_name); ?>><?php the_title(); ?></<?php echo esc_attr($title_tag_name); ?>>
										</div>
										<?php trainify_framework_the_excerpt(350, false ); ?>
										<div class="about-btns-wrap buttons-wrap">
											<?php if($readmore_text){ ?>
												<a class="custom-button button-secondary" href="<?php the_permalink(); ?>"><?php echo esc_html($readmore_text); ?></a>
											<?php } ?>
											<?php if($primary_button_text){ ?>
												<a class="custom-button button-primary" href="<?php echo esc_url($primary_button_url); ?>"><?php echo esc_html($primary_button_text); ?></a>
											<?php } ?>
										</div>
									</div> 
								</div>
								<?php if(has_post_thumbnail( get_the_ID() )): ?>
									<div class="about-media-part" style="">
										<figure class="page-thumbnail-wrap">
											<?php the_post_thumbnail( $thumbnail_size ); ?>
										</figure>
									</div>
									<?php
								endif;
							endwhile;
							wp_reset_postdata();
						}
						?>
					</div>
				</div>
			</div>
			<?php
			trainify_framework_after_widget($args);

		}

	}

endif;