<?php
/**
 * View template for Clever Product Advanced.
 *
 * @package jws\Templates
 * @copyright 2018 CleverSoft. All rights reserved.
 */

$inc_product_ids = $ex_product_ids = $default_asset = $filter_arr = $jws_json_config = '';
if($settings['ex_product_ids'] && is_array($settings['ex_product_ids'])){
    $ex_product_ids = $settings['ex_product_ids'];
}
if($settings['inc_product_ids'] && is_array($settings['inc_product_ids'])){
    $inc_product_ids = $settings['inc_product_ids'];
}


if ( is_front_page() ) {
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;   
} else {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
}
$meta_query = WC()->query->get_meta_query();

$wc_attr = array(
    'post_type'         => 'product',
    'posts_per_page'    => $settings['posts_per_page'],
    'paged'             => $paged,
    'orderby'           => $settings['orderby'],
    'order'             => $settings['order'],
);

if(!empty($ex_product_ids)) {
  $wc_attr['post__not_in'] = $ex_product_ids;  
}

if(!empty($inc_product_ids)) {
  $wc_attr['post__in'] = $inc_product_ids;  
}

$wc_attr['product_cat']=array();
if($settings['tabs_filter'] != 'asset'){
    if($settings['default_category'] != '' && $settings['default_category'] != 'all'){
        $wc_attr['product_cat'] = $settings['default_category'];
    }
    else{
        if($settings['filter_categories']){
            $wc_attr['product_cat'] = implode(',', $settings['filter_categories']);
        }
    }
    $default_asset = $settings['asset_type'];
}
else{
    $default_asset = $settings['default_asset'];
    if($settings['filter_categories_for_asset'] != '' && $settings['filter_categories_for_asset'] != 'all'){
        $wc_attr['product_cat'] = implode(',', $settings['filter_categories_for_asset']);
    }
}

switch ($default_asset) {
    case 'featured':
    $meta_query[] = array(
        array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured',
            'operator' => 'IN'
        ),
    );
    $wc_attr['tax_query'] = $meta_query;
    break;
    case 'onsale':
    $product_ids_on_sale = wc_get_product_ids_on_sale();
    $wc_attr['post__in'] = $product_ids_on_sale;
     $wc_attr['meta_query'] = array(
        'relation' => 'OR',
        array(
            'key'           => '_sale_price',
            'value'         => 0,
            'compare'       => '>',
            'type'          => 'numeric'
        )
    );
    break;
    case 'best-selling':
    $wc_attr['meta_key'] = 'total_sales';
    $wc_attr['orderby']  = 'meta_value_num';
    break;
    case 'latest':
    $wc_attr['orderby'] = 'date';
    break;
    case 'toprate':
    $wc_attr['orderby'] = 'meta_value_num';
    $wc_attr['meta_key'] = '_wc_average_rating';
    $wc_attr['order'] = 'DESC';
    break;
    case 'deal':
    $product_ids_on_sale = wc_get_product_ids_on_sale();
    $wc_attr['post__in'] = $product_ids_on_sale;
    $wc_attr['meta_query'] = array(
        'relation' => 'AND',
        array(
            'key' => '_sale_price_dates_to',
            'value' => time(),
            'compare' => '>'
        )
    );
    break;
    default:

    
    break;
}
$settings['wc_attr'] = $wc_attr; 

$jws_wrap_class = "woocommerce idealautoducts-wrap append-class jws-wrap";
$jws_wrap_class .= ' layout-' . $settings['design'];  
$jws_json_config = '';

$settings['columns_tablet'] = isset($settings['columns_tablet']) && !empty($settings['columns_tablet']) ? $settings['columns_tablet'] : $settings['columns'];
$settings['columns_mobile'] = isset($settings['columns_mobile']) && !empty($settings['columns_mobile']) ? $settings['columns_mobile'] : $settings['columns'];

