<?php get_header();
$queried_object = get_queried_object();
$tax = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$name = $queried_object->name;
$count_item = $queried_object->count;
//get profile
$profile = get_term_meta($term_id, 'profile_' . $tax, true);
if (!empty($profile)) $profile = wp_get_attachment_image_url($profile, 'full');
if (empty($profile)) $profile = THEME_TEMPLATE_URL . '/assets/front/img/actor.png';
//birthplace
$birthplace_actor = get_term_meta($term_id, 'birthplace_' . $tax, true);
//birthday
$birthday_actor = get_term_meta($term_id, 'birthday_actor', true);
//age
$age = (!empty($birthday_actor) && $birthday_actor != 'Not available') ? date("Y") - date("Y", strtotime($birthday_actor)) : '';
//imdb id
$imdb_id = get_term_meta($term_id, 'imdb_id', true); ?>

<div class="main">
    <section class="rt actor-info">
        <figure class="profile">
            <img src="<?php echo $profile; ?>" alt="<?php echo $name; ?>">
        </figure>
        <h1 class="title-n rt rt-fff rt-align"><?php echo $name; ?></h1>
        <ul class="rt rt-align info-list">
            <?php if (is_numeric($age) && $age >= 0) : ?>
                <li class="info-it">سن : <?php echo $age; ?> سال</li>
            <?php endif; ?>
            <?php if (!empty($birthday_actor)) : ?>
                <li class="info-it">تاریخ تولد : <?php echo $birthday_actor; ?></li>
            <?php endif; ?>
            <?php if (!empty($birthplace_actor)) : ?>
                <li class="info-it">محل تولد : <?php echo $birthplace_actor; ?></li>
            <?php endif; ?>
            <li class="info-it">حرفه : <?php echo (!empty($tax) && $tax == 'director') ? 'کارگردان' : 'بازیگر'; ?></li>
        </ul>
    </section>
</div>

<?php
$paged = (!empty($_GET['page']) && is_numeric($_GET['page'])) ? $_GET['page'] : 1;
$args = array(
    'post_type' => array('movies', 'series'),
    'post_status' => 'publish',
    'posts_per_page' => 12,
    'paged' => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => $tax,
            'field' => 'term_id',
            'terms' => $term_id
        )
    )
);
$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">
                    <h2 class="name rt rt-align rt-20 rt-fff rt-bold">مجموعه آثار</h2>
                    <?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
                $totalceil = $wp_query->max_num_pages;
                if ($totalceil > 1 && $paged <= $totalceil) { ?>
                    <div class="paginate rt rt-align rt-15">

                        <?php echo paginate_links(array(
                            'base'          => add_query_arg('page', '%#%'),
                            'format'        => '',
                            'current'       => max(1, $paged),
                            'total'         => $totalceil,
                            'prev_text'     => 'صفحه پیشین',
                            'next_text'     => 'صفحه بعد',
                            'type'          => 'link',
                            'show_all'      => false,
                            'end_size'      => 1,
                            'mid_size'      => 7
                        ));
                        ?>
                    </div>
                <?php } ?>
            </div>
        </div>
    </section>
    <?php wp_reset_query(); ?>
<?php endif; ?>

<?php get_footer(); ?>