<?php $page_streams = (!empty($args['page_streams'])) ? $args['page_streams'] : array();
$page_title = (!empty($page_streams['term_name'])) ? $page_streams['term_name'] : '';
if (!empty($_GET['s']) && is_search()) $page_title = 'شما برای ' . sanitize_text_field($_GET['s']) . ' جستجو کردید';
$page_description = (!empty($page_streams['term_description'])) ? $page_streams['term_description'] : '';
$post_type = array('movies', 'series');
if (!empty($page_streams['post_type'])) $post_type = $page_streams['post_type'];
$per_page = get_option('posts_per_page');
$per_page = (!empty($per_page)) ? $per_page : 12;
$paged = (get_query_var('paged')) ? get_query_var('paged') : null;
if (empty($paged) && !empty($_GET['page']) && is_numeric($_GET['page'])) $paged = (int) $_GET['page'];
else if (empty($paged)) $paged = 1;
$args = array(
    'post_type' => $post_type,
    'post_status' => 'publish',
    'posts_per_page' => $per_page,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'paged' => $paged
);
if (!empty($page_streams['term_id']) && !empty($page_streams['term_tax'])) {
    $args['tax_query'][] = array(
        'taxonomy' => $page_streams['term_tax'],
        'field' => 'term_id',
        'terms' => $page_streams['term_id']
    );
} elseif (is_search() && !empty($_GET['s'])) {
    $args = array(
        'post_type' => $post_type,
        'post_status' => 'publish',
        'posts_per_page' => $per_page,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'paged' => $paged,
        's' => sanitize_text_field($_GET['s'])
    );
} elseif ($page_streams['adv_search'] == true) {
    $post_type = (!empty($_GET['type']) && ($_GET['type'] == 'movies' || $_GET['type'] == 'series')) ? $_GET['type'] : array('movies', 'series');
    $genr = (!empty($_GET['genr'])) ? $_GET['genr'] : '';
    $args = array();
    $args['post_type'] = $post_type;
    $args['orderby'] = 'post_date';
    $args['order'] = 'DESC';
    if (!empty($genr) && $genr != 'all') {
        $args['meta_query'][] = array(
            'key'     => 'genre_movie',
            'value'   => $genr,
            'compare' => 'LIKE'
        );
    }
}
$wp_query = new WP_Query($args); ?>
<?php $genres = get_terms(
    array(
        'taxonomy' => array("genre"),
        'hide_empty' => false,
        'meta_query' => array(
            array(
                'key' => 'genretop',
                'value' => 'on',
                'compare' => '='
            )
        )
    )
);
if ($genres) : ?>
    <section class="cat-items rt rt-relative rt-overflow">
        <div class="main">
            <div class="title-asli rt">
                <h3 class="name right rt-18 rt-fff rt-bold">دسته بندی فیلم و سریال ها</h3>
            </div>
            <div class="slider-auto owl-carousel rt rt-flex">

                <?php foreach ($genres as $genre) :
                    $pic = get_term_meta($genre->term_id, 'genrepic', true);
                    $pic = (!empty($pic) && is_numeric($pic)) ? wp_get_attachment_image_url($pic, "genre_cover") : null;
                    if (empty($pic)) $pic = THEME_TEMPLATE_URL . '/assets/front/img/default-thumb.svg'; ?>
                    <a href="<?php echo get_term_link($genre->slug, $genre->taxonomy); ?>" class="right rt-relative rt-8px rt-overflow"><img src="<?php echo $pic; ?>" alt="<?php echo (!empty($genre->name)) ? $genre->name : ""; ?>" class="rt rt-absolute rt-10px">
                        <h3 class="rt rt-fff rt-bold rt-18"><?php echo (!empty($genre->name)) ? $genre->name : ""; ?></h3>
                    </a>
                <?php endforeach; ?>

            </div>
        </div>
    </section>
<?php endif; ?>

