{% extends 'nav.html.twig' %}
{% block content %}
<div id="blogSinglePostContentContainer">
<section id="blogSinglePostContentSection">
<div class="container" style="padding: 6rem 0 0 0">
<h2 style="margin-bottom: 0"> {{ post.title }}</h2>
<h6 class="text-secondary">{{ post.createdAt|date('d.m.y') }}</h6>
<p style="word-wrap: break-word;">{{ post.content|nl2br }}</p>
{% if post.imagePath is not null %}
<div class="d-flex justify-content-center">
<img src="/assets/images/{{ post.imagePath }}" style="max-width: 500px; height: auto; min-width: 400px">
</div>
{% endif %}
</div>
</section>
{% if comments is not empty or app.user%}
<section id="blogSinglePostCommentsSection">
<div class="container">
{% for comment in comments %}
{% if loop.first %}
<h3>
Комментариев: {{ comments|length }}
</h3>
{% endif %}
<div class="my-4 d-flex" id="comment" style="position: relative">
{% if comment.user.iconPath %}
<img class="avatar mr-3 rounded-circle" src="/assets/images/icons/{{ comment.user.iconPath }}" alt="avatar">
{% else %}
<img class="avatar mr-3 rounded-circle" src="/assets/images/icons/iconProfile.png" alt="avatar">
{% endif %}
<div>
<div class="mb-2">
<h5 class="m-0">{{ comment.user.name }} {{ comment.user.lastname }}</h5>
<span class="me-3 small">{{ comment.createdAt|date('d.m.y H:i') }}</span>
</div>
<p> {{ comment.content}} </p>
</div>
</div>
{% endfor %}
{% endif %}
{% if app.user %}
<div>
<h3>Оставить комментарий</h3>
<form class="row g-3 mt-2" id="singlePostCommentForm">
<div class="col-12">
<label class="form-label">Комментарий</label>
<textarea class="form-control" name="addComment[text]" rows="3" style="resize: none" ></textarea>
</div>
<div class="col-12">
<button type="button" class="btn btn-primary" onclick="sunbmitCommentForm()">Оставить комментарий</button>
</div>
<input type="text" name="addComment[userId]" style="display: none" value="{{ app.user.id }}">
<input type="text" name="addComment[postId]" style="display: none" value="{{ post.id }}">
</form>
</div>
{% endif %}
</div>
</section>
</div>
{% endblock %}
{% block pagescripts %}
<script>
/**
* Отправляет данные с формы добавления комментария на сервер
*/
function sunbmitCommentForm() {
event.preventDefault();
let form = document.getElementById('singlePostCommentForm');
let formaData = new FormData(form);
let url = '/blog/addComment'
sendPostRequest(url, formaData)
.then(function () {
location.reload()
})
}
</script>
{% endblock %}