{% extends "objects/forms/form_base.html" %}

{% block title %}New Object — {{ service_name }}{% endblock %}

{% block content %}
  <form class="form-horizontal" method="post" action="{% if previous_object_id %}{{ url_for('.new_object', action_id=action_id, previous_object_id=previous_object_id) }}{% else %}{{ url_for('.new_object', action_id=action_id) }}{% endif %}">
    {{ form.csrf_token() }}
    <input type="hidden" name="previous_actions" value="{{ previous_actions }}">
    {% if 'batch' in schema and schema['batch'] %}
    <strong style="font-size:1.5em; margin-bottom: 1em; display:block;">Batch Information</strong>
    <div class="form-group row{% if 'input_num_batch_objects' in errors %} has-error{% elif 'input_num_batch_objects' in form_data %} has-success{% endif %}" style="padding-right:0.75em">
      <label class="control-label col-md-3">Number of Objects in Batch</label>
      <div class="col-md-9">
        <input type="number" min="1" max="100" step="1" value="{% if 'input_num_batch_objects' in form_data %}{{ form_data['input_num_batch_objects'] }}{% else %}1{% endif %}" class="form-control" name="input_num_batch_objects" />
      </div>
    </div>
    {% endif %}
    {% set id_prefix = "object_" %}
    {% include "objects/forms/form_any.html" %}
    {% if can_copy_permissions and (config['LOAD_OBJECTS_IN_BACKGROUND'] or existing_objects) %}
    <div class="form-group clearfix" style="padding:0 0.75em">
      <div class="col-md-9 col-md-offset-3">
        <div class="input-group pull-right">
          <span class="input-group-addon" style="width:20em; height: 34px; display: inline-block">
            <label style="font-weight: 400">
              <input type="checkbox" name="copy_permissions_from_other_object" {% if 'copy_permissions_from_other_object' in request.form or ('previous_object_id' in request.args and has_grant_for_previous_object and not 'action_submit' in request.form) %}checked="checked"{% endif %} id="copyPermissionsChkBox">
              <div style="display: inline">
                <i class="fa fa-spinner fa-spin copyPermissionsLoading" style="display: none;"></i>
                <span class="sr-only copyPermissionsLoading" style="display: none;">Loading...</span>
              </div>
              Copy Permissions from other Objects
            </label>
          </span>
          {% set selected_obj_id = request.form.get('copy_permissions_object_id') %}
          {% if has_grant_for_previous_object and not 'action_submit' in request.form and request.args.get('previous_object_id') %}
          {% set selected_obj_id = request.args.get('previous_object_id') %}
          {% endif %}
          <select id="copyPermissionsSelect" class="selectpicker" data-width="280px" name="copy_permissions_object_id" data-live-search="true" data-sampledb-default-selected="{{ selected_obj_id }}" data-sampledb-required-perm="3" data-sampledb-start-show=".copyPermissionsLoading" data-sampledb-start-hide="#copyPermissionsChkBox" data-sampledb-empty-disable="#copyPermissionsChkBox, #copyPermissionsSelect" data-sampledb-stop-hide=".copyPermissionsLoading" data-sampledb-stop-show="#copyPermissionsChkBox">
            {% for object in existing_objects %}
            <option value="{{ object.id }}" {% if object.id | string == selected_obj_id %}selected="selected"{% endif %}>{{ object.data['name']['text'] }} (#{{ object.id }})</option>
            {% endfor %}
          </select>
        </div>
      </div>
    </div>
    {% endif %}
    {% if may_create_log_entry%}
    <div class="form-group clearfix" style="padding:0 0.75em">
      <div class="col-md-9 col-md-offset-3">
        <div class="input-group pull-right">
        {% if instrument_log_categories %}
          <span class="input-group-addon" style="width:20em; height: 34px; display: inline-block">
            <label style="font-weight: 400">
              <input type="checkbox" name="create_instrument_log_entry" {% if 'create_instrument_log_entry' in request.form or (create_log_entry_default and not 'action_submit' in request.form) %}checked="checked"{% endif %}>
              Create Instrument Log Entry
            </label>
          </span>
          <select class="selectpicker" multiple="multiple" data-width="280px" name="instrument_log_categories">
            {% for category in instrument_log_categories %}
            <option value="{{ category.id }}" {% if category.id | string in request.form.getlist('instrument_log_categories') %}selected="selected"{% endif %}>{{ category.title }}</option>
            {% endfor %}
          </select>
        {% else %}
          <span class="input-group-addon" style="width:20em; border: 1px solid #ccc; border-radius: 4px">
            <label style="font-weight: 400">
              <input type="checkbox" name="create_instrument_log_entry" {% if 'create_instrument_log_entry' in request.form or (create_log_entry_default and not 'action_submit' in request.form) %}checked="checked"{% endif %}>
              Create Instrument Log Entry
            </label>
          </span>
        {% endif %}
        </div>
      </div>
    </div>
    {% endif %}
    <div class="form-group row clearfix" style="padding-right:0.75em">
      <div class="col-md-9 col-md-offset-3">
        <button type="submit" name="action_submit" class="btn btn-primary pull-right" style="width:20em">Save</button>
      </div>
    </div>
  </form>
{% endblock %}
