{% extends "base.html" %}

{% block content %}
<br>
<div>
  <div id="schedulesModal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document" style="overflow-x: auto;">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">{{ 'Edit availability schedules'|t }}</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="{{ 'Close'|t }}">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body" style="overflow-x: auto;">
        </div>
        <div class="modal-footer">
          <button id="submit_schedules_change" type="button" class="btn btn-primary">{{ 'Save changes'|t }}</button>
          <button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Close'|t }}</button>
        </div>
      </div>
    </div>
  </div>
  <script>
    $('#schedulesModal').on('show.bs.modal', function (event) {
      //$( ".modal-body" ).loading("show");
      $(".modal-body").load("modal_availability_schedule.php", {
        orientation: window.innerHeight > window.innerWidth ? "portrait" : "landscape"
      }, function() {
        init_modal();
        //$( ".modal-body" ).loading("hide");
        return true;
      });
    });
  </script>
</div>
<div class="text-center">
  <p>{{ 'Are you available in case of alert?'|t }}</p>
  <button class="btn btn-lg btn-success" onclick="activate('{{ user.id }}')">{{ 'Activate'|t }}</button>
  <button class="btn btn-lg btn-danger" style="background-color: red"
    onclick="deactivate('{{ user.id }}')">{{ 'Deactivate'|t }}</button>
  <br>
  <button type="button" class="btn btn-lg" data-toggle="modal" data-target="#schedulesModal">
    {{ 'Edit availability schedules'|t }}
  </button>
</div>
<br>
<br>
<script>
  function generate_alert_string(id){
    if(id == '{{ user.id }}'){
      return '{{ "your availability"|t }}';
    } else {
      username = document.querySelector("#username-"+id).innerText;
      return `{{ "%s availability"|t|format('${username}') }}`;
    }
  }

  function activate(id) {
    $.ajax({
      url: "resources/ajax/ajax_change_availability.php",
      method: "POST",
      data: {
        change_id: id,
        dispo: 1,
        token_list: "{{ token_list }}"
      },
      success: function (data) {
        toastr.success(`{{ 'Thanks, %s, you have given %s in case of alert.'|t|format(user.name, '${generate_alert_string(id)}') }}`);
        clearInterval(window.loadTableInterval); //stop reloading
        loadTable({tablePage: "list", useCustomTableEngine: "default"});
      }
    });
  }

  function deactivate(id) {
    $.ajax({
      url: "resources/ajax/ajax_change_availability.php",
      method: "POST",
      data: {
        change_id: id,
        dispo: 0,
        token_list: "{{ token_list }}"
      },
      success: function (data) {
        toastr.success(`{{ 'Thanks, %s, you have removed %s in case of alert.'|t|format(user.name, '${generate_alert_string(id)}') }}`);
        clearInterval(window.loadTableInterval); //stop reloading
        loadTable({tablePage: "list", useCustomTableEngine: "default"});
      }
    });
  }
</script>
<img alt="VVF" src="./resources/images/owner.png" width="150"
  style="display: block; margin-left: auto; margin-right: auto;">
<br>
<br>
<div id="list" class="table-responsive">
  <table id="table" class="table table-striped table-bordered dt-responsive nowrap">
    <thead>
      <tr>
        <th>{{ 'Name'|t }}</th>
        <th>{{ 'Available'|t }}</th>
        {% if user.full_viewer %}
        <th>{{ 'Driver'|t }}</th>
        <th>{{ 'Call'|t }}</th>
        <th>{{ 'Write'|t }}</th>
        <th>{{ 'Services'|t }}</th>
        <th>{{ 'Availability Minutes'|t }}</th>
        <!-- <th>{{ 'Other'|t }}</th> TODO: fix "Other" page -->
        {% endif %}
      </tr>
    </thead>
    <tbody id="table_body">
    </tbody>
  </table>
  <script>
    loadTable({tablePage: "list", useCustomTableEngine: "default"});
  </script>
</div>

<br><br>
<p style="text-align: center;">
  <a class='pjax_disable' id='add' href="edit_user.php?add">{{ 'Add user'|t }}</a>
</p>
<br>
<br>
{% endblock %}