<?php
if (!empty(SITE_CATEGORIES) && is_array(SITE_CATEGORIES)) {
    foreach (SITE_CATEGORIES as $item) {
        $type = (!empty($item["type"])) ? $item["type"] : null;
        $post_types = (!empty($item["post_type"]) && is_array($item["post_type"])) ? $item["post_type"] : array();
        $orderby = (!empty($item["order"])) ? $item["order"] : "";
        $orderby = ($orderby == 'update') ? 'modified' : 'post_date';
        $genre = (!empty($item["genre"]) && is_numeric($item["genre"])) ? $item["genre"] : null;
        if (!empty($type)) : ?>

            <?php switch ($type):
                case "slider-1": ?>
                    <?php
                    $args = array(
                        'post_type' => $post_types,
                        'post_status' => 'publish',
                        'posts_per_page' => (!empty(SLIDER_1_PER_PAGE) && is_numeric(SLIDER_1_PER_PAGE)) ? SLIDER_1_PER_PAGE : 12,
                        'orderby' => $orderby,
                        'order' => 'DESC',
                    );
                    if (!empty($genre)) $args["tax_query"][] = array(
                        'taxonomy' => 'genre',
                        'field' => 'id',
                        'terms' => $genre
                    );
                    $wp_query = new WP_Query($args);
                    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">
                                        <h3 class="name right rt-19 rt-fff rt-bold"><a href="#" class="right rt-fff"><?php echo $item["label"]; ?></a></h3>
                                        <?php if (!empty($genre)) : ?><a href="<?php echo get_term_link($genre, "genre") ?>" class="show-all right rt-30px rt-fff rt-15">مشاهده همه <i class="fa fa-angle-left left rt-18"></i></a><?php endif; ?>
                                    </div>
                                    <div class="slider-auto owl-carousel rt rt-relative rt-flex">

                                        <?php while ($wp_query->have_posts()) : $wp_query->the_post();
                                            global $post;
                                            $post_id = $post->ID;
                                            $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; ?>" title="<?php echo $post_title; ?>" rel="bookmark"><?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>
                                </div>
                            </div>
                        </section>
                    <?php endif;
                    wp_reset_query(); ?>
                <?php break;
                case "slider-2": ?>
                    <?php
                    $args = array(
                        'post_type' => $post_types,
                        'post_status' => 'publish',
                        'posts_per_page' => (!empty(SLIDER_2_PER_PAGE) && is_numeric(SLIDER_2_PER_PAGE)) ? SLIDER_2_PER_PAGE : 9,
                        'orderby' => $orderby,
                        'order' => 'DESC',
                    );
                    if (!empty($genre)) $args["tax_query"][] = array(
                        'taxonomy' => 'genre',
                        'field' => 'id',
                        'terms' => $genre
                    );
                    $wp_query = new WP_Query($args);
                    if ($wp_query->have_posts()) : ?>
                        <section class="archive-slider rt rt-relative rt-overflow">
                            <div class="main">
                                <div class="title-asli rt">
                                    <h3 class="name right rt-19 rt-fff rt-bold"><a href="#" class="right rt-fff"><?php echo $item["label"]; ?></a></h3>
                                    <?php if (!empty($genre)) : ?><a href="<?php echo get_term_link($genre, "genre") ?>" class="show-all right rt-30px rt-fff rt-15">مشاهده همه <i class="fa fa-angle-left left rt-18"></i></a><?php endif; ?>
                                </div>
                                <div class="items rt rt-relative rt-flex">
                                    <?php while ($wp_query->have_posts()) : $wp_query->the_post();
                                        global $post;
                                        $post_id = $post->ID;
                                        $post_permalink = get_the_permalink($post_id);
                                        $post_title = get_the_title($post_id);
                                        $short_post_title = yc_get_post_meta($post_id, 'title_movie');
                                        $short_post_title = (!empty($short_post_title)) ? $short_post_title : $post_title;
                                        $release = yc_get_post_meta($post->ID, "release_movie");
                                        if (!empty($release)) $short_post_title = $short_post_title . " " . $release;
                                        $genres = wp_get_post_terms($post_id, "genre"); ?>
                                        <div class="item right rt-10px rt-relative rt-overflow">
                                            <a href="<?php echo $post_permalink; ?>" class="full-link rt rt-absolute"></a>
                                            <a href="<?php echo $post_permalink; ?>"><?php the_post_thumbnail('post_small_cover', array("class" => "right pic right", "alt" => $post_title)); ?></a>
                                            <div class="inside right">
                                                <h2 class="title rt rt-14"><a href="<?php echo $post_permalink; ?>" class="rt rt-fff rt-limit"><?php echo $short_post_title; ?></a></h2>
                                                <?php if (!empty($genres)) :
                                                    $count_g = count($genres); ?>
                                                    <div class="cat-p rt rt-11 rt-555 rt-limit">
                                                        <?php foreach ($genres as $key => $genre) : ?>
                                                            <a href="<?php echo get_term_link($genre->term_id, "genre") ?>" rel="category tag"><?php echo $genre->name; ?></a>
                                                            <?php if (($key + 1) < $count_g) echo ","; ?>
                                                        <?php endforeach; ?>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                            <a href="<?php echo $post_permalink; ?>" class="icon left rt-50px rt-center rt-fff rt-18"><i class="fa fa-play"></i></a>
                                        </div>
                                    <?php endwhile; ?>
                                </div>
                            </div>
                        </section>
                    <?php endif;
                    wp_reset_query(); ?>
                <?php break;
                case "slider-3": ?>
                    <?php
                    $args = array(
                        'post_type' => $post_types,
                        'post_status' => 'publish',
                        'posts_per_page' => (!empty(SLIDER_3_PER_PAGE) && is_numeric(SLIDER_3_PER_PAGE)) ? SLIDER_3_PER_PAGE : 8,
                        'orderby' => $orderby,
                        'order' => 'DESC',
                    );
                    if (!empty($genre)) $args["tax_query"][] = array(
                        'taxonomy' => 'genre',
                        'field' => 'id',
                        'terms' => $genre
                    );
                    $wp_query = new WP_Query($args);
                    if ($wp_query->have_posts()) : ?>
                        <section class="item-slider rt">
                            <div class="main">
                                <div class="slider-1x rt rt-relative owl-carousel rt-10px rt-overflow">
                                    <?php while ($wp_query->have_posts()) : $wp_query->the_post();
                                        global $post;
                                        $post_id = $post->ID;
                                        $post_permalink = get_the_permalink($post_id);
                                        $post_banner = yc_get_post_meta($post_id, "smallbanner");
                                        $post_banner = (!empty($post_banner)) ? wp_get_attachment_image_url($post_banner, "full") : "";
                                        $post_title = get_the_title($post_id); ?>
                                        <a href="<?php echo $post_permalink; ?>" title="<?php echo $post_title; ?>" rel="bookmark" class="rt"><img src="<?php echo (!empty($post_banner)) ? $post_banner : ""; ?>" alt="<?php echo $post_title; ?>" class="rt"></a>
                                    <?php endwhile; ?>
                                </div>
                            </div>
                        </section>
                    <?php endif;
                    wp_reset_query(); ?>
                <?php break;
                case "banner": ?>
                    <div class="big-banner rt">
                        <div class="main">
                            <a href="<?php echo (!empty($item["link"])) ? $item["link"] : ""; ?>" title="<?php echo (!empty($item["excerpt"])) ? $item["excerpt"] : ""; ?>" class="rt"><img src="<?php echo (!empty($item["image"])) ? $item["image"] : ""; ?>" alt="<?php echo (!empty($item["excerpt"])) ? $item["excerpt"] : ""; ?>" class="rt"></a>
                        </div>
                    </div>
                <?php break;
                case "large_banner": ?>
                    <div class="large-banner rt">
                        <a href="<?php echo (!empty($item["link"])) ? $item["link"] : ""; ?>" title="<?php echo (!empty($item["excerpt"])) ? $item["excerpt"] : ""; ?>" class="rt"><img src="<?php echo (!empty($item["image"])) ? $item["image"] : ""; ?>" alt="<?php echo (!empty($item["excerpt"])) ? $item["excerpt"] : ""; ?>" class="rt"></a>
                    </div>
                    <?php break; ?>
            <?php endswitch; ?>
<?php endif;
    }
}
