<?php global $wp;
$current_url = home_url($wp->request);
$post_id = get_the_ID();
$user_logged = is_user_logged_in();
$comments_number = get_comments_number($post_id);
$posttype = get_post_type($post_id);
$comments_error = null;
$registration = get_option('comment_registration');
if (!comments_open($post_id)) $comments_error = 'ارسال دیدگاه بسته شده است.';
else if ($registration && !$user_logged) $comments_error = 'جهت ثبت دیدگاه ثبت نام کنید یا وارد شوید !'; ?>

<section class="rt-comments rt rt-ddd">
    <div class="main">

        <div class="rt-send rt rt-10px">
            <?php if (empty($comments_error)) { ?>
                <?php $avatar = get_user_profile(null, true); ?>
                <img src="<?php echo $avatar; ?>" class="pic rt-50px right">
                <form action="<?php echo $current_url; ?>" class="left post_comment_form" data-active="true" method="post">
                    <input type="hidden" name="parent_comment_id" value="" class="parent_comment_id">
                    <input type="hidden" name="post_id" value="<?php echo $post_id; ?>">
                    <?php wp_nonce_field('nonce_comment', 'security_comment'); ?>
                    <div class="cancel-reply rt none">
                        <span class="btn right">انصراف از پاسخ</span>
                    </div>
                    <textarea name="content_comment" class="input rt rt-fff rt-14 rt-10px content_comment" placeholder="دیدگاه خود را بنویسید ...."></textarea>
                    <div class="show-toggle rt rt-0s">
                        <?php if (!$user_logged) : ?>
                            <input class="input right rt-fff rt-14 rt-10px" type="text" placeholder="ایمیل ..." name="author_email">
                            <input class="input right rt-fff rt-14 rt-10px" type="text" placeholder="نام و نام خانوادگی ..." name="author_comment">
                        <?php endif; ?>
                        <?php if ($posttype == "movies" || $posttype == "series") : ?>
                            <label class="input right rt-fff rt-14 rt-10px">
                                <span>حاوی اسپویل</span>
                                <input type="checkbox" name="spoil_comment" class="spoil-comment">
                            </label>
                        <?php endif; ?>
                        <button class="sub left rt-fff rt-bold rt-color rt-pointer rt-14 rt-10px" type="submit">ارسال دیدگاه</button>
                    </div>
                </form>
            <?php } else { ?>
                <div class="comment-error">
                    <p><?php echo $comments_error; ?></p>
                </div>
            <?php } ?>
        </div>

        <?php
        $comments = get_comments(array(
            'post_id' => $post_id,
            'status' => 'approve'
        ));
        if ($comments) {
            $comments_by_type = separate_comments($comments);
            if (!empty($comments_by_type['comment'])) { ?>
                <ul class="rt-lists rt">
                    <?php
                    wp_list_comments(
                        array(
                            'page' => 1,
                            'reverse_top_level' => false,
                            'style' => 'ul',
                            'short_ping'  => true,
                            'callback' => array('yascode_comments', 'yascode_post_comment_list')
                        ),
                        $comments
                    ); ?>
                </ul>

            <?php $cpage = get_query_var('cpage') ? get_query_var('cpage') : 1;
                if ($cpage > 1) {
                    echo '<div class="show-more rt rt-14 rt-align"> <span class="rt-8px rt-pointer rt-rang more">دیدگاه های بیشتر <i class="fa fa-angle-down left rt-18"></i></span> </div>
                    <script>
                    parent_post_id = ' . $post_id . ',
                        cpage = ' . $cpage . ',
                        lastpage = 1
                    </script>';
                }
            }
        } else { ?>
            <p class="first-cm rt-align rt-14 rt">اولین نفر باشید دیدگاهی ثبت میکند</p>
        <?php } ?>

    </div>
</section>
<!--پایان دیدگاه-->