{% load i18n thumbnail cosinnus_tags static %}
{% load endless %}

{% lazy_paginate 20 rows %}

{% for group, members, pendings, admins, invited in rows %}
    
    {% group_url 'cosinnus:group-dashboard' group=group as group_url %}
    {% comment %}  Microsite is not being linked to for now
    {% if user.pk in members or user.pk in admins or group.public %}
        {% group_url 'cosinnus:group-dashboard' group=group as group_url %}
    {% else %}
        {% group_url 'cosinnus:group-microsite' group=group as group_url %}
    {% endif %}
    {% endcomment %}
    <div class="fadedown regular-space {% if filter_group and filter_group.id == group.id %}open {% endif %}">
        <div class="btn btn-{% if group.is_active and user.is_authenticated and user.pk in members or group.is_active and user.is_authenticated and user.pk in invited or group.public and group.is_active %}emphasized{% else %}default{% endif %} w100">
            <ul class="media-list">
                <li class="media">
                    {% if not group.public and not user.pk in members and not user|is_superuser or not group.is_active or SETTINGS.COSINNUS_MICROSITES_ENABLED %}
                        <span class="pull-left">
                           {% include "cosinnus/group/group_avatar_image.html" with group=group %}
                        </span>
                    {% else %}
                        <a class="pull-left" href="{{ group_url }}">
                            {% include "cosinnus/group/group_avatar_image.html" with group=group %}
                        </a>
                    {% endif %}
                    <a class="pull-right fadedown-clickarea" href="#">
                        <i class="fa fa-chevron-down"></i>
                    </a>
                    <a class="pull-right" href="{{ group_url }}">
                        <i class="fa fa-chain"></i>
                    </a>
                    <div class="media-body">
                        <span class="annotation">
                           {% include 'cosinnus/feedback/report_button_a.html' with object=group %}&nbsp;
                           
                           {% if user.is_authenticated %}
                                {% if user.pk in admins %}
                                    {% trans "Admin" %}
                                {% elif user.pk in members %}
                                    {% trans "Member" %}
                                {% elif user.pk in pendings %}
                                    {% trans "Membership requested" %}
                                {% elif user.pk in invited %}
                                    {% trans "Invited to be a member" %}
                                {% elif group.public %}
                                    {% trans "Public" context "Group" %}
                                {% else %}
                                    {% trans "Not a member" %}
                                {% endif %}
                            {% endif %}
                        </span>
                        {% if not group.is_active %}
                        	{% trans "(Inactive)" %} {{ group.name }}
                        {% elif group.public or user.pk in members or user.pk in invited or user|is_superuser or SETTINGS.COSINNUS_MICROSITES_ENABLED %}
                            <a href="{{ group_url }}">
                                {{ group.name }}
                            </a>
                        {% else %}
                            {{ group.name }}
                        {% endif %}
                    </div>
                </li>
            </ul>
        </div>
        
        <div>
            {% if group.description and group.description|length > 0 %}
	            <div class="media-body well">
                    <div class="textfield">{{ group.description|textfield }}</div>
	            </div>
            {% endif %}
            
            {% if group.is_active %}
            
	            {% if user.is_authenticated and not user.pk in invited %}
			        {% captureas modal_id %}groupModal_{{group.slug}}{% endcaptureas %}
		            <button type="button" class="btn btn-emphasized" data-toggle="modal" data-target="#{{modal_id}}">
		                   
		                <ul class="media-list">
		                    <li class="media">
		                        <a class="pull-left" href="#">
		                            <i class="fa fa-group"></i>
		                        </a>
		                        <div class="media-body">
		                            {% if user.pk in members %}
		                                {% trans "Leave" %}
		                            {% elif user.pk in pendings %}
		                                {% trans "Withdraw" %}
		                            {% else %}
		                                {% trans "Join" %}
		                            {% endif %}
		                        </div>
		                    </li>
		                </ul>
		            </button>
	            {% endif %}
                
                {% if user.is_authenticated and user.pk in admins %}
	                <button type="button" class="btn btn-emphasized" href="{% group_url 'cosinnus:group-edit' group=group %}">
	                    <ul class="media-list">
	                        <li class="media">
	                            <a class="pull-left" href="#">
	                                <i class="fa fa-pencil"></i>
	                            </a>
	                            <div class="media-body">
	                                {% trans "Edit" %}
	                            </div>
	                        </li>
	                    </ul>
	                </button>
                {% endif %}
                
                {% if group.public or user.pk in members or user|is_superuser or user.pk in invited and SETTINGS.COSINNUS_MICROSITES_ENABLED %}
	                <button type="button" class="btn btn-emphasized" href="{% group_url 'cosinnus:group-microsite' group=group %}">
	                    <ul class="media-list">
	                        <li class="media">
	                            <a class="pull-left" href="#">
	                                <i class="fa fa-group"></i>
	                            </a>
	                            <div class="media-body">
	                                {% trans "Details" %}
	                            </div>
	                        </li>
	                    </ul>
	                </button>
                {% endif %}
                
                {% if user.pk in invited %}
                    
	                <span class="app-calendar"> {# Hacky green color #}
	                    <button type="button" class="btn btn-emphasized" onclick="$('#accept-group-invitation-form').submit();">
	                        <ul class="media-list">
	                            <li class="media">
	                                <a class="pull-left" href="#">
	                                    <i class="fa fa-sign-in"></i>
	                                </a>
	                                <div class="media-body">
	                                    {% trans "Accept Invitation" %}
	                                </div>
	                            </li>
	                        </ul>
	                    </button>
	                </span>
	                
	                {% captureas modal_decline_invitation_id %}userModal_decline_invitation_{{group.id}}{% endcaptureas %}
	                <span class="app-etherpad">
	                    <button type="button" class="btn btn-emphasized" data-toggle="modal" data-target="#{{modal_decline_invitation_id}}">
	                        <ul class="media-list">
	                            <li class="media">
	                                <a class="pull-left" href="#">
	                                    <i class="fa fa-close"></i>
	                                </a>
	                                <div class="media-body">
	                                    {% trans "Decline Invitation" %}</span>
	                                </div>
	                            </li>
	                        </ul>
	                    </button>
	                </span>
	                
	                {# hidden form, triggered by accept-button JS #}
	                <form id="accept-group-invitation-form" action="{% group_url 'cosinnus:group-user-accept' group=group %}" method="post" style="display: none;" >{% csrf_token %}
	                </form>
	                
	                {% captureas label %}{% blocktrans %}Do you really want to decline the invitation?{% endblocktrans %}{% endcaptureas %}
	                {% captureas title %}{% trans "Decline Invitation" %}{% endcaptureas %}
	                {% captureas action %}{% group_url 'cosinnus:group-user-decline' group=group %}{% endcaptureas %}
	                {% include "cosinnus/modal_box.html" with id=modal_decline_invitation_id label=label title=title form_action=action %}
                {% endif %}
                
                {% if user.pk in admins or user|is_superuser %}
	                <button type="button" class="btn btn-emphasized" href="{% url 'cosinnus:group-deactivate' group_id=group.id %}">
	                    <ul class="media-list">
	                        <li class="media">
	                            <a class="pull-left" href="#">
	                                <i class="fa fa-times"></i>
	                            </a>
	                            <div class="media-body">
	                                {% trans "Deactivate" %}
	                            </div>
	                        </li>
	                    </ul>
	                </button>
                {% endif %}
                
                {% include 'cosinnus/feedback/report_button_btn.html' with object=group %}
                
            {% else %} {% comment %} --- Group inactive --- {% endcomment %}
            	
            	{% if user|is_superuser or user.pk in admins %}
	                <button type="button" class="btn btn-emphasized" href="{% url 'cosinnus:group-activate' group_id=group.id %}">
	                    <ul class="media-list">
	                        <li class="media">
	                            <a class="pull-left" href="#">
	                                <i class="fa fa-flash"></i>
	                            </a>
	                            <div class="media-body">
	                                {% trans "Re-Activate" %}
	                            </div>
	                        </li>
	                    </ul>
	                </button>
                {% endif %}
            	
            {% endif %}
        </div>
        
    </div><!-- fadedown -->
    
    {% if user.is_authenticated %}    
        {% if user.pk in members %}
            {% captureas label %}{% blocktrans with title=group.name %}Do you really want to leave „{{ title }}“?{% endblocktrans %}{% endcaptureas %}
            {% captureas title %}{% trans "Leave" %}{% endcaptureas %}
            {% captureas action %}{% group_url 'cosinnus:group-user-leave' group=group %}{% endcaptureas %}
        {% elif user.pk in pendings %}
            {% captureas label %}{% blocktrans with title=group.name %}Do you really want to withdraw your join request to „{{ title }}“?{% endblocktrans %}{% endcaptureas %}
            {% captureas title %}{% trans "Withdraw Join Request" %}{% endcaptureas %}
            {% captureas action %}{% group_url 'cosinnus:group-user-withdraw' group=group %}{% endcaptureas %}
        {% else %}
            {% captureas label %}{% blocktrans with title=group.name %}Do you wish to request to join „{{ title }}“?{% endblocktrans %}{% endcaptureas %}
            {% captureas title %}{% trans "Join" %}{% endcaptureas %}
            {% captureas action %}{% group_url 'cosinnus:group-user-join' group=group %}{% endcaptureas %}
        {% endif %}
        
        {% include "cosinnus/modal_box.html" with id=modal_id label=label title=title form_action=action %}
    {% endif %}
    
{% empty %}
   {% if group_type == 1 %}
       {% trans "There are no groups." as no_group_message %}
   {% elif group_type == 0 %}
       {% trans "There are no projects." as no_group_message %}
   {% else %}
       {% trans "There are no invitations." as no_group_message %}
   {% endif %}
   {% include 'cosinnus/common/empty_button.html' with message=no_group_message %}
{% endfor %}
        
{% show_more %}
