<?php
/**
 * Template part for displaying Archive Header
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package NewsMaster
 */
$args = newsmaster_get_archive_header_query_args();
$enable_archive_featured_post = newsmaster_get_option('enable_archive_featured_post');
$enable_archive_post_count = newsmaster_get_option('enable_archive_post_count');
?>
<?php newsmaster_get_breadcrumb(); ?>

<header class="page-header archive-header">
    <?php
    the_archive_title('<h1 class="page-title">', '</h1>');
    if ($enable_archive_post_count) {
        newsmaster_archive_post_count();
    }
    the_archive_description('<div class="archive-description">', '</div>');
    ?>
</header><!-- .page-header -->
<?php
if ($enable_archive_featured_post) {
    $counter = 0;
    $enable_archive_author_meta = newsmaster_get_option('enable_archive_author_meta');
    $select_author_meta = newsmaster_get_option('select_author_meta');
    $archive_author_meta_title = newsmaster_get_option('archive_author_meta_title');
    $enable_archive_date_meta = newsmaster_get_option('enable_archive_date_meta');
    $select_archive_date = newsmaster_get_option('select_archive_date');
    $archive_date_meta_title = newsmaster_get_option('archive_date_meta_title');
    $select_date_format = newsmaster_get_option('select_date_format');
    $enable_category_meta = newsmaster_get_option('enable_category_meta');
    $archive_category_label = newsmaster_get_option('archive_category_label');
    $select_category_color = newsmaster_get_option('select_category_color');
    $enable_read_time_meta = newsmaster_get_option('enable_read_time_meta');

    $query_archive_header = new \WP_Query($args);

    if ($query_archive_header->have_posts()) {


        $has_featured_post = false;
        foreach ($query_archive_header->posts as $post_check) {
            $featured_check = get_post_meta($post_check->ID, 'newsmaster_single_post_featured_post', true);
            if ($featured_check) {
                $has_featured_post = true;
                break;
            }
        }


        if ($has_featured_post) {
            ?>
            <section class="wpi-section wpi-archive-header">
                <div class="archive-header-wrapper">
                    <?php
                    while ($query_archive_header->have_posts()) {
                        $query_archive_header->the_post();
                        $newsmaster_single_post_featured = get_post_meta($post->ID, 'newsmaster_single_post_featured_post', true);

                        if ($newsmaster_single_post_featured) {
                            ++$counter;


                            if (1 === $counter) {
                                $excerpt = 'true';
                                ?>
                                <article
                                        id="archive-image-<?php the_ID(); ?>" <?php post_class('wpi-post wpi-post-default'); ?>>
                                    <?php if (has_post_thumbnail()) : ?>
                                        <div class="entry-image entry-image-highlight image-hover-effect hover-effect-shine">
                                            <a class="post-thumbnail" href="<?php the_permalink(); ?>"
                                               aria-hidden="true" tabindex="-1">
                                                <?php
                                                the_post_thumbnail('medium_large', array(
                                                    'alt' => the_title_attribute(array('echo' => false)),
                                                ));
                                                ?>
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                    <div class="entry-details">
                                        <?php
                                        if ($enable_category_meta) {
                                            newsmaster_post_category($select_category_color, $archive_category_label);
                                        }
                                        the_title('<h2 class="entry-title entry-title-big"><a href="' . esc_url(get_permalink()) . '">', '</a></h2>');
                                        ?>

                                        <div class="entry-meta-wrapper">
                                            <?php
                                            if ($enable_read_time_meta) {
                                                newsmaster_get_readtime();
                                                echo '<div class="entry-meta-separator"></div>';
                                            }
                                            if ($enable_archive_date_meta) {
                                                newsmaster_posted_on($select_date_format, $archive_date_meta_title, $select_archive_date);
                                                echo '<div class="entry-meta-separator"></div>';
                                            }
                                            if ($enable_archive_author_meta) {
                                                newsmaster_posted_by($select_author_meta, $archive_author_meta_title);
                                            }
                                            ?>
                                        </div><!-- .entry-meta -->

                                        <?php if ('true' === $excerpt) { ?>
                                            <div class="cs-entry__excerpt">
                                                <?php echo esc_attr(get_the_excerpt()); ?>
                                            </div>
                                        <?php } ?>
                                    </div>
                                </article>
                                <?php
                            } elseif (2 === $counter || 3 === $counter) {
                                $excerpt = get_theme_mod('archive_header_excerpt', array('false'));
                                ?>
                                <article
                                        id="archive-image-<?php the_ID(); ?>" <?php post_class('wpi-post wpi-post-default'); ?>>
                                    <?php if (has_post_thumbnail()) : ?>
                                        <div class="entry-image entry-image-big image-hover-effect hover-effect-shine">
                                            <a class="post-thumbnail" href="<?php the_permalink(); ?>"
                                               aria-hidden="true" tabindex="-1">
                                                <?php
                                                the_post_thumbnail('medium', array(
                                                    'alt' => the_title_attribute(array('echo' => false)),
                                                ));
                                                ?>
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                    <div class="entry-details">
                                        <?php
                                        if ($enable_category_meta) {
                                            newsmaster_post_category($select_category_color, $archive_category_label);
                                        }
                                        the_title('<h2 class="entry-title entry-title-medium"><a href="' . esc_url(get_permalink()) . '">', '</a></h2>');
                                        if ('true' === $excerpt) { ?>
                                            <div class="cs-entry__excerpt">
                                                <?php echo esc_attr(get_the_excerpt()); ?>
                                            </div>
                                        <?php } ?>
                                        <div class="entry-meta-wrapper">
                                            <?php
                                            if ($enable_read_time_meta) {
                                                newsmaster_get_readtime();
                                                echo '<div class="entry-meta-separator"></div>';
                                            }
                                            if ($enable_archive_date_meta) {
                                                newsmaster_posted_on($select_date_format, $archive_date_meta_title, $select_archive_date);
                                                echo '<div class="entry-meta-separator"></div>';
                                            }
                                            if ($enable_archive_author_meta) {
                                                newsmaster_posted_by($select_author_meta, $archive_author_meta_title);
                                            }
                                            ?>
                                        </div><!-- .entry-meta -->
                                    </div>
                                </article>
                                <?php
                            } else {
                                $excerpt = get_theme_mod('archive_header_excerpt', array('false'));
                                ?>
                                <article
                                        id="archive-image-<?php the_ID(); ?>" <?php post_class('wpi-post wpi-post-list wpi-post-list-reverse'); ?>>
                                    <?php if (has_post_thumbnail()) : ?>
                                        <div class="entry-image entry-image-small">
                                            <a class="post-thumbnail" href="<?php the_permalink(); ?>"
                                               aria-hidden="true" tabindex="-1">
                                                <?php
                                                the_post_thumbnail('medium', array(
                                                    'alt' => the_title_attribute(array('echo' => false)),
                                                ));
                                                ?>
                                            </a>
                                        </div>
                                    <?php endif; ?>
                                    <div class="entry-details">
                                        <?php
                                        if ($enable_category_meta) {
                                            newsmaster_post_category($select_category_color, $archive_category_label);
                                        }
                                        the_title('<h2 class="entry-title entry-title-medium"><a href="' . esc_url(get_permalink()) . '">', '</a></h2>');
                                        if ('true' === $excerpt) { ?>
                                            <div class="cs-entry__excerpt">
                                                <?php echo esc_attr(get_the_excerpt()); ?>
                                            </div>
                                        <?php } ?>
                                        <div class="entry-meta-wrapper">
                                            <?php
                                            if ($enable_read_time_meta) {
                                                newsmaster_get_readtime();
                                                echo '<div class="entry-meta-separator"></div>';
                                            }
                                            if ($enable_archive_date_meta) {
                                                newsmaster_posted_on($select_date_format, $archive_date_meta_title, $select_archive_date);
                                                echo '<div class="entry-meta-separator"></div>';
                                            }
                                            if ($enable_archive_author_meta) {
                                                newsmaster_posted_by($select_author_meta, $archive_author_meta_title);
                                            }
                                            ?>
                                        </div><!-- .entry-meta -->
                                    </div>
                                </article>
                                <?php
                            }
                        }
                    }
                    ?>
                </div>
            </section>
            <?php
        }
    }

    wp_reset_postdata();
}
?>

