{% extends 'layouts/index.html' %}
{% from 'macros.html' import render_field %}
{% if current_user.is_authenticated %}
    {% set use_jquery = true %}
    {% set use_likes = true %}
{% endif %}

{% block content %}
    {% if current_user.is_authenticated %}
        <form method="POST" class="form post-form">
            {{ form.csrf_token }}
            {{ render_field(form.text, show_label=False) }}
            <input type=submit value="Create Post">
        </form>

        <hr style="margin: 1.5em 0">
    {% endif %}

    {% with post_count = posts|length %}
        <span style="display: inline-block; text-align: center; margin: 0.3em; margin-left: 1em;">Showing {{ post_count }} post{{ post_count|pluralize }}.</span>
    {% endwith %}

    {% for post in posts %}
        <div id="post-{{ post.id }}" class="post-box">
            {% if current_user.is_authenticated %}
                <i class="fas fa-heart {% if current_user.has_liked_post(post.id) %}liked{% endif %}" onclick="like({{ post.id }})"></i>
            {% endif %}
            {{ post.text }}
            <div class="post-author no-border">
                {% with comment_count = post.comments|length %}
                    <em>Posted by <a href="{{ url_for('main.view_user', username=post.author.username) }}">{{ post.author.name }}</a></em>
                    <span title="{{ post.date_posted }}">{{ post.get_time_ago() }} ago</span>. |
                    <a href="{{ url_for('main.view_post', post_id=post.id) }}"><span> {{ comment_count }} comment{{ comment_count|pluralize }}</span></a>
                    | <span class="post-like-status">{{ post.get_like_text()|safe }}</span>
                {% endwith %}
            </div>
        </div>
    {% endfor %}
{% endblock content %}
