<?php
get_header();

$archive_before_content = pzy_get_option('blog_archive_before_content');
$archive_after_content = pzy_get_option('blog_archive_after_content');

echo '<div class="container">';
if (!pzy_load_elementor_location('archive')) {
    pzy_breadcrumb_enabled() && pzy_breadcrumb();
    ?>
    <div class="pzy-archive-section pzy-section <?php echo pzy_breadcrumb_enabled()?'':'pzy-mt-40'?>">
        <header>
            <div class="pzy-header-title">
                <?php the_archive_title('<h1>','</h1>'); ?>
            </div>
            <div class="pzy-header-divider"></div>
            <div class="pzy_catalog_ordering">
                <i class="pzy_catalog_ordering_icon" data-feather="filter"></i>
                <span class="pzy_catalog_ordering_title"><?php esc_html_e('مرتب سازی بر اساس:', 'pezeshkyar') ?></span>
                <form method="get" id="pzy-sort-posts">
                    <input type="hidden" name="paged" value="<?php echo get_query_var('paged', '') ?>"/>
                    <select name="orderby" id="pzy-orderby" class="orderby">
                        <option value=""><?php esc_html_e('پیش فرض', "pezeshkyar") ?></option>
                        <option value="views" <?php isset($_GET['orderby']) && selected($_GET['orderby'], 'views'); ?>><?php esc_html_e('پربازدیدها', "pezeshkyar") ?></option>
                        <option value="popular" <?php isset($_GET['orderby']) && selected($_GET['orderby'], 'popular'); ?>><?php esc_html_e('محبوبیت', "pezeshkyar") ?></option>
                        <option value="new" <?php isset($_GET['orderby']) && selected($_GET['orderby'], 'new'); ?>><?php esc_html_e('جدیدترین', "pezeshkyar") ?></option>
                    </select>
                </form>
            </div>
        </header>
        <?php
            if($archive_before_content){
                pzy_load_element_template($archive_before_content);
            }
        ?>
        <div class="pzy-section-content pzy-mt-40">
            <div class="pzy-archive">
                <?php
                $archive_sidebar_position = pzy_get_option('archive_sidebar_position');
                ?>
                <?php if ($archive_sidebar_position == 'before'): ?>
                    <aside class="pzy-archive-sidebar">
                        <?php get_sidebar('blog'); ?>
                    </aside>
                <?php endif; ?>
                <section class="pzy-archive-content">
                    <div class="pzy-blog-posts">
                        <?php
                        while (have_posts()) {
                            the_post();
                            ?>
                            <article class="pzy-post">
                                <div class="pzy-post-header">
                                    <a class="pzy-post-img-link" href="<?php the_permalink(); ?>">
                                        <img src="<?php echo get_the_post_thumbnail_url(get_the_ID()) ?>"
                                             alt="<?php echo get_the_title() ?>"/>
                                    </a>
                                    <div class="pzy-read-time">
                                        <span class="pzy-read-time-number"><?php echo estimate_reading_time_in_minutes(get_the_ID())['reading_time'] ?></span>
                                        <span class="pzy-read-time-unit"><?php esc_html_e('دقیقه', 'pezeshkyar') ?></span>
                                    </div>
                                </div>
                                <div class="pzy-post-body">
                                    <h3 class="pzy-post-title"><a
                                                href="<?php the_permalink(); ?>"><?php the_title() ?></a></h3>
                                    <div class="pzy-post-excerpt"><?php echo get_the_excerpt() ?></div>
                                </div>
                                <div class="pzy-post-footer">
                                    <div class="pzy-post-info">
                                        <i class="pzy-post-info-icon" data-feather="message-square"></i>
                                        <span class="pzy-post-info-text"><?php echo get_comments_number(); ?></span>
                                    </div>

                                    <?php if (pzy_independent_analytics_enabled()): ?>
                                        <div class="pzy-post-info">
                                            <i class="pzy-post-info-icon" data-feather="bar-chart-2"></i>
                                            <span class="pzy-post-info-text">
                                                <?php 
                                                $views = get_post_meta(get_the_ID(),'iawp_total_views',true);
                                                if(is_numeric($views)){
                                                    $views = number_format($views);
                                                }
                                                ?>
                                                <?php echo $views ?>
                                            </span>
                                        </div>
                                    <?php endif; ?>


                                    <div class="pzy-cover-gap"></div>
                                    <a class="pzy-read-more" href="<?php the_permalink(); ?>">
                                        <span class="pzy-read-more-text"><?php esc_html_e('مطالعه', 'pezeshkyar'); ?></span>
                                        <i class="pzy-read-more-icon" data-feather="arrow-left"></i>
                                    </a>
                                </div>

                            </article>
                            <?php
                        }
                        ?>
                    </div>
                    <div class="pzy-pagination-links"><?php echo paginate_links(); ?></div>
                </section>
                <?php if ($archive_sidebar_position == 'after'): ?>
                    <aside class="pzy-archive-sidebar">
                        <?php get_sidebar('blog'); ?>
                    </aside>
                <?php endif; ?>
            </div>
        </div>
        
        <?php
            the_archive_description('<div class="pzy-archive-description">','</div>');
            if($archive_after_content){
                pzy_load_element_template($archive_after_content);
            }
        ?>
    </div>

    <?php
}
echo "</div>";
get_footer();