{% extends "docbow/base_user.html" %}
{% load i18n %}

{% block title %}
  {% trans "Profile" %}
{% endblock %}

{% block extra_scripts %}
  {{ profile_form.media.css }}
  {{ delegate_form.media.css }}
{% endblock %}

{% block main-column %}
  <h3 id="profile">{% trans "profile_menu" %}</h3>

  {% if profile_form %}
    <h4>Notifications</h4>
    <form class=" uniForm" method="post">
      {% csrf_token %}
      {% include "docbow/errors.html" with error_form=profile_form %}

      {% if profile_form.accept_notifications %}
        {% include "docbow/field.html" with field=profile_form.accept_notifications %}
      {% endif %}

      {% if profile_form.personal_email %}
        {% include "docbow/field.html" with field=profile_form.personal_email %}
      {% endif %}

      {% if profile_form.mobile_phone %}
        <p class="formHint">{{ mobile_phone_paragraph }}</p>
        {% include "docbow/field.html" with field=profile_form.mobile_phone %}
      {% endif %}

      <div class="buttonHolder">
        <input name="profile-preferences" value="{% trans 'Notification preferences' %}" class="submit submitButton" id="submit-id-profile-preferences" onclick="document.location.href='#notifications-overlay'; return false;" type="submit">
        <input name="profile-validate" value="{% trans 'Validate' %}" class="submit submitButton" id="submit-id-profile-validate" type="submit">
      </div>
    </form>
  {% endif %}

  {% if delegate_form %}
    <h3 id="delegate">{% trans "Delegations" %}</h3>

    <h4>Délégations attribuées</h4>

    {% if delegations %}
      <form method="post" action=".">
        {% csrf_token %}
        <table id="delegations">
          <thead>
            <tr>
              <th id="firstname">Prénom</th>
              <th id="lastname">Nom</th>
              <th id="email">Courriel</th>
              <th id="username">Identifiant</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {% for delegation in delegations %}
              <tr class="{% cycle 'odd' 'even' %}">
                <td>{{ delegation.to.first_name }}</td>
                <td>{{ delegation.to.last_name }}</td>
                <td>{{ delegation.to.email }}</td>
                <td>{{ delegation.to.username }}</td>
                <td><input type="image" name="delegate-delete-{{ delegation.to.username }}" src="{{ STATIC_URL }}/docbow/css/images/poubelle.png" alt="{% trans "Delete" %}"/></td>
              </tr>
              <tr class="last-connection {% cycle 'odd' 'even' %}">
                {% if delegation.journals %}
                  <td colspan="5">Dernière(s) connexion(s): {% for connection_time in delegation.journals %}
                    {{ connection_time }}
                    {% if not forloop.last %}, {% endif %}
                  {% endfor %}</td>
                {% else %}
                  {% if delegation.to.date_joined != delegation.to.last_login %}
                    <td colspan="5">Dernière connexion: {{ delegation.to.last_login }}</td>
                  {% else %}
                    <td colspan="5">Aucune connexion.</td>
                  {% endif %}
                {% endif %}
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </form>
    {% else %}
      <p>Aucune délégation attribuée</p>
    {% endif %}
    {% if delegate_form %}
      <h4>{% trans "Create a new delegation" %}</h4>
      <div id="new-delegation-form">
        <form action="." class=" uniForm" method="post">
          {% csrf_token %}
          {% include "docbow/errors.html" with error_form=delegate_form %}
          <p>{% trans "You can delegate to a newly created user, then all following fields are mandatory:" %}</p>

          {% include "docbow/field.html" with field=delegate_form.first_name %}

          {% include "docbow/field.html" with field=delegate_form.last_name %}

          {% include "docbow/field.html" with field=delegate_form.email %}

          {% if delegate_to_existing_user %}
            <p>{% trans "or you can delegate to an existing user:" %}</p>

            {% include "docbow/field.html" with field=delegate_form.existing_user %}
          {% endif %}
          <div class="buttonHolder">
            <input name="delegate-create" value="{% trans 'Create a new delegation' %}" class="submit submitButton" id="submit-id-delegate-create" type="submit">
          </div>
        </form>
      </div>
    {% endif %}
    <h4>{% trans "Received delegations" %}</h4>
    {% if received_delegations %}
      <ul id="received-delegations">
        {% for delegation in received_delegations %}
          <li>{% firstof delegation.by.get_full_name delegation.by.username %}</li>
        {% endfor %}
      </ul>
    {% else %}
      <p>Aucune délégation reçue</p>
    {% endif %}
  {% endif %}

  {% if password_change_form %}
    <h3 id="password-change">{% trans "Changing password" %}</h3>

    <form method="post" action="." class="profile-form">
      {% csrf_token %}
      {% include "docbow/errors.html" with error_form=password_change_form %}

      {% include "docbow/field.html" with field=password_change_form.new_password1 %}

      {% include "docbow/field.html" with field=password_change_form.new_password2 %}

      {% include "docbow/field.html" with field=password_change_form.old_password %}

      <input type="submit" name="{{password_change_form.prefix}}-validate" value="{% trans 'Change Password' %}" />
    </form>
  {% endif %}

  <div class="overlay" id="notifications-overlay" href="#">
    {% include "docbow/notifications.html" with form=notifications_form %}
  </div>


  {% if email_form %}
    <h3 id="email">{% trans "Changing email" %}</h3>
    <form autocomplete="off" class=" uniForm" method="post">
      {% csrf_token %}

      {% include "docbow/field.html" with field=email_form.old_email %}

      {% include "docbow/field.html" with field=email_form.password %}

      {% include "docbow/field.html" with field=email_form.email %}

      {% include "docbow/field.html" with field=email_form.email2 %}

      <div class="buttonHolder">
        <input name="email-validate" value="{% trans 'Validate' %}" class="submit submitButton" id="submit-id-email-validate" type="submit">
      </div>
    </form>
  {% endif %}

{% endblock main-column %}

{% block endscripts %}
  {{ profile_form.media.js }}
  {{ delegate_form.media.js }}
  {{ notifications_form.media.js }}
  <script src="{{ STATIC_URL }}jquery-plugin/js/jquery.scrollTo-min.js" type="text/javascript"></script>
  <script>
    $(function () {
      if ($('div.error').length) {
        $.scrollTo('div.error');
      } else {
        $.scrollTo('#errorMsg');
      }
    });
  </script>
{% endblock endscripts %}
