{% extends "calebasse/base.html" %}
{% load url from future %}

{% block extrascripts %}
{{ block.super }}
<script src="{{ STATIC_URL }}js/calebasse.datesel.js"></script>
<script src="{{ STATIC_URL }}js/calebasse.agenda.js"></script>
<script>
    $(function() {
        $('table#actes tr').click(function() {
            if ($(this).data('isbilled') == "False")
            {
                var id = $(this).data('id');
                var delete_button = {
                    text: "Supprimer",
                    id: "delete-btn",
                    click: function () {
                        $(dialog).dialog('close');
                        generic_ajaxform_dialog(id + '/delete', "Supprimer l'acte",
                                                '#acte-dlg', '500px', 'Oui', window.location.href);

                    }
                };
                generic_ajaxform_dialog($(this).data('id') + '/update', "Modifier l'acte",
                '#acte-dlg', '700px', 'Modifier', '.', function (dialog) {});
                } else if ($(this).data('oldbilled') == "True") {
                generic_ajaxform_dialog($(this).data('id') + '/rebill', "Refacturer cet acte de l'ancienne facturation) ?",
                '#acte-dlg', '700px', 'Refacturer', '.');
                } else {
                alert('Vous ne pouvez pas éditer un acte déjà facturé.');
            }
        });
    $('.date').datepicker();
    $('.new-act').click(function() {
        add_dialog('#acte-dlg', $(this).data('url'), 'Nouvel acte',
        '850px', 'Ajouter');
    });
    $('#search-form input[type="checkbox"]').on('change', function () {
      $('#search-form').submit();
    });

  });
</script>
{% endblock %}

{% block header %}
  {{ block.super }}
  <span>Actes - {{ service_name }}</spam>
{% endblock %}

{% block appbar %}
<div id="appbar">
  <h2>Saisie des actes</h2>
  <a href="../..">Retourner à l'accueil</a>
  <button class="new-act"
      data-url="{% url 'act-new' service=service date=date %}">
      Ajouter un acte
  </button>
</div>
{% endblock %}


{% block content %}
<div id="sidebar">
  <form method="get" id="search-form">
    <div>
      <h3>Rechercher dans les actes</h3>
      <h4>Patient</h4>
      {{ search_form.non_field_errors }}
      <label>Nom: {{ search_form.last_name }}</label>
      {{ search_form.last_name.errors }}
      <label>Numéro de dossier: {{ search_form.patient_record_id }}</label>
      {{ search_form.patient_record_id.errors }}
      <label>Numéro de sécu: {{ search_form.social_security_number }}</label>
      {{ search_form.social_security_number.errors }}
      <button>Rechercher</button>
      <h4>Intervenant</h4>
      <label>Nom: {{ search_form.doctor_name }}</label>
      {{ search_form.doctor_name.errors }}
      <button>Rechercher</button>
    </div>
    <div id="filtre">
      <h3>Filtrer les actes</h3>
      {{ search_form.filters }}
    </div>
  </form>
</div>

<div class="content">
  {% include 'calebasse/datesel.html' %}
  <br/>

  <table id="actes" class="main">
    <thead>
    <tr>
      <th>Dossier</th>
      <th>État</th>
      <th>Type d'acte</th>
      <th>Facturé</th>
      <th>Facturable</th>
      <th>Perdu</th>
      <th>Pause</th>
      <th>Intervenants</th>
      <th>Heure</th>
      <th>Durée</th>
    </tr>
    </thead>
    <tbody>
      {% for act in object_list %}
      <tr data-id="{{ act.id }}" data-isbilled="{{ act.is_billed }}"
      {% if not act.is_billed %}class="non-factu"{% else %}{% if not act.invoice_set.all.0.number %}data-oldbilled="True"{% endif %}{% endif %}>
        <td>{{ act.patient.paper_id|default_if_none:'' }} <span class="lightgray">({{ act.patient.id }})</span>
            {{ act.patient.first_name }}
            <span class="lastname">{{ act.patient.last_name }}</span>
        </td>
        <td>{{ act.get_state }}</td>
        <td>{{ act.act_type }}</td>
        <td>{% if act.is_billed %}{% if act.invoice_set.all %}{{ act.invoice_set.all.0.number }}{% else %}Ancienne facturation{% endif %}{% endif %}</td>
        <td>{{ act.is_billable|yesno:"Oui,Non," }}{% if act.switch_billable %} (I){% endif %}</td>
        <td>{{ act.is_lost|yesno:"Oui,Non," }}</td>
        <td>{{ act.pause|yesno:"Oui,Non," }}</td>
        <td>
            {% for doctor in act.doctors.all %}
            {{ doctor }}
            {% if not forloop.last %}
            <br/>
            {% endif %}
            {% endfor %}
        </td>
        <td>{{ act.time|date:"H:i" }}</td>
        <td>{{ act.duration }}</td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
</div>
{% endblock %}

{% block dialogs %}

<div id="acte-dlg" style="display: none;"></div>
{% endblock %}
