<?php
$chlist = new GOV_novel_chapter_list([
	"count" => 99999,
	"category" => basename(get_permalink()),
	"date_format" => "F j, Y",
]);
$first_chapter = $chlist->get_first_post();
$cover_link = $first_chapter['the_link']??"#/";
$cover_title = $first_chapter['chapter_title']??"";
?>

<div class="postbody">
<article id="post-<?php the_ID(); ?>" class="post-<?php the_ID(); ?> hentry" itemscope="itemscope" itemtype="http://schema.org/CreativeWorkSeries">
	<?php breadcrumb_ts(); ?>	
	<?php if (have_posts()) : while (have_posts()) : the_post(); 
	$score = get_post_meta( get_the_ID(), 'ero_score', true ); 
	$images = rwmb_meta( 'ero_cover','type=image&size=full' );
	?>
	<div class="bixbox animefull">

			<?php 
			if (is_array($images) && sizeof($images)) {
			$image = end($images); ?>
			<div class="bigcover">
				<div class="ime">
					<a href="<?php echo $cover_link;?>" title="<?php echo $cover_title; ?>" class="lnk"></a>
					<img src="<?php echo esc_url( $image['url'] ); ?>"  alt="<?php echo esc_attr( $image['alt'] ); ?>"/>
				</div>
				<a href="<?php echo $cover_link;?>" title="<?php echo $cover_title; ?>" class="gp"><i class="fas fa-book-open" aria-hidden="true"></i></a>
			</div>
			<?php } ?>
			
		<div class="bigcontent<?php if(!$score){echo ' noscr';} if(!$images){echo ' nobigcv'; } ?>">
			<div class="thumbook">
			<div class="thumb" itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
				<?php echo Lightnovel::get_post_thumbnail(get_the_ID(), "full", 500, 370, array('itemprop'=>'image','title'=> get_the_title(get_the_ID()),'alt'=>get_the_title(get_the_ID()))); ?>
			</div>
			<div class="rt">
				<?php if($score) { ?>
				<div class="rating">
					<?php
					if(!$score || ! is_numeric($score)){ $score = '0.0';}
					$scorepros = $score * 10;
					if($score){ ?>
					<strong><?php echo GOV_lang::get('series_info_rating_label', ["rating" => $score]);?></strong>
					<div class="rating-prc" itemscope="itemscope" itemprop="aggregateRating" itemtype="//schema.org/AggregateRating">
						<meta itemprop="ratingValue" content="<?php echo $score; ?>">
						<meta itemprop="worstRating" content="1">
						<meta itemprop="bestRating" content="10">
						<meta itemprop="ratingCount" content="10">
						<div class="rtp">
							<div class="rtb"><span style="width:<?php echo $scorepros; ?>%"></span></div>
						</div>
					</div>
					<?php } ?>
				</div>
				<?php } ?>
				<?php if (GOV_bookmark::is_enabled()) { ?>
					<div data-id="<?php echo get_the_ID();?>" class="bookmark">
						<i class="far fa-bookmark" aria-hidden="true"></i> <?php echo GOV_lang::get('bookmark_bookmark_label'); ?>
					</div>
					<?php $bc = get_post_meta(get_the_ID(),'ero_bookmark_count',true); if($bc){ ?>
						<div class="bmc"><?php echo GOV_lang::get('series_bookmarked_by', ['count' => $bc]);?></div>
					<?php } ?>
				<?php } ?>
			</div>
			</div>
			<div class="infox">
				<h1 class="entry-title" itemprop="name"><?php the_title(); ?></h1>
				<div <?php if($post->post_content == '') { echo 'class="entry-content" itemprop="description"'; } else { echo 'class="ninfo"'; } ?>>
				<div class="mindesc">
					<?php echo GOV_lang::get("series_bottom_keyword_text", ["post_title" => get_the_title(get_the_ID()),"sitename"=>get_bloginfo('name')]); ?>
				</div>
				<?php $meta = get_post_meta( get_the_ID(), 'ero_alt', true ); if($meta){ ?><span class="alter"><?php echo $meta; ?></span><?php } ?>
				<div class="info-content">
				<div class="spe">
					<?php $meta = get_post_meta( get_the_ID(), 'ero_status', true ); if($meta){ ?><span><b><?php echo GOV_lang::get('series_info_status_label');?>:</b> <?php rwmb_the_value('ero_status'); ?></span><?php } ?>
					<?php echo get_the_term_list( get_the_ID(), 'type', '<span><b>'. GOV_lang::get('series_info_type_label') .':</b> ', ', ', '</span>' ); ?>
					<?php echo get_the_term_list( get_the_ID(), 'writer', '<span><b>'. GOV_lang::get('series_info_writer_label') .':</b> ', ', ', '</span>' ); ?>
					<?php echo get_the_term_list( get_the_ID(), 'artist', '<span><b>'. GOV_lang::get('series_info_artist_label') .':</b> ', ', ', '</span>' ); ?>
					<?php $meta = get_post_meta( get_the_ID(), 'ero_date', true ); if($meta){ ?><span class="split"><b><?php echo GOV_lang::get('series_info_released_label');?>:</b> <?php echo $meta; ?></span><?php } ?>
					<?php $meta = get_post_meta( get_the_ID(), 'ero_lang', true ); if($meta){ ?><span><b><?php echo GOV_lang::get('series_info_native_label');?>:</b> <?php echo $meta; ?></span><?php } ?>
					<?php if (get_option('hideseriesauthor')!=='0') { ?>
						<span class="author vcard"><b><?php echo GOV_lang::get('series_info_posted_by_label')?>:</b> <i class="fn"><?php $author_id = get_post_field( 'post_author', get_the_ID() ); echo get_the_author_meta('display_name', $author_id); ?></i></span>
					<?php } ?>
					<?php if (get_option('hideseriesdate')!=='0') { ?>
						<span class="split"><b><?php echo GOV_lang::get('series_info_posted_on_label');?>:</b> <time itemprop="datePublished" datetime="<?php the_time('c'); ?>" class="updated"><?php the_time('F j, Y'); ?></time></span>
						<span class="split"><b><?php echo GOV_lang::get('series_info_updated_on_label');?>:</b> <time itemprop="dateModified" datetime="<?php the_modified_date('c'); ?>"><?php the_modified_date('F j, Y'); ?></time></span>
					<?php } ?>
					<?php if (get_option('ts_view_count', TRUE)) { ?>
						<span><b><?php echo GOV_lang::get('series_info_view_on_label');?>:</b> <x-view id="post-views"><?php echo GOV_lang::get("wpb_get_post_views_label", ["count" => "?"]); ?></x-view></span>
					<?php } ?>
				</div>
				<?php echo get_the_term_list( get_the_ID(), 'genre', '<div class="genxed">', ' ', '</div>' ); ?>
				<div class="desc">
					<?php echo GOV_lang::get("series_new_bottom_keyword_text", ["post_title" => get_the_title(get_the_ID()),"sitename"=>get_bloginfo('name')]); ?>
				</div>
				</div>
				</div>
			</div>
		</div>
		<?php echo get_the_term_list( get_the_ID(), 'post_tag', '<div class="bottom tags">', ' ', '</div>' ); ?>
	</div>
	<?php $warning = get_post_meta(get_the_ID(),'ero_mature',true); if($warning=='Yes'){ ?>
	<div class="warning">
		<?php echo GOV_lang::get('series_warning');?>
	</div>
	<?php } ?>
	<?php $tsshr = get_option('seriessocial',1); if($tsshr){ get_template_part('template-parts/general/social','share'); } ?>
	<?php $kln = get_option('series1'); if($kln) { ?><div class="kln"><?php echo $kln; ?></div><br/><?php } ?>
	<?php if($post->post_content) { ?>
	<div class="bixbox synp">
		<div class="releases"><h2><?php echo GOV_lang::get('series_info_synopsis_label', ["title" => get_the_title()]);?></h2></div>
		<div class="entry-content" itemprop="description">
			<?php the_content(); ?>
		</div>
	</div>
	<?php } endwhile; endif; ?>
	<?php if(get_option('galleryanime')=='1'){ $images = rwmb_meta('ero_gallery'); if($images){ ?>
	<div class="bixbox image-list">
		<div class="releases"><h2><?php echo GOV_lang::get('series_gallery_label', ["post_title" => get_the_title()]);?></h2></div>
		<div id="gallery"  class="owl-carousel">
			<?php
				foreach ( $images as $image ) {
					echo '<a href="', $image['full_url'], '"><img src="', $image['full_url'], '"></a>';
				} ?>
		</div>
	</div>
	<?php } } ?>
	<?php $kln = get_option('series2'); if($kln) { ?><div class="kln"><?php echo $kln; ?></div><br/><?php } ?>

	<?php 
		$ts_sh = new GOV_series_history();
		if ($ts_sh->is_enabled()) {
			get_template_part('template-parts/series/series-history'); 
		}
	?>

	<?php 
		$ep = Lightnovel::get_latest_chapters(get_the_ID(), TRUE);
		$epid = (int)$ep['id'];
		$epl = $ep['chapter'];
		$eplv = isset($ep['volume']) ? $ep['volume'] : '';
	?>
	<div class="bixbox bxcl epcheck">
		<div class="releases"><h2><?php echo GOV_lang::get('series_episode_list', ["post_title" => get_the_title()]);?></h2></div>
		<?php if(get_option('firstnewepisode') == '1' && $first_chapter && is_array($first_chapter) && $epid != $first_chapter['ID']){ ?>
		<div class="lastend">
				<div class="inepcx">
					<a href="<?php echo $first_chapter['the_link'];?>">
						<span><?php echo GOV_lang::get('series_first_episode_label'); ?></span>
						<span class="epcur epcurfirst">
							<?php if($first_chapter['chapter_volume']){ 
								echo GOV_lang::get('thumbnail_volume_min_label') . " " . $first_chapter['chapter_volume'] . " ";
								echo GOV_lang::get('thumbnail_chapter_min_label') . ' ' . $first_chapter['chapter_number']; 
							} else{
								echo GOV_lang::get('thumbnail_chapter_label') . ' ' . $first_chapter['chapter_number'];
							}
							?>
						</span>
					</a>
				</div>
				<div class="inepcx">
					<a href="<?php echo get_permalink($epid); ?>">
						<span><?php echo GOV_lang::get('series_new_episode_label'); ?></span>
						<span class="epcur epcurlast">
						<?php if($eplv){ ?>
							<?php echo GOV_lang::get('thumbnail_volume_min_label');?> <?php echo $eplv; ?> <?php echo GOV_lang::get('thumbnail_chapter_min_label');?> <?php echo $epl; ?>
						<?php } else { ?>
							<?php echo GOV_lang::get('thumbnail_chapter_label');?> <?php echo $epl; ?>
						<?php } ?>
						</span>
					</a>
				</div>
		</div>
		<?php } ?>
		<?php $chlist->show(); ?>
	</div>
	<script>
		if(jQuery('.epcheck li').length < 1) jQuery('.epcheck').hide();
	</script>
	
	<?php if (comments_open()) { ?>
	<div class="bixbox">
	<div class="releases"><h3><span><?php echo GOV_lang::get('comment_label');?></span></h3></div>
	<div class="cmt commentx">
	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
	<?php echo get_post_meta($post->ID, "series", true); ?>
	<?php comments_template(); ?>
	<?php endwhile; endif; ?>
		 </div>
	</div>
	<?php } ?>
	
	<?php
	if(get_option('relatedseriesanime')=='1'){
	$metas = get_the_ID();
	$terms = get_the_terms( $metas , 'genre', 'string');
	$term_ids = wp_list_pluck($terms?:[],'term_id');
	$term_ids = array_slice($term_ids, 0, 2);
	$args=array(
	'post_type' => 'series',
	'tax_query' => array(
                    array(
                        'taxonomy' => 'genre',
                        'field' => 'id',
                        'terms' => $term_ids,
                        'operator'=> 'AND'
                     )),
	'post__not_in' => array($metas),
	'posts_per_page'=> 5,
	'orderby' => 'rand',
	'ignore_sticky_posts'=>1,
	"update_post_term_cache" => false,
	);
	$my_query = new wp_query( $args );
	if( $my_query->have_posts() ) { ?>
	<div class="bixbox">
	<div class="releases"><h3><span><?php echo GOV_lang::get('recommended_series');?></span></h3></div>
	<div class="listupd">
	<?php while( $my_query->have_posts() ) { $my_query->the_post(); get_template_part('template-parts/general/main'); } ?>
	</div>
	</div>
	<?php } wp_reset_query(); ?>
	<?php } ?>
		</article>
</div>