<?php if(! defined('ABSPATH')){ return; }
/*
 Name: Recent Work
 Description: Create and display a Recent Work element
 Class: TH_RecentWork
 Category: content
 Level: 3
 Scripts: true
*/

/**
 * Class TH_RecentWork
 *
 * Create and display a Recent Work element
 *
 * @package  Kallyas
 * @category Page Builder
 * @author   Team Hogash
 * @since    4.0.0
 */
class TH_RecentWork extends ZnElements
{
	public static function getName(){
		return __( "Recent Work", 'zn_framework' );
	}

	/**
	 * Load dependant resources
	 */
	function scripts(){
		wp_enqueue_script( 'caroufredsel', THEME_BASE_URI . '/sliders/caroufredsel/jquery.carouFredSel-packed.js', array ( 'jquery' ), THEME_VERSION, true );
	}

	/**
	 * This method is used to display the output of the element.
	 *
	 * @return void
	 */
	function element()
	{
		$options = $this->data['options'];
		$rwStyle = $this->opt('rw_style', '1');
		$rwStyleMod = $rwStyle;

		$col_left = 3;
		$col_right = 9;

		// For style 3, do some re-spacing
		if($rwStyle == 3){
			$col_left = 4;
			$col_right = 8;
			$rwStyleMod = '2 rwc--3';
		}


        $rwheight = (int)$this->opt('rw_height',165);

        ?>

<div class="recentwork_carousel recentwork_carousel--<?php echo (int)$rwStyleMod; ?> clearfix">

    <div class="row">

        <div class="col-sm-<?php echo $col_left; ?>">
        	<div class="recentwork_carousel__left">
	            <?php
	                // ELEMENT TITLE
	                if ( ! empty ( $options['rw_title'] ) ) {
	                    echo '<h3 class="recentwork_carousel__title m_title">' . $options['rw_title'] . '</h3>';
	                }
	                // ELEMENT DESCRIPTION
	                if ( ! empty ( $options['rw_desc'] ) ) {
	                    echo '<p class="recentwork_carousel__desc">' . $options['rw_desc'] . '</p>';
	                }
	                // PORTFOLIO PAGE LINK
	                if ( ! empty ( $options['rw_port_link'] ) && ($rwStyle == 2 || $rwStyle == 3) ) {
	                    echo '<a href="' . $options['rw_port_link'] . '" class="btn btn-fullcolor">'. $this->opt('rw_port_link_text', 'VIEW ALL') .'</a>';
	                }
	            ?>
	            <div class="controls recentwork_carousel__controls">
	                <a href="#" class="prev recentwork_carousel__prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
	                <?php
	                    // PORTFOLIO PAGE LINK
	                    if ( ! empty ( $options['rw_port_link'] ) && ($rwStyle != 2 && $rwStyle != 3) ) {
	                        echo '<a href="' . $options['rw_port_link'] . '" class="complete"><span class="glyphicon glyphicon-th"></span></a>';
	                    }
	                ?>
	                <a href="#" class="next recentwork_carousel__next"><span class="glyphicon glyphicon-chevron-right"></span></a>
	            </div><!-- /.recentwork_carousel__controls -->
	            </div><!-- /.recentwork_carousel__left -->
        </div>

        <div class="col-sm-<?php echo $col_right; ?>">
			<div class="recentwork_carousel__crsl-wrapper">
	            <ul class="recent_works1 fixclear recentwork_carousel__crsl">
	                <?php
	                    global $post;

	                    $posts_per_page = $this->opt('ports_per_page', '4'); // how many posts

	                    if ( empty ( $options['portfolio_categories'] ) ) {
	                        $options['portfolio_categories'] = '';
	                    }

	                    // Start the query
	                    $queryArgs = array (
	                        'post_type'      => 'portfolio',
	                        'posts_per_page' => $posts_per_page,
                            'post_status'    => 'publish',
	                    );

	                    if( !empty( $options['portfolio_categories'] ) ){
	                    	$queryArgs['tax_query'] = array (
	                            array (
	                                'taxonomy' => 'project_category',
	                                'field'    => 'id',
	                                'terms'    => $options['portfolio_categories']
	                            )
	                        );
	                    }

	                    $theQuery = new WP_Query($queryArgs);

                        // Start the loop
                        if( $theQuery->have_posts() ) {

                            while($theQuery->have_posts()){
                                $theQuery->the_post();

		                        echo '<li>';

		                        echo '<a href="' . get_permalink() . '" class="recentwork_carousel__link">';

		                        $port_media = get_post_meta( $post->ID, 'zn_port_media', true );
		                        if ( ! empty ( $port_media ) && is_array( $port_media ) ) {

		                            $size      = zn_get_size( 'four' );
		                            $has_image = false;

		                            if ( $portfolio_image = $port_media[0]['port_media_image_comb'] ) {
		                                if ( is_array( $portfolio_image ) ) {

		                                    if ( $saved_image = $portfolio_image['image'] ) {
		                                        $has_image = true;
		                                    }
		                                }
		                                else {
		                                    $saved_image = $portfolio_image;
		                                    $has_image   = true;
		                                }

		                                if ( $has_image ) {
		                                    $image = vt_resize( '', $saved_image, $size['width'], '', true );
		                                }
		                            }

		                            // Check to see if we have video
		                            if ( $portfolio_media = $port_media[0]['port_media_video_comb'] ) {
		                            }

		                            echo '<div class="hover recentwork_carousel__hover">';
		                            // IMAGE
		                            if ( ! empty( $saved_image ) ) {
	                                	echo '<div style="height: '.$rwheight.'px; background-image:url(' . $image['url'] . ');" class="recentwork_carousel__img"></div>';
		                            }
		                            elseif ( $portfolio_media ) {
		                                echo get_video_from_link( $portfolio_media, '', "100%", $rwheight );
		                            }
		                            	echo '<span class="hov recentwork_carousel__hov"></span>';
	                                echo '</div>';
		                        }

		                        $cat = '<span class="recentwork_carousel__cat">' . strip_tags( get_the_term_list( $post->ID, 'project_category', '', ' , ', '' ) ) . '</span>';

		                        echo '<div class="details recentwork_carousel__details">';
		                        	echo '<span class="bg recentwork_carousel__bg"></span>';

		                        	if($rwStyle == 2 || $rwStyle == 3) echo $cat;

			                        // GET THE POST TITLE
			                        echo '<h4 class="recentwork_carousel__crsl-title">' . get_the_title() . '</h4>';

			                        // GET ALL POST CATEGORIES
		                        	if($rwStyle != 2 && $rwStyle != 3) echo $cat;

		                        echo '</div>';

		                        echo '</a>';

		                        echo '</li>';
		                    }
		                    wp_reset_query();
		                }
	                ?>

	            </ul>
	        </div><!-- /.recentwork_carousel__crsl-wrapper -->
        </div>
    </div>
</div><!-- end row // recentworks_carousel default-style -->



        <?php
	}

