<!DOCTYPE html>
<html>
<head>
  <link href="favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">
  <script src="{{ urlsoftware }}/resources/dist/{{ resource('main.js') }}"></script>
  {% if enable_debug_bar %}{{ debug_bar_head|raw }}{% endif %}
</head>
<body>
  {% if service.modalità == "edit" or service.modalità == "add" %}
  <form method="post">
    <div class="container">
      <label><b>{{ 'Service day'|t }}</b></label>
      <div class="input-group">
        <input aria-label="Date picker" placeholder="DD/MM/YYY" autocomplete="off" name="date" data-provide="datepicker"
          value="{{ values.date }}" type="text" class="form-control">
        <div class="input-group-addon">
          <span class="glyphicon glyphicon-th"></span>
        </div>
      </div>
      <br>
      <br>
      <label><b>{{ 'code'|t }}</b></label>
      <input id="progressivo" type="text" name="code" placeholder="1234/5" required value="{{ values.code }}">
      <br>
      <br>
      <label for="timePicker1"><b>{{ 'Start time'|t }}</b></label>
      <input id="timePicker1" type="time" name="beginning" required value="{{ values.beginning }}">
      <br>
      <br>
      <label for="timePicker2"><b>{{ 'End time'|t }}</b></label>
      <input id="timePicker2" type="time" name="end" required value="{{ values.end }}">
      <br>
      <br>
      <label><b>{{ 'Chief'|t }}</b></label>
      <br>
      {% for user in service.crew %}
      <div {{ user.name == "test" ? "style='display: none'" : "" }} class="form-check">
        <input aria-label="{{ username(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>
      {% endfor %}
      <script>
        $('.chief').on('change', function () {
          $('input[name="' + this.name + '"]').not(this).prop('checked', false);
        });
      </script>
      <br>
      <label><b>{{ 'Drivers'|t }}</b></label>
      <br>
      {% for user in service.crew %}
      {% if user.driver == 1 %}
      <div {{ user.name == "test" ? "style='display: none'" : "" }} class="form-check">
        <input aria-label="{{ username(user.id) }}" class="form-check-input drivers drivers-{{ user.id }}"
          style="transform: scale(1.4); -webkit-transform: scale(1.4);" type="checkbox" name='drivers[]'
          value='{{ user.id }}'>
        <label class="form-check-label" for="drivers-{{ user.id }}">
          {{ username(user.id) }}
        </label>
      </div>
      {% endif %}
      {% endfor %}
      <br>
      <label><b>{{ 'Crew'|t }}</b></label>
      <br>
      {% for user in service.crew %}
      <div {{ user.name == "test" ? "style='display: none'" : "" }} class="form-check">
        <input aria-label="{{ username(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>
      {% endfor %}
      <br>
      {% if option('use_location_picker') %}
      <label><b>{{ 'Service place'|t }}</b></label>
      <div id="map"></div>
      <div id="search">
        <input type="text" name="addr" value="" id="addr" size="10" />
        <button type="button" onclick="addrSearch('{{ 'Search results'|t }}', '{{ 'No results found'|t }}');"
          class="btn btn-primary">{{ 'Search'|t }}</button>
        <div id="results"></div>
      </div>
      <input type="hidden" name="place" value="" />
      <script src="{{ urlsoftware }}/resources/dist/{{ resource('maps.js') }}" onload="loadMap();"></script>
      {% else %}
      <label><b>{{ 'Service place'|t }}</b></label>
      <input type="text" name="place" required value="{{ values.place }}">
      {% endif %}
      <br>
      <br>
      <label><b>{{ 'Others notes (ex. others infos)'|t }}</b></label><br>
      <textarea name='notes' id='notes'></textarea>
      <br>
      <br>
      <label><b>{{ 'Service type'|t }}</b></label>
      <br>
      <select name='type' class="types">
        {% for type in service.types %}
        <option value='{{ type.name }}'>{{ type.name }}</option>
        {% endfor %}
        {% if service.types is empty %}
        <option id="empty_option" value=''></option>
        {% endif %}
        <option value='add_new'>{{ 'Add type...'|t }}</option>
      </select>
      <br>
      <input id="modalità" type="hidden" value="{{ service.modalità }}" name="mod"></input>
      <input id="token" type="hidden" value="{{ service.token }}" name="token"></input>
      <input id="id" type="hidden" value="{{ service.id }}" name="id"></input>
      <button type="submit">{{ 'Submit'|t }}</button>
    </div>
  </form>
  <script>
    $( ".types" ).change(function() {
        $('#empty_option').remove();
        var type = "";
        $( ".types option:selected" ).each(function() {
            type = $( this ).val();
        });
        console.log(type);
        if(type == "add_new"){
            new_type = prompt("{{ 'Insert type name:'|t }}");
            if(!new_type) return;
            $.ajax({
                url: "resources/ajax/ajax_add_type.php",
                method: "POST",
                data: {
                    type: new_type
                },
                success: function (data) {
                    $('option:last-of-type').before($('<option>').val(new_type).text(new_type)); 
                    $('option[value='+new_type+']').prop('selected', 'selected').change();
                    toastr.success(`{{ "Type '%s' added successfully."|t|format('${new_type}') }}`);
                }
            });
        }
    });
    {% if service.modalità == "edit" %}
    $.each('{{ values.chief }}'.split(','), function (index, value) {
      $('.chief-' + value).prop('checked', true);
    });
    $.each('{{ values.drivers }}'.split(','), function (index, value) {
      $('.drivers-' + value).prop('checked', true);
    });
    $.each('{{ values.crew }}'.split(','), function (index, value) {
      $('.crew-' + value).prop('checked', true);
    });
    $(".tipi").val('{{ values.type }}');
    $('#notes').val('{{ values.notes }}');
    {% endif %}
    $('form').submit(function () {
      var type = "";
      $( ".types option:selected" ).each(function() {
        type = $( this ).val();
      });
      if(type == "add_new" || type == ""){
        toastr.error("{{ 'You must select a valid service type!'|t }}");
        return false;
      }
      var progressivo_valido = /^[0-9/]{3,6}$/
      var progressivo = $('#progressivo').val();
      if (!progressivo_valido.test(progressivo)) {
        var invia_progressivo = confirm("{{ 'The code has been detected as incorrect, do you want to add the service anyway?'|t }}");
      } else {
        var invia_progressivo = true;
      }
      return invia_progressivo;
    });
  </script>
  {% endif %}
  {% if service.modalità == "delete" %}
  <div style="margin: 0 auto; text-align: center">
    <p>{{ 'Are you sure you want to delete the service?'|t }}</p>
    <form method="post">
      <input id="modalità" type="hidden" value="delete" name="mod"></input>
      <input id="token" type="hidden" value="{{ service.token }}" name="token"></input>
      <input id="id" type="hidden" value="{{ service.id }}" name="id"></input>
      <input id="increment" type="hidden" value="{{ increment }}" name="increment"></input>
      <button id="remove" type="submit">{{ 'Submit'|t }}</button>
    </form>
    <script>
      $('form').submit(function () {
        return confirm("{{ 'The action cannot be canceled. Are you sure you want to continue?'|t }}");
      });
    </script>
  </div>
  {% endif %}
  {% if enable_debug_bar %}{{ debug_bar|raw }}{% endif %}
</body>
</html>