$settings['show_nav_tablet'] = isset($settings['show_nav_tablet']) && !empty($settings['show_nav_tablet']) ? $settings['show_nav_tablet'] : $settings['show_nav'];
$settings['show_nav_mobile'] = isset($settings['show_nav_mobile']) && !empty($settings['show_nav_mobile']) ? $settings['show_nav_mobile'] : $settings['show_nav'];

$grid_class = 'product-item product '.$settings['display'].' col-xl-' . $settings['columns'] . ' col-lg-' . $settings['columns_tablet'] . ' col-' . $settings['columns_mobile'] .'';

$filter_arr = array(
    'asset_type'            => $settings['asset_type'],
    'ex_product_ids'           => $ex_product_ids,
    'inc_product_ids'           => $inc_product_ids,
    'orderby'               => $settings['orderby'],
    'order'                 => $settings['order'],
    'posts_per_page'        => $settings['posts_per_page'],
    'filter_categories'     => $wc_attr['product_cat'],
    'display' => $settings['display'],
    'layout' => $settings['layout'],
    'columns' => $settings['columns'],
    'columns_tablet' => $settings['columns_tablet'],
    'columns_mobile' => $settings['columns_mobile'],
    'enble_muntirow' => $settings['enble_muntirow']
);

$jws_json_config = '';
if($settings['layout'] == 'carousel') {
    $jws_wrap_class        .= ' ' . $settings['nav_position'];
    $jws_wrap_class        .= ' jws-carousel';
    
    $settings['slides_to_show_tablet'] = isset($settings['slides_to_show_tablet']) ? $settings['slides_to_show_tablet'] : $settings['slides_to_show'];
    $settings['slides_to_show_mobile'] = isset($settings['slides_to_show_mobile']) ? $settings['slides_to_show_mobile'] : $settings['slides_to_show']; 
    
    $jws_wrap_class .= '  jws-grid-lg-' . $settings['slides_to_show']['size'] . '-cols jws-grid-md-' . $settings['slides_to_show_tablet']['size'] . '-cols jws-grid-' . $settings['slides_to_show_mobile']['size'] . '-cols';
    $grid_class .= ' slick-slide';
    $class    = 'slider-layout row';
    $settings['autoplay'] ? $settings['autoplay'] : $settings['autoplay'] = 'false';
    $settings['show_nav'] ? $settings['show_nav'] : $settings['show_nav'] = 'false';
    $settings['show_nav_tablet'] ? $settings['show_nav_tablet'] : $settings['show_nav_tablet'] = 'false';
    $settings['show_nav_mobile'] ? $settings['show_nav_mobile'] : $settings['show_nav_mobile'] = 'false';
    
    $settings['show_pag'] ? $settings['show_pag'] : $settings['show_pag'] = 'false';

    
  
    
  
        $filter_arr["slides_to_show"] = $settings['slides_to_show']['size'];
        $filter_arr["slides_to_show_tablet"] = $settings['slides_to_show_tablet']['size'];
        $filter_arr["slides_to_show_mobile"] = $settings['slides_to_show_mobile']['size'];
        $filter_arr["speed"] = $settings['speed'];    
        $filter_arr["scroll"] = $settings['scroll'];   
        $filter_arr["autoplay"] = $settings['autoplay']; 
    
        
        $filter_arr["show_pag"] = $settings['show_pag']; 
   
        $filter_arr["show_nav"] = $settings['show_nav']; 
     

    

    if($settings['enble_muntirow'] == 'yes') {
       $settings['number_row'] ? $settings['number_row'] : $settings['number_row'] = 1;
       $settings['number_row_tablet'] ? $settings['number_row_tablet'] : $settings['number_row_tablet'] = 1;
       $settings['number_row_mobile'] ? $settings['number_row_mobile'] : $settings['number_mobile'] = 1;
       
       $settings['number_col_row'] ? $settings['number_col_row'] : $settings['number_col_row'] = 1;
       $settings['number_col_row_tablet'] ? $settings['number_col_row_tablet'] : $settings['number_col_row_tablet'] = 1;
       $settings['number_col_row_mobile'] ? $settings['number_col_row_mobile'] : $settings['number_col_row_mobile'] = 1;
       
        $filter_arr["number_row"] = $settings['number_row']; 
        $filter_arr["number_row_tablet"] = $settings['number_row_tablet']; 
        $filter_arr["number_row_mobile"] = $settings['number_row_mobile']; 
        $filter_arr["number_col_row"] = $settings['number_col_row']; 
        $filter_arr["number_col_row_tablet"] = $settings['number_col_row_tablet'];  
        $filter_arr["number_col_row_mobile"] = $settings['number_col_row_mobile'];   

 
        
        
        $data_slick = 'data-slick=\'{"rows":"'.$settings['number_row'].'","slidesPerRow":"'.$settings['number_col_row'].'","slidesToShow":1 ,"slidesToScroll":1,"autoplay": '.$settings['autoplay'].',"arrows": '.$settings['show_nav'].', "dots":'.$settings['show_pag'].',
        "speed": '.$settings['speed'].', "responsive":[{"breakpoint": 1024,"settings":{"rows":"'.$settings['number_row_tablet'].'","slidesPerRow":"'.$settings['number_col_row_tablet'].'"}},
        {"breakpoint": 768,"settings":{"rows":"'.$settings['number_row_mobile'].'","slidesPerRow":"'.$settings['number_col_row_mobile'] .'"}}]}\''; 
  }else{
       $data_slick = 'data-slick=\'{"slidesToShow":'.$settings['slides_to_show']['size'].' ,"slidesToScroll": '.$settings['scroll'].',"autoplay": '.$settings['autoplay'].',"arrows": '.$settings['show_nav'].', "dots":'.$settings['show_pag'].',
        "speed": '.$settings['speed'].', "responsive":[{"breakpoint": 1024,"settings":{"slidesToShow": '.$settings['slides_to_show_tablet']['size'].',"slidesToScroll": '.$settings['scroll'].'}},
        {"breakpoint": 768,"settings":{"slidesToShow": '.$settings['slides_to_show_mobile']['size'].',"slidesToScroll": '.$settings['scroll'].'}}]}\''; 
  }
 

    
   
}else {
    $data_slick = '';
    $class    = 'grid-layout row'; 
}


