<?php
/**
 * Copyright © Magefan (support@magefan.com). All rights reserved.
 * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
 *
 * Glory to Ukraine! Glory to the heroes!
 */
?>
<?php
/**
 * Blog post comments template
 *
 * @var $block \Magefan\Blog\Block\Post\View\Comments
 */
?>
<?php
    $comments = $block->getCommentsCollection();
    $numberOfCommentsToDisplay = $block->getNumberOfComments();
    $session = $block->getCustomerSession();

    $canPost = $block->canPost();
?>
<div id="post-comments">
    <div class="c-count">
        <strong><?= $block->escapeHtml(__('%1 Comment(s)', $block->getPost()->getCommentsCount())) ?></strong>
    </div>
    <!-- reply form -->
    <div class="c-reply cf">
        <?php if ($image = $block->getCustomerImage()) { ?>
        <div class="c-img">
            <img src="<?= $block->escapeUrl($image) ?>" alt="<?= $block->escapeHtml($block->getCustomerName()) ?>">
        </div>
        <?php } ?>
        <div class="c-replyform <?php if ($canPost) {  echo 'no-active'; } ?>">
            <form class="comment-form-blog-recaptcha"
                  action="<?= $block->escapeUrl($block->getFormUrl()) ?>"
                  method="post"
                  data-mage-init='{"validation":{}}'>
                <?= $block->getBlockHtml('formkey') ?>
                <input type="hidden" name="post_id" value="<?= (int)($block->getPost()->getId()) ?>" />
                <textarea name="text"
                          placeholder="<?= $canPost ? $block->escapeHtml(__('Add a comment...')) : $block->escapeHtml(__('Sign in to add a comment...')) ?>" <?php if (!$canPost) {?>
                          disabled="disabled"<?php } ?>
                          data-validate="{required:true}"></textarea>
                <div class="c-btn-hld">

                    <?php if ($canPost && !$session->getCustomerGroupId()) { ?>
                    <div class="lr-hld">
                        <div class="left-hld">
                            <input type="text"
                                   name="author_nickname"
                                   value=""
                                   placeholder="<?= $block->escapeHtml(__('Full Name')) ?>"
                                   class="input-text required-entry"
                                   data-validate="{required:true}"
                                   autocomplete="off"
                                   aria-required="true">
                        </div>
                        <div class="right-hld">
                            <input type="email"
                                   name="author_email"
                                   autocomplete="email"
                                   value=""
                                   placeholder="<?= $block->escapeHtml(__('Email')) ?>"
                                   class="input-text"
                                   data-validate="{required:true, 'validate-email':true}"
                                   aria-required="true">
                        </div>
                    </div>
                    <?php } ?>

                    <?php if ($block->displayPrivacyPolicyCheckbox()) { ?>
                        <?= $block->getChildBlock('display_privacy_policy_checkbox')->setCommentType('post')->toHtml(); ?>
                    <?php } ?>
                    <div class="clearfix"></div>

                    <div id="recaptcha-append-to"><?= $block->getChildHtml('blog.post.comments.magefan.additional'); ?></div>

                    <?php if ($canPost) { ?>
                        <button type="submit"><?= $block->escapeHtml(__('Submit')) ?></button>
                    <?php } else { ?>
                        <button type="button"
                                onclick="window.location='<?= $block->escapeUrl($block->getCustomerUrl()->getLoginUrl()) ?>';">
                            <?= $block->escapeHtml(__('Sign In')) ?>
                        </button>
                    <?php } ?>

                </div>
            </form>
        </div>
    </div>
<?php
    $commentsCount = count($comments);
if ($commentsCount) {
    ?>
    <!-- comments list -->
    <div class="c-comments" >
    <?php $i = 0 ?>
    <?php foreach ($comments as $comment) { ?>
            <?php
                $i++;
                $comment->setIsHidden($i > $numberOfCommentsToDisplay);
            ?>
            <?= $block->getCommentHtml($comment) ?>
        <?php } ?>
        <?php if ($commentsCount > $numberOfCommentsToDisplay) { ?>
            <a href="#" class="c-allcomments more-comments-action" data-comment="0"><?= $block->escapeHtml(__('Load more comments')) ?></a>
        <?php } ?>
    </div>

    <!-- reply comment form -->
    <div id="c-replyform-comment" class="c-replyform c-replyform-comment" style="display:none">
        <form class="reply-form-blog-recaptcha"
              action="<?= $block->escapeUrl($block->getFormUrl()) ?>"
              method="post"
              data-mage-init='{"validation":{}}'>
            <?= $block->getBlockHtml('formkey') ?>
            <input type="hidden" name="post_id" value="<?= $block->escapeHtml($block->getPost()->getId()) ?>" />
            <input type="hidden" name="parent_id" value="" />
            <textarea class="refresh-value"
                      name="text"
                      placeholder="<?= $canPost ? $block->escapeHtml(__('Add a reply...')) : $block->escapeHtml(__('Sign in to add a comment...')) ?>" <?php if (!$canPost) {?>
                      disabled="disabled"<?php } ?>
                      data-validate="{required:true}"></textarea>
            <div class="c-btn-hld">

            <?php if ($canPost && !$session->getCustomerGroupId()) { ?>
                <div class="lr-hld">
                    <div class="left-hld">
                        <input type="text"
                               name="author_nickname"
                               value=""
                               placeholder="<?= $block->escapeHtml(__('Full Name')) ?>"
                               class="input-text required-entry refresh-value"
                               data-validate="{required:true}"
                               autocomplete="off"
                               aria-required="true">
                    </div>
                    <div class="right-hld">
                        <input type="email"
                               name="author_email"
                               autocomplete="email"
                               value=""
                               placeholder="<?= $block->escapeHtml(__('Email')) ?>"
                               class="input-text required-entry refresh-value"
                               data-validate="{required:true, 'validate-email':true}"
                               aria-required="true">
                    </div>
                </div>
                <?php } ?>
                <button type="button" class="cancel reply-cancel-action" ><?= $block->escapeHtml(__('Cancel')) ?></button>
            <?php if ($canPost) { ?>
                    <button type="submit"><?= $block->escapeHtml(__('Submit')) ?></button>
                    <?php if ($block->displayPrivacyPolicyCheckbox()) { ?>
                        <?= $block->getChildBlock('display_privacy_policy_checkbox')->setCommentType('comment')->toHtml(); ?>
                    <?php } ?>
                <?php } else { ?>
                    <?php if ($block->displayPrivacyPolicyCheckbox()) { ?>
                        <?= $block->getChildBlock('display_privacy_policy_checkbox')->setCommentType('comment')->toHtml(); ?>
                    <?php } ?>
                    <button type="button"
                            onclick="window.location='<?= $block->escapeUrl($block->getCustomerUrl()->getLoginUrl()) ?>';">
                        <?= $block->escapeHtml(__('Sign In')) ?>
                    </button>
                <?php } ?>
                <div class="clearfix"></div>
                <div id="recaptcha-reply-append-to"><?= $block->getChildHtml('blog.post.comments.magefan.reply.additional'); ?></div>
            </div>
        </form>
    </div>
<?php } ?>
</div>

<script type="text/x-magento-init">
{
    "#post-comments": {
        "Magento_Ui/js/core/app": <?= /*@noEscape*/ $block->getJsLayout() ?>
    }
}
</script>
