{% load url from future %}
{% load apptags %}
<a class="print" href="#">Imprimer</a>
<div>
  {% for appointment in ressource_agenda.appointments %}
  <h3 class="{{ appointment.type }} {% if appointment.act_absence %}act-absence{% endif %} appointment"
      {% if appointment.act_absence %}title="{{appointment.act_absence}}"{% endif %} id="{{ appointment.event_id }}">
    <span class="hour">{{ appointment.begin_hour }}</span>
    {% if appointment.title %}<span class="title">{{ appointment.title }}</span>{% endif %}
    {% if appointment.length %}<span class="length">{{ appointment.length }} min</span> {% endif %}
    {% if appointment.act_type %}<span class="act_type">{{ appointment.act_type }}</span>{% endif %}
    <span class="participants">
      {% if appointment.len_workers > 4 %}
      {% if appointment.workers_absent %}<span class="absent" title="Au moins un intervenant est absent">{% endif %}
        {{ appointment.len_workers }} inter.
        {% if appointment.workers_absent %}</span>{% endif %}
      {% else %}
      {% if appointment.workers %}
      {% for worker in appointment.workers %}
      {% if worker in appointment.workers_absent %}<span class="absent" title="Absent">{% endif %}
        {{ worker.worker.initials }}{% if not forloop.last %} {% endif %}
        {% if worker in appointment.workers_absent %}</span>{% endif %}
      {% endfor %}
      {% endif %}
      {% endif %}
    </span>

    <span class="right">
      {% for service_name in appointment.other_services_names %}
      <span class="box {{ service_name }}" title="{{ service_name }}"></span>
      {% endfor %}
      <span title="Un commentaire existe" class="icon-comment" {% if appointment.description %}style='display: inline'{% endif %}></span>
      {% if appointment.event_id %}
      {% if appointment.is_recurrent %} R {% endif %}
      {% if service in appointment.services_names %}
      {% if appointment.patient_record_id %}
      <button title="Éditer un rendez-vous" class="edit-appointment icon-edit" data-event-id="{{ appointment.event_id }}"></button>
      {% else %}
      <button title="Éditer un événement" class="edit-event icon-edit" data-event-id="{{ appointment.event_id }}">
        {% endif %}
        <button class="remove-appointment icon-remove-sign" title="Supprimer un rendez-vous" data-url="{% url 'delete-occurrence' date=date service=service pk=appointment.event_id %}" data-rdv="{{ appointment.title }}"></button>
        {% endif %}
        {% endif %}
    </span>
  </h3>
  <div>
    {% if appointment.type == 'free' %}
    <button class='newrdv' data-url="{% url 'nouveau-rdv' service=service date=date %}" data-hour="{{ appointment.begin_hour }}" data-duration="{{ appointment.length }}">Nouveau rendez-vous patient</button>
    <button class='newevent' data-url="{% url 'new-event' service=service date=date %}" data-hour="{{ appointment.begin_hour }}" data-duration="{{ appointment.length }}">Nouvel événement</button>
    {% endif %}
    {% if appointment.event_id %}
    {% if appointment.workers %}
    <p class="workers">
      Intervenants :
      {% for worker in appointment.workers %}
      {{ worker.first_name }} <span class="lastname">{{ worker.last_name }}</span>{% if forloop.last %}.{% else %}, {% endif %}
      {% endfor %}
    </p>
    <p class="phones">
    {% if appointment.patient.mobile %}
    <span title="{{ appointment.patient.first_name }} {{ appointment.patient.last_name|upper }} (Patient - Perso)" class="icon-user-space">
    {{ appointment.patient.mobile }}
    </span>
    {% endif %}
    {% if appointment.patient.phone %}
    <span title="{{ appointment.patient.first_name }} {{ appointment.patient.last_name|upper }} (Patient - Pro)" class="icon-user-space">
    {{ appointment.patient.phone }}
    </span>
    {% endif %}
    {% for address in appointment.patient.addresses.all %}
      {% if address.place_of_life %}
        {% if address.phone %}<span title="{{ address.display_name }}" class="icon-home-space">{{ address.phone }}</span>{% endif %}
        {% for contact in address.patientcontact_set.all  %}
          {% if contact.id != appointment.patient.id %}
          {% if contact.mobile %}<span title="{{ contact.first_name }} {{ contact.last_name|upper }} (Perso)" class="icon-user-space">{{ contact.mobile }}</span>{% endif %}
          {% if contact.phone %}<span title="{{ contact.first_name }} {{ contact.last_name|upper }} (Pro)" class="icon-user-space">{{ contact.phone }}</span>{% endif %}
          {% endif %}
        {% endfor %}
      {% endif %}
    {% endfor %}
    </p>
    {% endif %}
    {% endif %}

    {% if appointment.patient_record_id %}
    <a href="/{{ service }}/dossiers/{{ appointment.patient_record_id }}/view" target="_blank">Dossier patient</a> -
    <a href="/{{ service }}/dossiers/{{ appointment.patient_record_id }}/view#tab=5" target="_blank">Prochains rendez-vous</a> -
    <a href="#" class="generate-mail-btn" data-dossier-id="{{ appointment.patient_record_id }}" data-date="{{date|date:"Y-m-d"}}" data-event-id="{{ appointment.event_id }}" data-next-url="{{ request.get_full_path }}">Courrier</a>
    {% endif %}

    {% if appointment.validation %}
      <div><span>{% if appointment.validation.1 %}<strong>{{ appointment.validation.2 }}</strong> {% if appointment.validation.0.is_billed %}<strong>Acte facturé</strong>{% endif %}
        {% else %}
          Non pointé.
        {% endif %}
        </span>
       </div>
    {% endif %}

    {% if not appointment.validation.0.validation_locked and appointment.validation.3 %}
    <form method="post" class="inline-form">
      {% csrf_token %}
      <input type="hidden" value="{{appointment.validation.0.id}}" name="acte-id">
      <select data-previous="{{ last_status.state_name }}" name="act_state">
        {% for state_name, display_state_name in appointment.validation.3.items %}
        <option value="{{ state_name }}" {% if state_name == appointment.validation.1.state_name %}selected{% endif %}>{{ display_state_name }}</option>
        {% endfor %}
      </select>
      <button>Pointer</button>
    </form>
    {% endif %}

    {% if service in appointment.services_names %}
    <div class="tabs-ressource-{{ ressource_agenda.ressource.id }} textedit">
      <span></span>
      <textarea>{{ appointment.description }}</textarea>
      <button disabled="disabled" data-event-id="{{ appointment.event_id }}" data-date="{{ date|date:"Y-m-d" }}">✔</button>
    </div>
    {% else %}
    <p>{{ appointment.description }}</p>
    {% endif %}
  </div>
  {% endfor %}
</div>
</div>
