<?php
global $sidebar, $labels, $options, $wp_query;

$ht_archive_post = irk_get_head_tag('ht-archive-post', ['class' => 'post-title']);
$active_sidebar = is_active_sidebar('irk-sidebar-post') && array_key_exists('post', $sidebar);
$sort = $_GET['sort'] ?? '';
$type = $_GET['_type'] ?? '';
?>
<?php get_header(); ?>
<?php get_template_part('partials/main-header'); ?>
    <main id="main">
        <div class="container">
            <div class="row">
                <?php $active_sidebar ? get_sidebar('post') : null; ?>
                <div class="archive-content col-12 <?php echo $active_sidebar ? 'col-lg-9' : ''; ?>">
                    <header class="archive-header">
                        <?php irk_breadcrumbs(); ?>
                        <?php
                        if (is_post_type_archive('news') || is_post_type_archive('blog') || is_home()) {
                            if ($options->get_option('post-archive-title')) {
                                $queried_object = get_queried_object();
                                if (is_home()) {
                                    echo '<h1 class="term-name">' . esc_html($queried_object->post_title) . '</h1>';
                                } else {
                                    echo '<h1 class="term-name">' . esc_html($queried_object->labels->singular_name) . '</h1>';
                                }
                            }
                        } else {
                            if (is_search()) {
                                ?>
                                <form class="search-form" action="<?php echo esc_url(home_url('/')); ?>" method="get">
                                    <input type="search" name="s" value="<?php echo get_search_query(); ?>">
                                </form>
                                <?php
                            } else {
                                $term_id = get_queried_object()->term_id;
                                $term_description = term_description($term_id);
                                $term_icon = get_term_meta($term_id, '_irk_cat_icon', true);
                                ?>
                                <?php if ($options->get_option('post-archive-title')): ?>
                                    <h1 class="term-name"><?php echo esc_html(get_term($term_id)->name); ?></h1>
                                <?php endif; ?>
                                <?php if ($term_description && $options->get_option('post-archive-description')): ?>
                                    <div class="media-header">
                                        <?php if ($term_icon): ?>
                                            <?php echo irk_get_icon($term_icon, 'term-icon'); ?>
                                        <?php endif; ?>
                                        <div class="term-description more-less"><?php echo $term_description; ?></div>
                                    </div>
                                <?php endif; ?>
                                <?php
                            }
                        }
                        ?>
                        <?php if ($options->get_option('post-archive-sort') || $options->get_option('post-archive-type') || $options->get_option('post-archive-count')): ?>
                            <div class="footer-content clearfix">
                                <div class="post-filter">
                                    <form action="" method="get" id="post-filter">
                                        <?php if ($options->get_option('post-archive-sort')): ?>
                                            <div>
                                                <span class="title"><?php echo esc_html($labels->get_label('sorting')); ?>:&nbsp;&nbsp;</span>
                                                <select name="sort">
                                                    <option value="date"><?php echo esc_html($labels->get_label('newest')); ?></option>
                                                    <option value="comments" <?php selected($sort, 'comments'); ?>><?php echo esc_html($labels->get_label('most-comments')); ?></option>
                                                </select>
                                            </div>
                                        <?php endif; ?>
                                        <?php if ($options->get_option('post-archive-type')): ?>
                                            <div>
                                                <span class="title"><?php echo esc_html($labels->get_label('type')); ?>:&nbsp;&nbsp;</span>
                                                <select name="_type">
                                                    <option value="all"><?php echo esc_html($labels->get_label('all')); ?></option>
                                                    <option value="text" <?php selected($type, 'text'); ?>><?php echo esc_html($labels->get_label('text')); ?></option>
                                                    <option value="video" <?php selected($type, 'video'); ?>><?php echo esc_html($labels->get_label('video')); ?></option>
                                                    <option value="podcast" <?php selected($type, 'podcast'); ?>><?php echo esc_html($labels->get_label('podcast')); ?></option>
                                                </select>
                                            </div>
                                        <?php endif; ?>
                                    </form>
                                </div>
                                <?php if ($options->get_option('post-archive-count')): ?>
                                    <div class="found-posts">
                                        <strong><?php echo number_format($wp_query->found_posts); ?></strong>
                                        <?php echo esc_html($labels->get_label('posts-exists')); ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                    </header>
                    <?php if (have_posts()): ?>
                        <div class="posts">
                            <div class="row">
                                <?php while (have_posts()): ?>
                                    <?php
                                    the_post();

                                    $_post_type = get_post_meta(get_the_ID(), 'irk_post_type', true);
                                    $type_class = $_post_type ? 'type-' . $_post_type : '';
                                    ?>
                                    <div <?php post_class('post-item col-12 col-sm-6 col-md-3 ' . $type_class); ?>>
                                        <div class="post-item-inner">
                                            <div class="post-image">
                                                <span class="mask"></span>
                                                <span class="post-comments"><?php comments_number('0', '1', '%'); ?></span>
                                                <?php
                                                switch (true) {
                                                    case is_tax('news_cat'):
                                                        $news_cats = wp_get_post_terms(get_the_ID(), 'news_cat');
                                                        echo count($news_cats) ? '<span class="post-category">' . esc_html($news_cats[0]->name) . '</span>' : '';
                                                        break;
                                                    case is_tax('blog_cat'):
                                                        $blog_cats = wp_get_post_terms(get_the_ID(), 'blog_cat');
                                                        echo count($blog_cats) ? '<span class="post-category">' . esc_html($blog_cats[0]->name) . '</span>' : '';
                                                        break;
                                                    default:
                                                        $categories = get_the_category();
                                                        echo count($categories) ? '<span class="post-category">' . esc_html($categories[0]->name) . '</span>' : '';
                                                }
                                                ?>
                                                <?php echo irk_get_post_thumbnail(get_the_ID(), 'woocommerce_thumbnail', ['class' => 'img-fluid']) ?>
                                            </div>
                                            <?php echo $ht_archive_post['open']; ?>
                                            <a href="<?php echo get_permalink(); ?>" title="<?php echo get_the_title(); ?>">
                                                <?php echo get_the_title(); ?>
                                            </a>
                                            <?php echo $ht_archive_post['close']; ?>
                                            <?php if ($options->get_option('post-archive-expert')): ?>
                                                <div class="post-expert"><?php echo wp_trim_words(get_the_excerpt(), 20); ?></div>
                                            <?php endif; ?>
                                            <?php if ($options->get_option('post-archive-author') || $options->get_option('post-archive-date')): ?>
                                                <div class="post-meta clearfix">
                                                    <?php if ($options->get_option('post-archive-author')): ?>
                                                        <span class="post-author"><?php echo get_avatar(get_the_author_meta('user_email'), 24); ?><?php echo get_the_author(); ?></span>
                                                    <?php endif; ?>
                                                    <?php if ($options->get_option('post-archive-date')): ?>
                                                        <span class="post-date"><?php echo get_the_date(get_option('date_format')); ?></span>
                                                    <?php endif; ?>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                <?php endwhile; ?>
                            </div>
                        </div>
                        <nav class="pagination">
                            <?php
                            echo paginate_links(
                                array(
                                    'base' => esc_url_raw(str_replace(999999999, '%#%', get_pagenum_link(999999999, false))),
                                    'format' => '',
                                    'add_args' => false,
                                    'current' => max(1, get_query_var('paged')),
                                    'total' => $wp_query->max_num_pages,
                                    'prev_text' => '',
                                    'next_text' => '',
                                    'type' => '',
                                    'end_size' => 3,
                                    'mid_size' => 3,
                                )
                            );
                            ?>
                        </nav>
                    <?php else: ?>
                        <div class="no-posts"><?php echo esc_html($labels->get_label('no-items-found')); ?></div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </main>
<?php
get_template_part('partials/main-footer');
get_footer();