<?php
get_header();
echo '<div class="container">';
pzy_breadcrumb_enabled() && pzy_breadcrumb();
?>
    <div class="pzy-section <?php echo pzy_breadcrumb_enabled()?'':'pzy-mt-40'?>">
        <header>
            <div class="pzy-header-title">
                <?php echo pzy_get_option('blog_title'); ?>
            </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>
        <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
                        $paged = max(1, get_query_var('paged', 1));
                        $args = array(
                            'post_type' => 'post',
                            'paged' => $paged,
                            'post_status' => array('publish')
                        );

                        $order = get_query_var('orderby', 'new');

                        if (isset($_GET['orderby']) && !empty($_GET['orderby'])) {
                            switch ($_GET['orderby']) {
                                case 'views':
                                    $args['meta_key'] = 'iawp_total_views'; // Assuming you have a custom field for views count
                                    $args['orderby'] = 'meta_value_num';
                                    $args['order'] = 'DESC';
                                    break;
                                case 'popular':
                                    $args['meta_key'] = 'rmp_avg_rating'; // Assuming you have a custom field for popularity
                                    $args['orderby'] = 'meta_value_num';
                                    $args['order'] = 'DESC';
                                    break;
                                case 'new':
                                    $args['orderby'] = 'date';
                                    $args['order'] = 'DESC';
                                    break;
                                default:
                                    // Default ordering can be set here
                                    break;
                            }
                        }

                        $custom_query = new WP_Query($args);

                        while ($custom_query->have_posts()) {
                            $custom_query->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(array(
                            'total' => $custom_query->max_num_pages,
                        )); ?></div>
                </section>
                <?php if ($archive_sidebar_position == 'after'): ?>
                    <aside class="pzy-archive-sidebar">
                        <?php get_sidebar('blog'); ?>
                    </aside>
                <?php endif; ?>
            </div>
        </div>
    </div>

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