<?php if(! defined('ABSPATH')){ return; }
/*
 Name: Blog Archive
 Description: Create and display the current post content
 Class: TH_BlogArchive
 Category: content
 Level: 3
*/

/**
 * Class TH_BlogArchive
 *
 * Create and display the current page content
 *
 * @package  Kallyas
 * @category Page Builder
 * @author   Team Hogash
 * @since    4.0.0
 */
class TH_BlogArchive extends ZnElements
{
	public static function getName(){
		return __( "Blog archive", 'zn_framework' );
	}

	/**
	 * Load dependant resources
	 */
	function scripts(){
		/*
		 * Load resources in footer
		 */
		wp_enqueue_script('isotope', THEME_BASE_URI.'/js/jquery.isotope.min.js',array ( 'jquery' ), THEME_VERSION, true);

	}



	/**
	 * This method is used to display the output of the element.
	 * @return void
	 */
	function element()
	{

		global $zn_config, $query_string, $wp_query, $paged;

		$zn_config['blog_columns'] = $this->opt( 'blog_columns', '1' );
		$category = $this->opt('category') ? $this->opt('category') : '';
		$count = $this->opt('count')  ? $this->opt('count') : '4';

		$args = array(
			'posts_per_page' => ( int )$count,
			'post_status' => 'publish',
			'category__in' => $category,
			'paged' => $paged
		);

		// PERFORM THE QUERY
		query_posts( $args );

		echo '<div class="zn_blog_archive_element '.$this->data['uid'].'">';
			if ( $zn_config['blog_columns'] > 1 ) {
				get_template_part( 'blog', 'columns' );
			}
			else {
				get_template_part( 'blog', 'default' );
			}
		echo '</div>';
		// Reset the global $the_post as this query will have stomped on it
		wp_reset_query();
	}

	// TODO : Uncomment this if JS errors appears because of clients shortcodes/plugins
	// /**
	//  * This method is used to display the output of the element.
	//  * @return void
	//  */
	// function element_edit()
	// {
	//     echo '<div class="zn-pb-notification">This element will be rendered only in View Page Mode and not in PageBuilder Edit Mode.</div>';
	// }

	function options() {

		$args = array(
			'type' => 'post'
		);	
				
		$post_categories = get_categories($args);

		$option_post_cat = array();

		foreach ($post_categories as $category) {
			$option_post_cat[$category->cat_ID] = $category->cat_name;
		}

		return array(
			array(
				'id'          => 'blog_columns',
				'name'        => 'Blog columns',
				'description' => 'Select the number of columns to use',
				'type'        => 'select',
				'std'		  => '1',
				'options'        => array(
					'1' => '1 column',
					'2' => '2 column',
					'3' => '3 column',
					'4' => '4 column',
				),
			),
			array(
				'id'          => 'category',
				'name'        => 'Categories',
				'description' => 'Select your desired categories for post items to be displayed.',
				'type'        => 'select',
				'options'	  => $option_post_cat,
				'multiple'	  => true
				),
			array(
				'id'          => 'count',
				'name'        => 'Number of items per page',
				'description' => 'Please choose the desired number of items that will be shown on a page',
				'type'        => 'slider',
				'std'		  => '4',
				'class'		  => 'zn_full',
				'helpers'	  => array(
					'min' => '1',
					'max' => '50',
					'step' => '1'
				),
			),
		);
	}

}