if($settings['hiden_item_outside']) {
   $jws_wrap_class .= ' hidden-outside'; 
}




if(function_exists('zoo_product_hover_effect')) {
	$class .= ' hover-effect-' . zoo_product_hover_effect();
}

$product_query = new WP_Query($settings['wc_attr']);

?>
<div class="<?php echo esc_attr($jws_wrap_class) ?> " 
    data-args='<?php echo json_encode($filter_arr); ?>'
    data-slider = '<?php echo esc_attr($jws_json_config); ?>'
    data-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>">
    
    <?php if ($settings['title'] != '') :
            printf('<h3 %s><span>%s</span></h3>',$this->get_render_attribute_string('title'), $settings['title']); 
    endif; ?>
    <?php if(isset($settings['enble_banner']) && $settings['enble_banner'] == 'yes') : ?>
    <div class="row">
    <div class="col-xl-3 col-12">
    <div class="product-banner">
        <?php 
             $image_size = (!empty($settings['images_size']['width']) || !empty($settings['images_size']['height'])) ? $settings['images_size']['width'].'x'.$settings['images_size']['height'] : 'full';
             $attach_id = $settings['image']['id'];
             $img = jws_getImageBySize(array('attach_id' => $attach_id, 'thumb_size' => $image_size, 'class' => 'attachment-large wp-post-image'));
             if(!empty($img['thumbnail'])) echo ''.$img['thumbnail'];
        ?>
        <div class="banner-text">
            <?php 
                if($settings['text1']) {
                    echo '<h6>'.$settings['text1'].'</h6>'; 
                }
                if($settings['text2']) {
                    echo '<h3>'.$settings['text2'].'</h3>'; 
                }
                if($settings['text3']) {
                    echo '<p>'.$settings['text3'].'</p>'; 
                }
            ?> 
        </div>
        <?php 
            if(!empty($settings['button_text'])) {
                    echo '<a class="elementor-button btn-main" href="'.$settings['button_url']['url'].'">'.$settings['button_text'].'</a>'; 
            }
        ?>
    </div>
    </div>
    <?php endif; ?>
    <?php if(isset($settings['enble_banner']) && $settings['enble_banner'] == 'yes') echo '<div class="col-xl-9 col-12">'; ?>
    <div class="jws-head-filter has-tabs <?php echo esc_attr($settings['title'] ? 'has-border' : '');?>">
        <?php if($settings['tabs_filter'] == 'cate') { ?>
            <ul class="jws-ajax-load filter-cate ct_ul_ol">
                <?php
                if($settings['default_category'] && isset(get_term_by('slug',$settings['default_category'], 'product_cat')->name)){
                    echo '<li><a href="'.get_term_link($settings['default_category'], 'product_cat').'" class="ajax-load active" data-type="product_cat" data-value="'.$settings['default_category'].'" >' . get_term_by('slug',$settings['default_category'], 'product_cat')->name . '</a></li>';
                }
                if($settings['filter_categories']){
                    foreach ($settings['filter_categories'] as $product_cat_slug) {
                        $product_cat = get_term_by('slug', $product_cat_slug, 'product_cat');
                        $selected = '';
                        if(isset($product_cat->slug)){
                            if (isset($settings['wc_attr']['product_cat']) && $settings['wc_attr']['product_cat'] == $product_cat->slug) {
                                $selected = 'jws-selected';
                            }
                     
                            $ajax_load = ' ajax-load';
                          
                            echo '<li><a href="' . esc_url(get_term_link($product_cat->slug, 'product_cat')) . '"
                            class="' . esc_attr($selected) . $ajax_load . '" 
                            data-type="product_cat" data-value="' . esc_attr($product_cat->slug) . '" 
                            title="' . esc_attr($product_cat->name) . '">' . esc_html($product_cat->name) . '</a></li>';
                        }
                        
                    } 
                }
                ?>
            </ul>
        <?php } ?>
        <?php if($settings['tabs_filter'] == 'asset') { ?>
            <ul class="jws-ajax-load filter-asset ct_ul_ol">
              <?php
              $asset_title = '';
              switch ( $settings['default_asset'] ) {
                 case 'featured':
                 $asset_title = esc_html__( 'Featured', 'idealauto' );
                 break;
                 case 'onsale':
                 $asset_title = esc_html__( 'On Sale', 'idealauto' );
                 break;
                 case 'deal':
                 $asset_title = esc_html__( 'Deal', 'idealauto' );
                 break;
                 case 'latest':
                 $asset_title = esc_html__( 'New Arrivals', 'idealauto' );
                 break;
                 case 'best-selling':
                 $asset_title = esc_html__( 'Best Seller', 'idealauto' );
                 break;
                 case 'toprate':
                 $asset_title = esc_html__( 'Top Rate', 'idealauto' );
                 break;
                 default:
                 break;
             }
             if ( $asset_title ) { ?>
                <li class="cvca-ajax-load">
                    <a href="#" class="active ajax-load" data-type="asset_type"
                    data-value="<?php echo esc_attr( $settings['default_asset'] ) ?>"
                    title="<?php echo esc_attr( $asset_title ); ?>"><?php echo esc_attr( $asset_title ); ?></a>
                </li>

                <?php
            }
            $html = '';
            if ( $settings['filter_assets'] ) {

             foreach ( $settings['filter_assets'] as $val ) {
                switch ( $val ) {
                   case 'featured':
                   $html .= $settings['default_asset'] != 'featured' ? '<li><a href="#" class=" ajax-load" data-type="asset_type" data-value="featured" title="' . esc_html__( 'Featured', 'idealauto' ) . '">' . esc_html__( 'Featured', 'idealauto' ) . '</a></li>' : '';
                   break;
                   case 'onsale':
                   $html .= $settings['default_asset'] != 'onsale' ? '<li><a href="#" class=" ajax-load" data-type="asset_type" data-value="onsale" title="' . esc_html__( 'On Sale', 'idealauto' ) . '">' . esc_html__( 'On Sale', 'idealauto' ) . '</a></li>' : '';
                   break;
                   case 'deal':
                   $html .= $settings['default_asset'] != 'deal' ? '<li><a href="#" class=" ajax-load" data-type="asset_type" data-value="deal" title="' . esc_html__( 'Deal', 'idealauto' ) . '">' . esc_html__( 'Deal', 'idealauto' ) . '</a></li>' : '';
                   break;
                   case 'latest':
                   $html .= $settings['default_asset'] != 'latest' ? '<li><a href="#" class=" ajax-load" data-type="asset_type" data-value="latest" title="' . esc_html__( 'New Arrivals', 'idealauto' ) . '">' . esc_html__( 'New Arrivals', 'idealauto' ) . '</a></li>' : '';
                   break;
                   case 'best-selling':
                   $html .= $settings['default_asset'] != 'best-selling' ? '<li><a href="#" class=" ajax-load" data-type="asset_type" data-value="best-selling" title="' . esc_html__( 'Best Seller', 'idealauto' ) . '">' . esc_html__( 'Best Seller', 'idealauto' ) . '</a></li>' : '';
                   break;
                   case 'toprate':
                   $html .= $settings['default_asset'] != 'toprate' ? '<li><a href="#" class=" ajax-load" data-type="asset_type" data-value="toprate" title="' . esc_html__( 'Top Rate', 'idealauto' ) . '">' . esc_html__( 'Top Rate', 'idealauto' ) . '</a></li>' : '';
                   break;
                   default:
                   break;
               }
           }
       }

       echo ent2ncr( $html ); ?>
   </ul>
   <?php
}
?>
</div>
<div class="products-tab <?php echo esc_attr($class) ?>">
    
    <?php
        if($settings['layout'] == 'carousel') echo '<div class="carousel" '.$data_slick.'>';  
    
     
        while ($product_query->have_posts()) : $product_query->the_post();
             echo '<div class="'.esc_attr($grid_class).'">';    
             wc_get_template_part( 'archive-layout/content', $settings['display'] );
             echo '</div>';
        endwhile;
        
        if($settings['layout'] == 'carousel') echo '</div>'; 
    ?>

