<?php

get_header();

$search_query   = get_search_query();
global $wp_query;
$total_results  = (int) $wp_query->found_posts;
?>

<main class="search-results-page">

    <section class="search-results-header">
        <div class="container">

            <h1 class="search-title">
                <?php esc_html_e( 'نتائج البحث', 'saint' ); ?>
            </h1>

            <div class="search-meta">
                <span>
                    <i class="fas fa-search"></i>
					<?php
                    printf(
        
                        esc_html__( 'عن الكلمة: %s', 'saint' ),
                        '<span class="search-query">' . esc_html( $search_query ) . '</span>'
                    );
                    ?>
                </span>

                <span>
                    <i class="fas fa-list-ul"></i>
					<?php
                    printf(
                        _n(
                            'تم العثور على نتيجة واحدة',
                            'تم العثور على %s نتائج',
                            $total_results,
                            'saint'
                        ),
                        number_format_i18n( $total_results )
                    );
                    ?>
                </span>
            </div>

            <div class="search-again">
				<?php get_search_form(); ?>
            </div>
        </div>
    </section>

    <section class="search-results-body">
        <div class="container">

			<?php if ( have_posts() ) : ?>

                <div class="search-results-list">

					<?php
                    while ( have_posts() ) :
                        the_post();

                        $post_id         = get_the_ID();
                        $thumb           = get_the_post_thumbnail_url( $post_id, 'medium_large' );
                        $post_type_obj   = get_post_type_object( get_post_type() );
                        $post_type_label = $post_type_obj ? $post_type_obj->labels->singular_name : '';
                        $categories      = get_the_category();
                        ?>
                        <article <?php post_class( 'search-result-item' ); ?>>
                            <div class="result-content">

								<?php if ( $thumb ) : ?>
                                    <a href="<?php the_permalink(); ?>" class="result-thumbnail">
                                        <img src="<?php echo esc_url( $thumb ); ?>" alt="<?php the_title_attribute(); ?>">
                                        <div class="thumbnail-overlay">
                                            <i class="fas fa-arrow-left"></i>
                                        </div>
                                    </a>
								<?php endif; ?>

                                <div class="result-details">

                                    <div class="result-meta-top">
										<?php if ( $post_type_label ) : ?>
                                            <span>
                                                <i class="fas fa-folder-open"></i>
												<?php echo esc_html( $post_type_label ); ?>
                                            </span>
										<?php endif; ?>

                                        <span>
                                            <i class="fas fa-calendar-alt"></i>
											<?php echo esc_html( get_the_date() ); ?>
                                        </span>

										<?php if ( ! empty( $categories ) ) : ?>
                                            <span>
                                                <i class="fas fa-tags"></i>
												<?php echo esc_html( $categories[0]->name ); ?>
                                            </span>
										<?php endif; ?>
                                    </div>

                                    <h2 class="result-title">
                                        <a href="<?php the_permalink(); ?>">
											<?php the_title(); ?>
                                        </a>
                                    </h2>

                                    <div class="result-excerpt">
										<?php
                                        echo wp_kses_post(
                                            wp_trim_words( get_the_excerpt(), 40, '…' )
                                        );
                                        ?>
                                    </div>

                                    <div class="result-footer">
                                        <div class="result-author">
											<?php echo get_avatar( get_the_author_meta( 'ID' ), 40 ); ?>
                                            <span>
												<?php
                                                printf(
                                                    esc_html__( 'بواسطة %s', 'saint' ),
                                                    esc_html( get_the_author() )
                                                );
                                                ?>
                                            </span>
                                        </div>

                                        <a class="read-more-btn" href="<?php the_permalink(); ?>">
                                            <span><?php esc_html_e( 'قراءة التفاصيل', 'saint' ); ?></span>
                                            <i class="fas fa-arrow-left"></i>
                                        </a>
                                    </div>

                                </div>
                            </div>
                        </article>
					<?php endwhile; ?>

                </div>

                <!-- ترقيم الصفحات -->
				<?php if ( function_exists( 'wp_pagenavi' ) ) : ?>
                    <div class="search-pagination">
						<?php wp_pagenavi(); ?>
                    </div>
				<?php else : ?>
                    <div class="search-pagination">
						<?php
                        the_posts_pagination(
                            array(
                                'mid_size'  => 2,
                                'prev_text' => '<i class="fas fa-arrow-right"></i>',
                                'next_text' => '<i class="fas fa-arrow-left"></i>',
                            )
                        );
                        ?>
                    </div>
				<?php endif; ?>

			<?php else : ?>

                <div class="no-results">
                    <div class="no-results-icon">
                        <i class="fas fa-search"></i>
                    </div>
                    <h2><?php esc_html_e( 'لا توجد نتائج مطابقة', 'saint' ); ?></h2>
                    <p><?php esc_html_e( 'حاول استخدام كلمات مختلفة أو أكثر عمومية.', 'saint' ); ?></p>

                    <div class="search-again">
						<?php get_search_form(); ?>
                    </div>
                </div>

			<?php endif; ?>

        </div>
    </section>

</main>

<?php
get_footer();
