<?php
/**
 * The template for displaying comments
 *
 * This is the template that displays the area of the page that contains both the current comments
 * and the comment form.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Zentorone
 */

/*
 * If the current post is protected by a password and
 * the visitor has not yet entered the password we will
 * return early without loading the comments.
 */
if ( post_password_required() ) {
	return;
}
?>

<div id="comments" class="comments-area mt-5 pt-4 border-top">

	<?php
	// You can start editing here -- including this snippet!
	if ( have_comments() ) :
		?>
		<h2 class="comments-title mb-4 h4">
			<?php
			$zentorone_comment_count = get_comments_number();
			if ( '1' === $zentorone_comment_count ) {
				printf(
					/* translators: 1: title. */
					esc_html__( 'One thought on &ldquo;%1$s&rdquo;', 'zentorone' ),
					'<span>' . wp_kses_post( get_the_title() ) . '</span>'
				);
			} else {
				printf( 
					/* translators: 1: comment count, 2: title. */
					esc_html( _nx( '%1$s thought on &ldquo;%2$s&rdquo;', '%1$s thoughts on &ldquo;%2$s&rdquo;', $zentorone_comment_count, 'comments title', 'zentorone' ) ),
					number_format_i18n( $zentorone_comment_count ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					'<span>' . wp_kses_post( get_the_title() ) . '</span>'
				);
			}
			?>
		</h2><!-- .comments-title -->

		<?php the_comments_navigation( array(
            'class' => 'comment-navigation mb-4',
            'prev_text' => '<i class="bi bi-arrow-left me-2"></i>' . esc_html__( 'Older Comments', 'zentorone' ),
            'next_text' => esc_html__( 'Newer Comments', 'zentorone' ) . '<i class="bi bi-arrow-right ms-2"></i>',
        ) ); ?>

		<ol class="comment-list list-unstyled">
			<?php
			wp_list_comments(
				array(
					'style'      => 'ol',
					'short_ping' => true,
                    'avatar_size' => 64,
                    'callback' => null, // Use default or custom callback for Bootstrap styling if needed.
				)
			);
			?>
		</ol><!-- .comment-list -->

		<?php
		the_comments_navigation( array(
            'class' => 'comment-navigation mt-4',
            'prev_text' => '<i class="bi bi-arrow-left me-2"></i>' . esc_html__( 'Older Comments', 'zentorone' ),
            'next_text' => esc_html__( 'Newer Comments', 'zentorone' ) . '<i class="bi bi-arrow-right ms-2"></i>',
        ) );

		// If comments are closed and there are comments, let's leave a little note, shall we?
		if ( ! comments_open() ) :
			?>
			<p class="no-comments mt-4 text-secondary italic"><?php esc_html_e( 'Comments are closed.', 'zentorone' ); ?></p>
			<?php
		endif;

	endif; // Check for have_comments().

    // Customizing the comment form for Bootstrap
    $commenter = wp_get_current_commenter();
    $req = get_option( 'require_name_email' );
    $aria_req = ( $req ? " aria-required='true'" : '' );

    $fields = array(
        'author' => '<div class="row mb-3"><div class="col-md-6 mb-3 mb-md-0"><label for="author" class="form-label">' . esc_html__( 'Name', 'zentorone' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label>' .
                    '<input id="author" name="author" type="text" class="form-control" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . '></div>',
        'email'  => '<div class="col-md-6"><label for="email" class="form-label">' . esc_html__( 'Email', 'zentorone' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label>' .
                    '<input id="email" name="email" type="email" class="form-control" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . '></div></div>',
    );

	comment_form( array(
        'class_form' => 'comment-form mt-5',
        'title_reply' => esc_html__( 'Leave a Reply', 'zentorone' ),
        'title_reply_to' => esc_html__( 'Leave a Reply to %s', 'zentorone' ),
        'class_submit' => 'btn btn-primary px-4 py-2',
        'comment_field' => '<div class="mb-3"><label for="comment" class="form-label">' . esc_html__( 'Comment', 'zentorone' ) . ' <span class="required">*</span></label>' .
                           '<textarea id="comment" name="comment" cols="45" rows="8" class="form-control" aria-required="true"></textarea></div>',
        'fields' => $fields,
        'submit_button' => '<button name="%1$s" type="submit" id="%2$s" class="%3$s">%4$s</button>',
        'submit_field'  => '<div class="form-submit mt-4">%1$s %2$s</div>',
    ) );
	?>

</div><!-- #comments -->
