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

global $options, $labels;
$ht_posts_slider = irk_get_head_tag('ht-posts-slider', ['class' => 'title']);

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

switch ($post_type) {
    default:
    case 'post':
        $cat = $category;
        $tax = 'category';
        break;
    case 'news':
        $cat = $news_cat;
        $tax = 'news_cat';
        break;
    case 'blog':
        $cat = $blog_cat;
        $tax = 'blog_cat';
        break;
}

if ($cat && $cat != '-1') {
    $tax_query = array(
        'taxonomy' => $tax,
        'terms' => $cat,
    );
    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 (!empty($post_in)) {
    $p_args['post__in'] = explode(',', $post_in);
}

switch ($orderby) {
    default:
    case 'default':
        $p_args['orderby'] = 'date';
        break;
    case 'random':
        $p_args['orderby'] = 'rand';
        break;
    case 'comment-count':
        $p_args['orderby'] = 'comment_count';
        break;
}

$posts = new WP_Query($p_args);
?>
<?php if ($posts->have_posts()): ?>
    <?php
    $all_url = null;
    if (empty($post_in) && $options->get_option('posts-slider-view-all')) {
        if ($cat == -1 || $cat == '') {
            if ($post_type != 'post') {
                $all_url = '<a href="' . get_post_type_archive_link($post_type) . '" class="view-all">' . esc_html($labels->get_label('view-all-posts')) . '</a>';
            }
        } else {
            $all_url = '<a href="' . get_category_link($cat) . '" class="view-all">' . esc_html($labels->get_label('view-all-posts')) . '</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 ($posts->have_posts()): ?>
                <?php
                $posts->the_post();
                $post_id = $posts->post->ID;
                ?>
                <div <?php post_class('item', $post_id); ?>>
                    <a href="<?php echo get_permalink(); ?>" title="<?php echo get_the_title(); ?>">
                        <div class="img-wrapper">
                            <?php echo irk_get_post_thumbnail($post_id, 'woocommerce_thumbnail', ['class' => 'img-fluid']); ?>
                        </div>
                        <?php echo $ht_posts_slider['open'] . get_the_title() . $ht_posts_slider['close']; ?>
                        <?php if ($expert): ?>
                            <div class="expert"><?php echo wp_strip_all_tags(get_the_excerpt($posts->post->ID)); ?></div>
                        <?php endif; ?>
                        <?php if ($date): ?>
                            <div class="post-date">
                                <span class="day"><?php echo get_the_date('d'); ?></span>
                                <span class="month"><?php echo get_the_date('F'); ?></span>
                                <span class="year"><?php echo get_the_date('Y'); ?></span>
                            </div>
                        <?php endif; ?>
                        <?php if ($view_more): ?>
                            <a href="<?php echo get_the_permalink($posts->post->ID); ?>" class="post-more"><?php echo esc_html($labels->get_label('post-more')); ?></a>
                        <?php endif; ?>
                    </a>
                </div>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>
        </div>
    </div>
    <?php echo $args['after_widget']; ?>
<?php endif;