{% extends "balafon/_bs_section.html" %}
{% load i18n favorite_tags balafon_utils %}

{% block section_title %}
  <span class="glyphicon glyphicon-user"></span>
  {%if partial %}{% trans "Latest contacts" %}{% else %}{% trans "All contacts" %}{%endif%}
{% endblock %}
  
{% block section_data %}
<div>
{% for l in "*ABCDEFGHIJKLMNOPQRSTUVWXYZ~" %}
  <span class="letter-filter btn btn-xs {% if letter_filter == l %}btn-primary{% else %}btn-default{% endif %}" rel="{{l}}">
    {{l}}
  </span>
{% endfor %}
</div>

<table class="table with-odd-even contacts-table">
  <tr>
    <th colspan="2">{% trans "Contacts" %}</th>
    <th class="extra-data">
      <span class="glyphicon glyphicon-envelope"></span>
      <span class="text">{% trans "City" %}</span>
    </th>
    <th class="extra-data">
      <span class="glyphicon glyphicon-earphone"></span>
      <span class="text">{% trans "Phone" %}</span>
    </th>
    <th class="extra-data">
      <span class="glyphicon glyphicon-send"></span>
      <span class="text">{% trans "Email" %}</span>
    </th>
  </tr>
  
  {% for e in entities %}{% with counter=forloop.counter %}
    {% if e.is_single_contact %}
      <tr class="{% if counter|divisibleby:2 %}even{% else %}odd{% endif %}"> 
        {% with e.single_contact as c %}
        <td colspan="2">
          <span class="glyphicon glyphicon-user"></span>
          <a href="{% url 'crm_view_contact' c.id %}">{{c.display_name }}</a>
        </td>
      
        <td class="extra-data">{{c.city.get_friendly_name|default:"&nbsp;"}}</td>
        <td class="extra-data">{{c.get_phones|join:", "|default:"&nbsp;"}}</td>
        <td class="extra-data">
          {% with em=c.get_email %}{% if em %}<a href="mailto:{{em}}">{{em}}</a>{% else %}&nbsp;{% endif %}{% endwith %}
        </td>
        
        {% endwith %}
      </tr>
    {% else %}
      
      {% if e.main_contacts %}
        {% for c in e.main_contacts %}
          <tr class="{% if counter|divisibleby:2 %}even{% else %}odd{% endif %}">
  
            <td>
              {% if forloop.counter0 == 0 %}
                <span class="glyphicon glyphicon-tower"></span>
                <a href="{% url "crm_view_entity" e.id %}">{{e.name}}</a>
              {% else %}
                &nbsp;
              {% endif %}
            </td>
            
            <td><a href="{% url "crm_view_contact" c.id %}">{{c.display_name}}</a></td>
            
            <td class="extra-data">{{c.get_city.get_friendly_name|default:"&nbsp;"}}</td>
            <td class="extra-data">{{c.get_phones|seq_to_br|default:"&nbsp;"}}</td>
            <td class="extra-data">
              {% with em=c.get_email %}{% if em %}<a href="mailto:{{em}}">{{em}}</a>{% else %}&nbsp;{% endif %}{% endwith %}
            </td>
          </tr>
        {% endfor %}
      {% else %}
        <tr class="{% if counter|divisibleby:2 %}even{% else %}odd{% endif %}">
          <td>
            <span class="glyphicon glyphicon-tower"></span>
            <a href="{% url "crm_view_entity" e.id %}">{{e.name}}</a>
          </td>
          <td></td>
          <td class="extra-data">{{e.city.get_friendly_name|default:"&nbsp;"}}</td>
          <td class="extra-data">{{e.get_phone|default:"&nbsp;"}}</td>
          <td class="extra-data">
            {% with em=e.get_email %}{% if em %}<a href="mailto:{{em}}">{{em}}</a>{% else %}&nbsp;{% endif %}{% endwith %}
          </td>
        </tr>
      {% endif %}
      
    {% endif %}
  {% endwith %}{% endfor %}
</table>
{% include "coop_cms/_pagination.html" %}
{% endblock %}

{% block section_buttons %}
  {% if partial %}
  </li><a href="{% url "crm_view_entities_list" %}">{% trans "All" %}</a></li>
  {% endif %}
{% endblock %}
