<?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.
 *
 */

/*
 * 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">

	<?php
	// You can start editing here -- including this comment!
	if ( have_comments() ) : ?>
		<h2 class="comments-title">
			<?php
			$comments_number = get_comments_number();
			if ( '1' === $comments_number ) {
				/* translators: %s: post title */
				printf( _x( 'One Reply to &ldquo;%s&rdquo;', 'comments title', '' ), get_the_title() );
			} else {
				printf(
					/* translators: 1: number of comments, 2: post title */
					_nx(
						'%1$s Reply to &ldquo;%2$s&rdquo;',
						'%1$s Replies to &ldquo;%2$s&rdquo;',
						$comments_number,
						'comments title',
						'weexplain'
					),
					number_format_i18n( $comments_number ),
					get_the_title()
				);
			}
			?>
		</h2>

		<ol class="comment-list">
			<?php wp_list_comments('type=comment&callback=format_comment'); ?>
		</ol>

		<?php 
	/*
			//the_comments_pagination();
			the_comments_pagination( array(
				'prev_text' => '<span class="screen-reader-text">' . __( 'Previous', '' ) . '</span>',
				'next_text' => '<span class="screen-reader-text">' . __( 'Next', '' ) . '</span>',
			) );

		if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
			<nav id="comment-nav-below" class="navigation" role="navigation">
				<h1 class="assistive-text section-heading"><?php _e( 'Comment navigation', '' ); ?></h1>
				<div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', '' ) ); ?></div>
				<div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', '' ) ); ?></div>
    		</nav>
    <?php endif; 
	*/

	endif; // Check for have_comments().

	// If comments are closed and there are comments, let's leave a little note, shall we?
	if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) : ?>

		<p class="no-comments"><?php _e( 'Comments are closed.', '' ); ?></p>
	<?php
	endif;
	
	// custom args for comment_form
	$aria_req = ' aria-required="true"';
	
	$args = array(
		'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'weexplain' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',		
		'fields' => apply_filters( 'comment_form_default_fields', array(

    		'author' => '<p class="comment-form-author">' . '<label for="author">' . 
						__( 'Name*', 'weexplain' ) . '</label> ' .
						( $req ? '<span class="required">*</span>' : '' ) .
						'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
						'" size="30"' . $aria_req . ' /></p>',
    		'email' =>
					  '<p class="comment-form-email"><label for="email">' . __( 'Email*', 'weexplain' ) . '</label> ' .
					  ( $req ? '<span class="required">*</span>' : '' ) .
					  '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
					  '" size="30"' . $aria_req . ' /></p>',
    		'url' =>
				  '<p class="comment-form-url"><label for="url">' .
				  __( 'Website', 'weexplain' ) . '</label>' .
				  '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
				  '" size="30" /></p>'
    		)
		),

	);
	$comment_form_args = array( 
		'title_reply'=> __('Post a comment', 'weexplain'), 
		'label_submit' => __('Submit', 'weexplain'),
		'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'weexplain' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
		'fields' => apply_filters( 'comment_form_default_fields', array(
    		'url' => '',
			)								 
		),		
	);
	// hide logged in napis
	add_filter( 'comment_form_logged_in', '__return_empty_string' );
	// show email and name for logged in users (for demo)
	//add_action( 'comment_form_logged_in_after', 'custom_logged_in_fields' );
	function custom_logged_in_fields()
	{
    ?>
    <p class="comment-form-author">
		<label for="author">
			<?php _e( 'Name*', 'weexplain' ); ?>
		</label>
		<input id="author" name="author" type="text" value="<?php echo esc_attr( $commenter['comment_author'] ); ?>" size="30" class="wpcf7-text" />
    </p>
    <p class="comment-form-email">
		<label for="email">
			<?php _e( 'Email*', 'weexplain' ); ?>
		</label>
		<input id="email" name="email" type="text" value="<?php echo esc_attr(  $commenter['comment_author_email'] ); ?>" size="30" class="wpcf7-text" />
   </p>
    <?php
}
	
	comment_form($comment_form_args);
	
	function format_comment($comment, $args, $depth) {
    
    	$GLOBALS['comment'] = $comment;
		
		global $wpdb;
		// get children of only approved comments
		$children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d AND comment_approved=1", $comment->comment_ID) );
		$class = 'comment-no-children no-children-depth-'.$depth;					
		if(count($children) > 0){
			$class = 'comment-has-children has-children-depth-'.$depth;				
		}
	?>       
       
        <li <?php comment_class($class); ?> id="comment-<?php echo $comment->comment_ID;//comment_ID() ?>">
                
            <div class="comment-intro">
               <div class="comment-meta">
                <div class="comment-author"><?php printf(__('%s'), get_comment_author_link()) ?></div>
                <div class="comment-time"><span><?php _e('Posted on', 'weexplain') ?></span> <a class="comment-permalink" href="<?php echo htmlspecialchars ( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s'), get_comment_date(), get_comment_time()) ?></a></div> 
            </div>
            
            <?php if ($comment->comment_approved == '0') : ?>
                <em><?php _e('Your comment is awaiting moderation.') ?></em><br />
            <?php endif; ?>
            			                    	
           	<div class="comment-body">
           		<?php comment_text(); ?>
           		<div class="comment-reply">
                	<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
            	</div>
            </div>
            
	<?php }	?>

</div>