{% extends 'base.html' %}
{% load static %}
{% load helpers %}

{% block content %}
<article class="account">
    <header>
        <h1>{{ object }}</h1>
        {% if request.user == object %}
            <p><a href="{% url 'account-update' object.pk %}">Update account</a></p>
        {% endif %}
    </header>
    <section>
        <h3>Posts</h3>
        <table>
            <thead>
                <tr>
                    <th>Post</th>
                    <th>Comments</th>
                    <th>Last comment</th>
                </tr>
            </thead>
            {% for post in posts %}
                <tr class="has-link" onclick="document.location='{% url 'core:post-detail' post.topic.pk post.pk %}'">
                    <td>
                        <h4>{{ post.title }}</h4>
                        by <a href="{% url 'account-detail' post.author.pk %}">{{ post.author }}</a> » <time>{{ post.created_at }}</time>
                    </td>                
                    <td>{{ post.comments__count }}</td>
                    {% if post.comments.last is not None %}
                        {% with comment=post.comments.last %}
                        <td>
                            by <a href="{% url 'account-detail' comment.author.pk %}">{{ comment.author }}</a> » <time>{{ comment.created_at }}</time>
                        </td>
                        {% endwith %}
                    {% else %}
                        <td>No comments</td>
                    {% endif %}
                </tr>
            {% empty %}
                <tr>
                    <td colspan="3">No posts</td>
                </tr>
            {% endfor %}
            <tbody>
                
            </tbody>
        </table>
    </section>
    <section>
        <h3>Comments</h3>

        <table>
            <thead>
                <tr>
                    <th>Content</th>
                    <th>Post</th>
                    <th>Date</th>
                </tr>
            </thead>
            {% for comment in comments %}
                <tr>
                    <td>{{ comment.content|markdown|safe|truncatechars_html:64 }}</td>
                    {% with post=comment.content_object %}
                    <td>
                        <h4><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h4>
                        by <a href="{% url 'account-detail' post.author.pk %}">{{ post.author }}</a> » <time>{{ post.created_at }}</time>
                    </td>
                    {% endwith %}
                    <td><time>{{ comment.created_at }}</time></td>
                </tr>
            {% empty %}
                <tr>
                    <td colspan="3">No comments</td>
                </tr>
            {% endfor %}
            <tbody>
                
            </tbody>
        </table>
    </section>
</article>
{% endblock %}
