<?php
/**
 * Kopa Widget Tab
 *
 * @author 		Kopatheme
 * @category 	Widgets
 * @package 	KopaFramework/Widgets
 * @since    	1.0.0
 * @extends 	Kopa_Widget
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class Kopa_Widget_Tab extends Kopa_Widget {
	/**
	 * Constructor
	 */
	public function __construct() {

        //Tags Array
        $tags = get_tags();
        $arr_tags = array(
            '' => __('----SELECT----', 'kopa-framework')
            );
        foreach ($tags as $category) 
            $arr_tags[$category->term_id] = $category->name.' ('.$category->count.')';

        //Categories Array
        $categories = get_categories();
        $arr_cats = array(
            '' => __('----SELECT----', 'kopa-framework')
            );
        foreach ($categories as $category)
            $arr_cats[$category->term_id] = $category->name.' ('.$category->count.')';

		$this->widget_cssclass    = 'kopa-tab-1-widget';
		$this->widget_description = __( 'Display posts with tabs.', 'newmoment' );
		$this->widget_id          = 'kopa-tab-1-widget';
		$this->widget_name        = __( 'Kopa Widget Tab', 'newmoment' );
		$this->settings           = array(
			'title'  => array(
				'type'  => 'text',
				'std'   => __( 'Kopa Widget Tab', 'newmoment' ),
				'label' => __( 'Title:', 'newmoment' )
			),
            'categories' => array(
                'type'    => 'multiselect',
                'std'     => '',
                'label'   => __( 'Categories:', 'newmoment' ),
                'options' => $arr_cats,
            ),
            'relation' => array(
                'type'    => 'select',
                'std'     => '',
                'label'   => __( 'Relation:', 'newmoment' ),
                'options' => array(
                    'OR'  => __( 'OR', 'newmoment' ),
                    'AND' => __( 'AND', 'newmoment' ),
                )
            ),
            'tags' => array(
                'type'    => 'multiselect',
                'std'     => '',
                'label'   => __( 'Tags:', 'newmoment' ),
                'options' => $arr_tags,
            ),
            'orderby' => array(
                'type'    => 'select',
                'std'     => '',
                'label'   => __( 'Orderby:', 'newmoment' ),
                'options' => array(
                    'latest'       => __('Latest', 'newmoment'),
                    'popular'      => __('Popular by view count', 'newmoment'),
                    'most_comment' => __('Popular by comment count', 'newmoment'),
                    'random'       => __('Random', 'newmoment')
                )
            ),
            'timestamp' => array(
                'type'  => 'select',
                'std'   => '',
                'label' => __( 'Timestamp (ago):', 'newmoment' ),
                'options' => array(
                    ''          => __('-- Select --', 'newmoment'),
                    '-1 week'   => __('1 week', 'newmoment'),
                    '-2 week'   => __('2 weeks', 'newmoment'),
                    '-3 week'   => __('3 weeks', 'newmoment'),
                    '-1 month'  => __('1 months', 'newmoment'),
                    '-2 month'  => __('2 months', 'newmoment'),
                    '-3 month'  => __('3 months', 'newmoment'),
                    '-4 month'  => __('4 months', 'newmoment'),
                    '-5 month'  => __('5 months', 'newmoment'),
                    '-6 month'  => __('6 months', 'newmoment'),
                    '-7 month'  => __('7 months', 'newmoment'),
                    '-8 month'  => __('8 months', 'newmoment'),
                    '-9 month'  => __('9 months', 'newmoment'),
                    '-10 month' => __('10 months', 'newmoment'),
                    '-11 month' => __('11 months', 'newmoment'),
                    '-1 year'   => __('1 year', 'newmoment'),
                    '-2 year'   => __('2 years', 'newmoment'),
                    '-3 year'   => __('3 years', 'newmoment'),
                    '-4 year'   => __('4 years', 'newmoment'),
                    '-5 year'   => __('5 years', 'newmoment'),
                    '-6 year'   => __('6 years', 'newmoment'),
                    '-7 year'   => __('7 years', 'newmoment'),
                    '-8 year'   => __('8 years', 'newmoment'),
                    '-9 year'   => __('9 years', 'newmoment'),
                    '-10 year'  => __('10 years', 'newmoment'),
                )
            ),
			'posts_per_page' => array(
				'type'  => 'number',
				'step'  => 1,
				'min'   => 1,
				'max'   => '',
				'std'   => 5,
				'label' => __( 'Number of items:', 'newmoment' )
			), 	
            'type' => array(
                'type'    => 'select',
                'std'     => '',
                'label'   => __( 'Type:', 'newmoment' ),
                'options' => array(
                    '1' => __('Type 1', 'newmoment'),
                    '2' => __('Type 2', 'newmoment'),
                    '3' => __('Type 3', 'newmoment'),
                    '4' => __('Type 4', 'newmoment')
                )
            ),
		);
		parent::__construct();
	}

	/**
	 * widget function.
	 *
	 * @see WP_Widget
	 * @access public
	 * @param array $args
	 * @param array $instance
	 * @return void
	 */
	public function widget( $args, $instance ) {

		if ( $this->get_cached_widget( $args ) )
			return;

		ob_start();
		extract( $args );

		$title       = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );

        $aCategories = $instance['categories'];
        $type = $instance['type'];
        $status = get_theme_mod( 'kopa_status_default_thumbnail', true );
        if($status == 'enable') {
            $status = true;
        } else {
            $status = false;
        }
        ?>

        <?php if (!empty($aCategories)) : ?> 
        <div class="widget kopa-tab-1-widget">          
            <ul class="nav nav-tabs kopa-tabs-<?php echo $type; ?>">
                <?php $index = 0; ?>
                <?php foreach($aCategories as $v): $index++; ?>
                    <li <?php if($index == 1) echo 'class="active"';?>><a data-toggle="tab" href="#tab<?php echo $this->number;?>-<?php echo $index;?>"><?php echo get_cat_name($v);?><span></span></a></li>
                <?php endforeach; ?>
            </ul>
            <!-- nav-tabs -->

            <div class="tab-content">
                <?php $index1 = 0; foreach($aCategories as $v): $index1++; ?>
                    <div id="tab<?php echo $this->number;?>-<?php echo $index1;?>" class="tab-pane<?php if($index1 == 1) echo ' active';?>">
                        <ul class="clearfix">
                            <?php 
                            $args['categories'] = $v;
                            $args['relation'] = esc_attr($instance['relation']);
                            $args['tags'] = $instance['tags'];
                            $args['posts_per_page'] = (int) $instance['posts_per_page'];

                            if (isset($instance['timestamp'])){
                                $args['date_query'] = $instance['timestamp'];
                            }

                            $args['orderby'] = $instance['orderby'];
                            $posts = kopa_widget_posttype_build_query($args);
                            while($posts->have_posts()) : $posts->the_post();
                            ?>
                            <li>
                                <article class="entry-item clearfix">
                                    <?php if(has_post_thumbnail() || $status == true) : ?>
                                        <div class="entry-thumb">
                                            <a href="<?php the_permalink(); ?>">
                                                <?php the_post_thumbnail('kopa-size-10'); ?>
                                            </a>
                                        </div> 
                                    <?php endif; ?>
                                    <div class="entry-content">
                                        <h6 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
                                        <footer>
                                            <span class="entry-time"><i class="fa fa-clock-o"></i><span><?php echo get_post_time('g:i A') ;?></span></span>
                                            <span class="entry-meta">|</span>
                                            <span class="entry-date"><i class="fa fa-calendar"></i><span><?php echo get_the_date(); ?></span></span>
                                        </footer>
                                    </div>
                                </article>
                            </li>
                            <?php endwhile; ?>
                        </ul>
                    </div>
                <?php endforeach; ?>
            </div>
            <!-- tab-content -->
        </div>
        <?php endif; ?>
		<?php
		wp_reset_postdata();

        $content = ob_get_clean();

        echo $content;

        $this->cache_widget( $args, $content );
	}
}

register_widget( 'Kopa_Widget_Tab' );