<?php if ($wp_query->have_posts()) : ?>
    <section class="archive-slider rt rt-relative rt-overflow">
        <div class="rt">
            <div class="main">
                <div class="title-asli rt">
                    <h1 class="name rt rt-align rt-20 rt-fff rt-bold"><?php echo (!empty($page_title)) ? $page_title : YC_NAME; ?></h1>
                    <?php if (!empty($page_description)) : ?><h2 class="desc rt rt-align rt-14 rt-ddd rt-400"><?php echo (!empty($page_description)) ? $page_description : ""; ?></h2><?php endif; ?>
                </div>
                <div class="inline rt rt-relative rt-align">

                    <?php while ($wp_query->have_posts()) : $wp_query->the_post();
                        global $post;
                        $post_permalink = get_the_permalink($post->ID);
                        $post_title = yc_get_post_meta($post->ID, 'title_movie', true);
                        $post_title = (!empty($post_title)) ? $post_title : get_the_title($post->ID);
                        $imdb_rate = yc_get_post_meta($post->ID, 'imdb_rate_movie', true);
                        $isDubbed = yc_get_post_meta($post->ID, 'has_dubbed', true);
                        $isSubbed = yc_get_post_meta($post->ID, 'has_subtitle', true);
                        $genre_slug = 'genre';
                        $genres = wp_get_post_terms($post->ID, $genre_slug);
                        $imdb_rate = yc_get_post_meta($post->ID, 'imdb_rate_movie', true);
                        $release = yc_get_post_meta($post->ID, "release_movie"); ?>
                        <article class="mini-p rt rt-relative rt-overflow">
                            <figure class="rt rt-relative rt-overflow rt-5px">
                                <?php if (!empty($isDubbed) || !empty($isSubbed)) : ?>
                                    <div class="chasbs">
                                        <?php if (!empty($isDubbed)) : ?>
                                            <span class="dooble rt-fff rt-11 rt-absolute"><i class="fa fa-microphone rt-13 right"></i> دوبله</span>
                                        <?php endif; ?>
                                        <?php if (!empty($isSubbed)) : ?>
                                            <span class="srt dooble rt-fff rt-11 rt-absolute"><i class="fa fa-closed-captioning rt-13 right"></i> زیرنویس</span>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                                <a href="<?php echo $post_permalink; ?>"><?php the_post_thumbnail('post_cover', array("class" => "rt", "alt" => $post_title)); ?></a>
                                <div class="hover rt rt-absolute rt-center rt-5px">
                                    <a href="<?php echo $post_permalink; ?>" class="full-link rt rt-absolute rt-z-index"></a>
                                    <i class="fa fa-play rt-31 rt-fff"></i>
                                    <?php if (!empty($genres)) :
                                        $count_g = count($genres); ?>
                                        <div class="cat-p rt">
                                            <?php foreach ($genres as $key => $genre) : ?>
                                                <a href="<?php echo get_term_link($genre->term_id, $genre_slug) ?>" rel="category tag"><?php echo $genre->name; ?></a>
                                                <?php if (($key + 1) < $count_g) echo ","; ?>
                                            <?php endforeach; ?>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                <?php if (!empty($imdb_rate)) : ?>
                                    <a href="<?php echo $post_permalink; ?>" class="imdb left rt-absolute rt-8px rt-align">
                                        <i class="fa fa-imdb rt-400 rt rt-31"></i>
                                        <span class="rt rt-12 rt-fff"><?php echo $imdb_rate; ?></span>
                                    </a>
                                <?php endif; ?>
                            </figure>
                            <h2 class="title rt rt-14 rt-overflow"><a href="<?php echo $post_permalink; ?>" class="rt-ddd rt"><?php echo $post_title; ?>
                                    <?php if (!empty($release)) echo $release; ?>
                                </a>
                            </h2>
                        </article>
                    <?php endwhile; ?>
                </div>

                <?php $pages = $wp_query->max_num_pages;
                if ($pages >= $paged) :
                    $big = 999999999; ?>
                    <div class="paginate rt rt-align rt-15">
                        <?php echo paginate_links(array(
                            'base'    => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
                            'format'  => '/page/%#%',
                            'current' => $paged,
                            'total'   => $pages,
                            'prev_text' => 'قبل',
                            'next_text' => 'بعد',
                            'type' => 'link',
                            'mid_size' => 5
                        )); ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </section>
<?php endif;
wp_reset_query(); ?>