{% extends "messages/base.html" %}

{% load i18n %}

{% block head_title %}{% trans "Invitations" %}{% endblock %}

{% block body %}
    <h1>{% trans "Invitations" %}</h1>
    
    <div class="form-toggle">
        <p><span id="join-invite-toggle">{% trans "Invite someone new to join" %}</span></p>
        
        <form id="join-invite" method="POST" action="">
            <p>{% trans "If you'd like to invite someone to join Pinax, you can enter their email address and a message for them below:" %}</p>
            <table>
                <tr>
                    <th>{{ join_request_form.email.label_tag }}</th>
                    <td>
                        {% if join_request_form.existing_users %}
                            <ul class="errorlist">
                                <li>{% trans "Someone with this email address has already joined:" %}
                                    {% for other_user in join_request_form.existing_users %}
                                    <a href="{% url profile_detail other_user %}">{{ other_user }}</a>{% endfor %}
                                </li>
                            </ul>
                        {% else %}
                            {{ join_request_form.email.errors }}
                        {% endif %}
                        {{ join_request_form.email }}
                    </td>
                </tr>
                <tr>
                    <th>{{ join_request_form.message.label_tag }}</th>
                    <td>{{ join_request_form.message.errors }}{{ join_request_form.message }}</td>
                </tr>
                <tr><td></td><td><input type="hidden" name="action" value="invite" /><input type="submit" value="{% trans "invite" %}"/></td></tr>
            </table>
        </form>
    </div>
    
    <h2>{% trans "Friendship Requests Received" %}</h2>
    
    {% if invites_received %}
        <table class="invitation_list">
            {% for invitation in invites_received %}
                {% ifequal invitation.status "2" %}
                    <tr>
                        <td colspan="3" class="friendship_request">
                            <p>From <a href="{% url profile_detail invitation.from_user.username %}">{{ invitation.from_user }}</a>
                                ({{ invitation.sent }}):</p>
                            <p class="invitation_message">{{ invitation.message }}</p>
                            <form method="post" action="">
                                <input type="hidden" name="invitation" value="{{ invitation.id }}"/>
                                <input type="hidden" name="action" value="accept"/>
                                <input type="submit" value="{% trans 'accept' %}" />
                            </form>
                        </td>
                    </tr>
                {% else %}
                    <tr>
                        <td><a href="{% url profile_detail invitation.from_user.username %}">{{ invitation.from_user }}</a></td>
                        <td>{{ invitation.sent }}</td>
                        <td class="invitation_{{ invitation.status }}">{{ invitation.get_status_display }}</td>
                    </tr>
                {% endifequal %}        
            {% endfor %}
        </table>
    {% else %}
        <p>{% trans "No friendship requests received." %}</p>
    {% endif %}
    
    
    <h2>{% trans "Friendship Requests Sent" %}</h2>
    
    {% if invites_sent %}
        <table class="invitation_list">
            {% for invitation in invites_sent %}
                <tr>
                    <td><a href="{% url profile_detail invitation.to_user.username %}">{{ invitation.to_user }}</a></td>
                    <td>{{ invitation.sent }}</td>
                    <td class="invitation_{{ invitation.status }}">{{ invitation.get_status_display }}</td>
                </tr>
            {% endfor %}
        </table>
    {% else %}
        <p>{% trans "No friendship requests sent." %}</p>
    {% endif %}
    
    
    <h2>{% trans "Join Requests Sent" %}</h2>
    
    {% if joins_sent %}
        <table class="invitation_list">
            {% for invitation in joins_sent %}
                <tr>
                    <td>{{ invitation.contact.email }}</td>
                    <td>{{ invitation.sent }}</td>
                    <td class="invitation_{{ invitation.status }}">{{ invitation.get_status_display }}</td>
                    <td>
                        {% for other_user in invitation.contact.users.all %}
                            <a href="{% url profile_detail other_user.username %}">{{ other_user }}</a>
                        {% endfor %}
                    </td>
                </tr>
            {% endfor %}
        </table>
    {% else %}
        <p>{% trans "No join requests sent." %}</p>
    {% endif %}
    
{% endblock %}

{% block extra_body %}
    <script type="text/javascript">
        $(document).ready(function() {
            $('#join-invite').hide();
            $('#join-invite-toggle').click(function() {
                $('#join-invite').slideToggle();
                $('#join-invite').autoscroll();
                return false;
            });
            if ($('#join-invite ul.errorlist').length) {
                $('#join-invite').show();
                $('#join-invite ul.errorlist').autoscroll();
            }
        });
    </script>
{% endblock %}
