{% extends "base.html" %}

{% block menu %}
{% endblock %}

{% block content %}
  {% if training.modalità == "edit" or training.modalità == "add" %}
  <form method="post">
    <div class="container">
      <div class="form-group">
        <label for="date-picker">{{ 'Training day'|t }}</label>
        <input id="date-picker" aria-label="Date picker" placeholder="DD/MM/YYY" autocomplete="off" name="date" data-provide="datepicker"
          value="{{ values.date }}" type="text" class="form-control">
      </div>
      <div class="form-group">
        <label for="name">{{ 'Name'|t }}</label>
        <input id="name" class="form-control" type="text" name="name" placeholder="1° training" required value="{{ values.name }}">
      </div>
      <div class="form-group">
        <label for="timePicker1">{{ 'Start time'|t }}</label>
        <input id="timePicker1" class="form-control" type="time" name="start_time" required value="{{ values.beginning }}">
      </div>
      <div class="form-group">
        <label for="timePicker2">{{ 'End time'|t }}</label>
        <input id="timePicker2" class="form-control" type="time" name="end_time" required value="{{ values.end }}">
      </div>
      <div class="form-group">
        <label>{{ 'Chief'|t }}</label>
        <br>
        {% for user in training.crew %}
        {% if not user.hidden %}
        <div class="form-check">
          <input aria-label="{{ username(user.id) }}" id="chief-{{ user.id }}" class="form-check-input chief chief-{{ user.id }}"
            style="transform: scale(1.4); -webkit-transform: scale(1.4);" type="checkbox" name='chief[]'
            value='{{ user.id }}'>
          <label class="form-check-label" for="chief-{{ user.id }}">
            {{ username(user.id) }}
          </label>
        </div>
        {% endif %}
        {% endfor %}
      </div>
      <script{% if enable_js_nonce %} nonce="{{ nonce }}"{% endif %}>
        $('.chief').on('change', function () {
          $('input[name="' + this.name + '"]').not(this).prop('checked', false);
        });
      </script>
      <div class="form-group">
        <label>{{ 'Crew'|t }}</label>
        <br>
        {% for user in training.crew %}
        {% if not user.hidden %}
        <div class="form-check">
          <input aria-label="{{ username(user.id) }}" id="crew-{{ user.id }}" class="form-check-input crew crew-{{ user.id }}"
            style="transform: scale(1.4); -webkit-transform: scale(1.4);" type="checkbox" name='crew[]'
            value='{{ user.id }}'>
          <label class="form-check-label" for="crew-{{ user.id }}">
            {{ username(user.id) }}
          </label>
        </div>
        {% endif %}
        {% endfor %}
      </div>
      {% if option('use_location_picker') %}
      <label>{{ 'Training place'|t }}</label>
      <div id="map"></div>
      <div id="search">
        <div class="form-inline">
          <div class="form-group mx-sm-3 mb-2">
            <input type="text" class="form-control" name="addr" value="" id="addr" size="50" />
          </div>
          <div class="form-group mx-sm-3 mb-2">
            <button id="search_button" type="button" class="btn btn-primary mb-2">
                {{ 'Search'|t }}
            </button>
            <script{% if enable_js_nonce %} nonce="{{ nonce }}"{% endif %}>
            $("#search_button").on("click",function(){
                allertaJS.maps.addrSearch('{{ 'Search results'|t }}', '{{ 'No results found'|t }}');
            });
            </script>
          </div>
        </div>
        <div id="results"></div>
      </div>
      <input type="hidden" name="place" value="" />
      <script{% if enable_js_nonce %} nonce="{{ nonce }}"{% endif %}>window.loadMapOnScriptLoad = true;</script>
      {{ script('maps.js') }}
      {% else %}
      <div class="form-group">
        <label for="place">{{ 'Training place'|t }}</label>
        <input id="place" type="text" class="form-control" name="place" required value="{{ values.place }}">
      </div>
      <br>
      {% endif %}
      <div class="form-group">
        <label for="notes">{{ 'Others notes (ex. others infos)'|t }}</label><br>
        <textarea class="form-control" id="notes" name='notes'></textarea>
      </div>
      <br>
      <input id="modalità" type="hidden" value="{{ training.modalità }}" name="mod"></input>
      <input id="id" type="hidden" value="{{ training.id }}" name="id"></input>
      <input id="token" type="hidden" value="{{ training.token }}" name="token"></input>
      <button id="submit_button" type="submit" class="btn btn-primary">{{ 'Submit'|t }}</button>
    </div>
  </form>
  <script{% if enable_js_nonce %} nonce="{{ nonce }}"{% endif %}>
    {% if training.modalità == "edit" %}
    {% if option('use_location_picker') %}
    {% set place = values.place|split('#')[0] %}
    $(function(){
      allertaJS.maps.setMarker(new L.LatLng({{place|split(';')[0]}}, {{place|split(';')[1]}}), true);
    });
    {% endif %}
    $.each('{{ values.chief|striptags|e("js") }}'.split(','), function (index, value) {
      $('.chief-' + value).prop('checked', true);
    });
    $.each('{{ values.crew|striptags|e("js") }}'.split(','), function (index, value) {
      $('.crew-' + value).prop('checked', true);
    });
    $('#notes').val('{{ values.notes|e("js") }}');
    {% endif %}
  </script>
  {% endif %}
  {% if training.modalità == "delete" %}
  <div style="margin: 0 auto; text-align: center">
    <p>{{ 'Are you sure you want to delete the training?'|t }}</p>
    <form method="post">
      <input id="modalità" type="hidden" value="delete" name="mod"></input>
      <input id="token" type="hidden" value="{{ training.token }}" name="token"></input>
      <input id="id" type="hidden" value="{{ training.id }}" name="id"></input>
      <button id="remove" type="submit">{{ 'Submit'|t }}</button>
    </form>
    <script{% if enable_js_nonce %} nonce="{{ nonce }}"{% endif %}>
      $('form').submit(function () {
        return confirm("{{ 'The action cannot be canceled. Are you sure you want to continue?'|t }}");
      });
    </script>
  </div>
  {% endif %}

{% block footer %}
{% endblock %}

{% endblock %}