<?php
defined('ABSPATH') || exit('No Access!');

global $user_id, $labels, $options, $labels;
$qty_input = $options->get_option('products-slider-quantity-input');
$ht_products_slider = irk_get_head_tag('ht-products-slider', ['class' => 'title']);

$p_args = array(
    'post_type' => 'product',
    'posts_per_page' => $count ? $count : 10,
);

$i = 0;
if ($category && $category != '-1') {
    $i++;

    $tax_query = array(
        'taxonomy' => 'product_cat',
        'terms' => $category,
    );
    if ($include_children) {
        $tax_query['include_children'] = true;
    } else {
        $tax_query['field'] = 'term_id';
        $tax_query['operator'] = 'AND';
        $tax_query['include_children'] = false;
    }
    $p_args['tax_query'][0] = $tax_query;
}

if ($attribute && $attribute_id) {
    $tax_query = array(
        'taxonomy' => 'pa_' . $attribute,
        'field' => 'term_id',
        'terms' => explode(',', $attribute_id),
        'operator' => 'IN',
    );

    $p_args['tax_query'][$i] = $tax_query;
}

if (!empty($post_in)) {
    $post_in = explode(',', $post_in);
    $p_args['post__in'] = $post_in;
}

if ($instock) {
    $meta_query = array(
        'key' => '_stock_status',
        'value' => 'instock',
        'compare' => '=',
    );
    $p_args['meta_query'][0] = $meta_query;
}

switch ($orderby) {
    default:
    case 'default':
        $p_args['orderby'] = 'date';
        break;
    case 'random':
        $p_args['orderby'] = 'rand';
        break;
    case 'rating':
        $p_args['meta_key'] = '_wc_average_rating';
        $p_args['orderby'] = 'meta_value_num';
        break;
    case 'total-sales':
        $p_args['meta_key'] = 'total_sales';
        $p_args['meta_compare'] = '!=';
        $p_args['meta_value'] = '0';
        $p_args['orderby'] = 'meta_value_num';
        break;
}

$products = new WP_Query($p_args);
?>
<?php if ($products->have_posts()): ?>
    <div class="row">
        <div class="col-12 <?php echo $promotion_single ? 'col-lg-10' : ''; ?>">
            <?php
            $all_url = null;
            if (empty($post_in) && $options->get_option('products-slider-view-all')) {
                if ($category == -1 || $category == '') {
                    $all_url = '<a href="' . get_permalink(wc_get_page_id('shop')) . '" class="view-all">' . esc_html($labels->get_label('view-all')) . '</a>';
                } else {
                    $all_url = '<a href="' . get_term_link(get_term($category, 'product_cat'), 'product_cat') . '" class="view-all">' . esc_html($labels->get_label('view-all')) . '</a>';
                }
            }

            echo $args['before_widget'];
            echo $args['before_title'] . '<span>' . irk_get_icon($icon) . apply_filters('widget_title', $title) . '</span>' . $all_url . $args['after_title'];
            ?>
            <div class="slider-inner">
                <div class="owl-carousel">
                    <?php while ($products->have_posts()): ?>
                        <?php
                        $products->the_post();
                        $product_id = $products->post->ID;
                        $product = wc_get_product($product_id);
                        $second_image = irk_second_image($product, 'products-slider');
                        $additional_class = $second_image ? 'has-secondary-image' : '';
                        ?>
                        <div <?php wc_product_class('item ' . $additional_class, $product_id); ?>>
                            <?php echo irk_product_badges_html($product_id); ?>
                            <a href="<?php echo $product->get_permalink(); ?>" title="<?php echo $product->get_name(); ?>">
                                <div class="img-wrapper">
                                    <?php
                                    echo $product->get_image('woocommerce_thumbnail', ['class' => 'img-fluid']);
                                    if ($second_image) {
                                        echo wp_get_attachment_image($second_image, 'woocommerce_thumbnail', false, ['class' => 'secondary-image img-fluid']);
                                    }
                                    ?>
                                </div>
                                <?php echo $ht_products_slider['open'] . $product->get_name() . $ht_products_slider['close']; ?>
                                <?php echo $product->get_price_html(); ?>
                            </a>
                            <?php if ($options->get_option('products-slider-add2cart')): ?>
                                <?php if (irk_active_quantity_input($product)): ?>
                                    <?php echo $qty_input ? woocommerce_quantity_input(['min_value' => apply_filters('woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product), 'max_value' => apply_filters('woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product),], $product, false) : ''; ?>
                                    <a href="#" class="ajax-add-to-cart add-to-cart button"
                                       data-id="<?php echo $product_id; ?>">
                                        <?php if ($qty_input): ?>
                                            <span class="plus" data-icon="dk-plus"></span>
                                        <?php else: ?>
                                            <span class="button-text"><?php echo esc_html($product->add_to_cart_text()); ?></span>
                                        <?php endif; ?>
                                        <img src="<?php echo IRK_IMG_URI; ?>oval.svg" class="ajax-loader" width="24"
                                             height="24" alt="loader">
                                    </a>
                                <?php else: ?>
                                    <a class="add-to-cart button" href="<?php echo $product->get_permalink(); ?>"
                                       data-id="<?php echo $product_id; ?>"><?php echo esc_html($product->add_to_cart_text()); ?></a>
                                <?php endif; ?>
                            <?php endif; ?>
                            <div class="product-actions">
                                <?php if ($options->get_option('products-slider-quick-view')): ?>
                                    <a href="#" class="quick-view" data-modal="#quick-view-modal"
                                       data-id="<?php echo $product_id; ?>"
                                       title="<?php echo esc_attr($labels->get_label('quick-view')); ?>"></a>
                                <?php endif; ?>
                                <?php if ($options->get_option('products-slider-favorites') && $user_id): ?>
                                    <a href="#"
                                       class="add-to-favorite favorites <?php echo IRK_Favorites_Manager::is_favorite($user_id, $product_id) ? 'active' : ''; ?>"
                                       data-pid="<?php echo $product_id; ?>"
                                       title="<?php echo esc_attr($labels->get_label('add-favorite')); ?>"></a>
                                <?php endif; ?>
                                <?php if ($options->get_option('products-slider-compare')): ?>
                                    <a href="#"
                                       class="compare add-to-compare <?php echo in_array($product_id, irk_compare_list()) ? 'active' : ''; ?>"
                                       data-id="<?php echo $product_id; ?>"
                                       title="<?php echo esc_attr($labels->get_label('add-compare')); ?>"></a>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endwhile; ?>
                    <?php wp_reset_postdata(); ?>
                </div>
            </div>
            <?php echo $args['after_widget']; ?>
        </div>
        <?php if ($promotion_single): ?>
            <div class="promotion-single-wrapper col-12 col-lg-2">
                <?php
                $post__in = $promotion_single;
                echo '<div class="widget-promotion-single widget">';
                echo '<header class="header"><h3 class="clearfix">' . esc_html($promotion_title) . '</h3></header>';
                include(IRK_TPL_PATH . 'widgets/promotion-single.php');
                echo '</div>';
                ?>
            </div>
        <?php endif; ?>
    </div>
<?php endif;