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

{% block section_title %}{% trans "Relationships" %}{% endblock %}
  
{% block section_data %}
<table class="table table-striped">
{% if same_as %}
  {% for same_as_contact in same_as %}
  {% with same_as_contact.has_left as has_left %}
  {% spaceless %}
  <tr>
    <td>
      <a class="colorbox-form" href="{% url 'crm_make_main_contact' contact.id same_as_contact.id %}"
        title="{% trans 'Change priority order for contact' %}"
      >
        <span class="badge {% if 1 == same_as_contact.same_as_priority %}highlight{% endif %}">
          {{ same_as_contact.same_as_priority }}
        </span>
      </a>
      {% trans "Same As" %}
    </td>
    {% if same_as_contact.entity.is_single_contact %}
    <td colspan="2"><a href="{% url "crm_view_contact" same_as_contact.id %}">{{same_as_contact.fullname}}</a></td>
    {% else %}
    <td>
      <a href="{% url "crm_view_contact" same_as_contact.id %}">
        {{same_as_contact.fullname}}
      </a>
      {% if has_left %}<span class="label label-warning ut-has-left">{% trans "has left" %}</span>{% endif %}
    </td>
    <td><a href="{% url "crm_view_entity" same_as_contact.entity.id %}">{{ same_as_contact.entity }}</a></td>
    {% endif %}
    <td>{{same_as_contact.get_roles|seq_to_dash|default:'&nbsp;'}}</td>
    <td>{{same_as_contact.get_phones|seq_to_br}}</td>
    <td><a href="mailto:{{same_as_contact.get_email}}">{{same_as_contact.get_email}}</a></td>
    <td>
      <a class="colorbox-form" href="{% url "crm_remove_same_as" contact.id same_as_contact.id %}">
        <span class="glyphicon glyphicon-remove"></span>
      </a>
    </td>
  </tr>
  {% endspaceless %}
  {% endwith %}
  {% endfor %}
{% endif %}

{% with contact.get_relationships as relationships %}
{% if relationships %}
  {% for relationship in relationships %}
  {% with relationship.contact.has_left as has_left %}
  <tr class="relationship {% if relationship.contact.main_contact %}main-contact{% endif %} {% if has_left %}old-contact{% endif %}">
    <td>{{relationship.type_name}}</td>
    {% if relationship.contact.entity.is_single_contact %}
    <td colspan="2"><a href="{% url "crm_view_contact" relationship.contact.id %}">{{relationship.contact.fullname}}</a></td>
    {% else %}
    <td>
      <a href="{% url "crm_view_contact" relationship.contact.id %}">
        {{relationship.contact.fullname}}
      </a>
      {% if has_left %}<span class="label label-warning ut-has-left">{% trans "has left" %}</span>{% endif %}
    </td>
    <td><a href="{% url "crm_view_entity" relationship.contact.entity.id %}">{{ relationship.contact.entity }}</a></td>
    {% endif %}
    <td>{{relationship.contact.get_roles|seq_to_dash|default:'&nbsp;'}}</td>
    <td>{{relationship.contact.get_phones|seq_to_br}}</td>
    <td><a href="mailto:{{relationship.contact.get_email}}">{{relationship.contact.get_email}}</a></td>
    <td><a class="colorbox-form" href="{% url "crm_delete_relationship" contact.id relationship.id %}">
      <span class="glyphicon glyphicon-remove"></span>
    </a></td>
  </tr>
  {% endwith %}
  {% endfor %}
{% endif %}
{% endwith %}
</table>
{% endblock %}

{% block section_menu_name %}<span class="glyphicon glyphicon-pencil"></span> {% trans "Edit" %}{% endblock %}

{% block section_buttons %}
  <li>
    <a class="colorbox-form" href="{% url "crm_same_as" contact.id %}">{% trans "Same As" %}</a>
  </li>
  {% if contact.can_add_relation %}
    <li><a class="colorbox-form add-relation" href="{% url "crm_add_relationship" contact.id %}">{% trans "Add relationship" %}</a><li>
  {% endif %}
{% endblock %}
