<?php

/**
 * The template for displaying all single posts
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * @package WowPress
 */

get_header();

use Wenprise\ORM\WP\Post;
use WoWPress\Models\User;

the_post();
$author = User::find(get_the_author_meta('ID'));
$tp = Post::find(get_the_ID());
?>

<section id="primary">
	<main id="main">

		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
			<div class="post-wrapper" style="--author-class:var(--color-<?= $author->color ?>)">
				<div class="post-title">
					<h1><?php the_title() ?></h1>
				</div>
				<div class="post-content"><?php the_content() ?></div>
				<div class="post-day"><?= get_the_date('d') ?></div>
				<div class="post-month"><?= get_the_date('F') ?></div>
				<?php
				$category = get_the_category();
				$category = array_pop($category);
				$color = get_term_meta($category->term_id, 'color', true);
				?>
				<div class="post-category ribbon" style="--color-ribbon:#<?= ctype_xdigit($color) ? $color : 'fff' ?>"><span><?= $category->name ?></span></div>
				<div class="post-avatar"><img src="<?= $author->avatar ?>"></div>
				<div class="post-author">
					<?php if (current_user_can('edit_post', $post->ID)) : ?>
						<a href="<?= get_edit_post_link($post->ID) ?>">
							<?= $author->username ?>
						</a>
					<?php else : ?>
						<?= $author->username ?>
					<?php endif; ?>
				</div>
		</article><!-- #post-${ID} -->

		<?php if (comments_open() || get_comments_number()) : ?>
			<div class="bg-glass shadow p-auto flex flex-col gap-3">
				<h3>Kommentare: <?= get_comments_number(); ?></h3>
				<?php foreach ($tp->comments as $comment) : if($comment->comment_approved && !$comment->comment_approved == "trash") :?>
					<?=dd($comment)?>
					<div class="flex flex-col lg:flex-row gap-2 border p-2">
						<div class="lg:w-1/12">
							<?php
							$comment_author = User::where('user_login',$comment->comment_author)->first();
							if(!empty($comment_author->ID)):
								?>
								<img src="<?=$comment_author->avatar?>" alt="" class="w-full hidden lg:block">
								<div class="flex flex-row justify-center items-center text-<?=$comment_author->color?>"><?=$comment_author->username?></div>
								<?php
							else:
								?> 
							<?=$comment->comment_author?>
								<?php
							endif;
							?>
						</div>
						<div class="bg-slate-500 bg-opacity-45 p-auto w-full text-white">
							<?= $comment->comment_content ?>
						</div>
					</div>

				<?php endif; endforeach; ?>
			</div>
			<?php if(is_user_logged_in() && comments_open()) :?>
			<div class="bg-glass shadow p-auto flex flex-col gap-2">
				<!--h3>Kommentar verfassen</h3>
				
				<button class="btn btn-outline btn-alliance">Kommentar abschicken</button-->
				<?=comment_form([
					'class_container' => 'flex flex-col gap-2',
					'class_submit' => 'btn btn-outline btn-alliance',
					'title_reply' => "",
					'logged_in_as' => "",
					'comment_field' => '<textarea name="comment" id="comment" rows="10" class="w-full bg-glass border-alliance"></textarea>',
				])?>
			</div>
			<?php endif; ?>
		<?php endif; ?>

		<?php
		/* Start the Loop */
		//	while ( have_posts() ) :
		//		the_post();
		//			get_template_part( 'template-parts/content/content', 'single' );//


		//				if ( is_singular( 'post' ) ) {
		//					// Previous/next post navigation.
		//					the_post_navigation(
		//						array(
		//							'next_text' => '<span aria-hidden="true">' . __( 'Next Post', 'wowpress' ) . '</span> ' .
		//								'<span class="sr-only">' . __( 'Next post:', 'wowpress' ) . '</span> <br/>' .
		//								'<span>%title</span>',
		//							'prev_text' => '<span aria-hidden="true">' . __( 'Previous Post', 'wowpress' ) . '</span> ' .
		//								'<span class="sr-only">' . __( 'Previous post:', 'wowpress' ) . '</span> <br/>' .
		//								'<span>%title</span>',
		//						)
		//					);
		//				}

		// If comments are open, or we have at least one comment, load
		// the comment template.
		//if ( comments_open() || get_comments_number() ) {
		//		comments_template();
		//		}

		// End the loop.
		//			endwhile;
		?>

	</main><!-- #main -->
</section><!-- #primary -->

<?php
get_footer();