	/**
	 * This method is used to retrieve the configurable options of the element.
	 * @return array The list of options that compose the element and then passed as the argument for the render() function
	 */
	function options()
	{
		return array (
				array (
					"name"        => __( "Recent Works Items Height", 'zn_framework' ),
					"description" => __( "Enter a height for the carousel items", 'zn_framework' ),
					"id"          => "rw_height",
					"std"         => "165",
					"type"        => "text",
					"placeholder" => "ex: 165px"
				),
                array (
                    "name"        => __( "Style", 'zn_framework' ),
                    "description" => __( "Please select the style you want to use.", 'zn_framework' ),
                    "id"          => "rw_style",
                    "std"         => "1",
                    "type"        => "select",
                    "options"     => array (
                        '1' => __( 'Style 1', 'zn_framework' ),
                        '2' => __( 'Style 2 (since v4.0) ! Place inside full-width section', 'zn_framework' ),
                        '3' => __( 'Style 3 (since v4.0) ! Place inside 12 cols', 'zn_framework' )
                    ),
                ),
				array (
					"name"        => __( "Recent Works Title", 'zn_framework' ),
					"description" => __( "Enter a title for your Recent Works element", 'zn_framework' ),
					"id"          => "rw_title",
					"std"         => "",
					"type"        => "text",
				),
				array (
					"name"        => __( "Recent Works Description", 'zn_framework' ),
					"description" => __( "Please enter a description that will appear bellow
									 the title.", 'zn_framework' ),
					"id"          => "rw_desc",
					"std"         => "",
					"type"        => "textarea",
				),
				array (
					"name"        => __( "Portfolio page link", 'zn_framework' ),
					"description" => __( "Please enter the link to your portfolio page.", 'zn_framework' ),
					"id"          => "rw_port_link",
					"std"         => "",
					"type"        => "text",
				),

                array (
                    "name"        => __( "Portfolio text button", 'zn_framework' ),
                    "description" => __( "Please enter the text for the link to your portfolio page.", 'zn_framework' ),
                    "id"          => "rw_port_link_text",
                    "std"         => "VIEW ALL",
                    "type"        => "text",
                    "dependency"  => array( 'element' => 'rw_style' , 'value'=> array('2', '3') )
                ),

				array (
					"name"        => __( "Portfolio Category", 'zn_framework' ),
					"description" => __( "Select the portfolio category to show items", 'zn_framework' ),
					"id"          => "portfolio_categories",
					"multiple"         => true,
					"std"         => "0",
					"type"        => "select",
					"options"     => WpkZn::getPortfolioCategories(),
				),
				array (
					"name"        => __( "Number of portfolio Items", 'zn_framework' ),
					"description" => __( "Please enter how many portfolio items you want to load.", 'zn_framework' ),
					"id"          => "ports_per_page",
					"std"         => "4",
					"type"        => "text"
				)
		);
	}
}