</div>
<?php if(isset($settings['enble_banner']) && $settings['enble_banner'] == 'yes') echo '</div></div>'; ?>
<?php if($settings['pagination'] != 'none' && $settings['tabs_filter'] == 'none' && $settings['layout'] == 'grid') : ?>
  <?php
  $zoo_pag_type = $settings['pagination'];
  ?>
  <?php if($zoo_pag_type == 'ajaxload' || $zoo_pag_type == 'infinity'):
        wp_enqueue_script('infinite-scroll');
  ?>
    <div class="pagination-ajax">
    <?php endif; ?>
    <?php
    //jws_pagination(3, $product_query, '', '<i class="zoo-icon-long-arrow-left"></i> ' . esc_html__('', 'idealauto'), esc_html__('', 'idealauto') . ' <i class="zoo-icon-long-arrow-right"></i>');
    ?>
    <?php if($zoo_pag_type == 'ajaxload' || $zoo_pag_type == 'infinity') : ?>

        <div class="scroller-status">
            <div class="infinite-scroll-request">
                <div class="pagination-loading"><span class="loading"><?php echo esc_html__('LOADING...','idealauto') ?></span></div>
            </div>
            <p class="infinite-scroll-last"><?php echo esc_html__('All items loaded','idealauto'); ?></p>
            <p class="infinite-scroll-error"><?php echo esc_html__('No more page','idealauto'); ?></p>
        </div>
        <?php if ($zoo_pag_type == 'ajaxload') { ?>
            <p class="view-more-button"><span><?php echo esc_html__('Load More','idealauto') ?></span></p>
        <?php } ?>
    </div>
<?php endif; ?>
<?php endif; ?>
<?php
wp_reset_postdata();
?>
</div>