{% if data is not none and "object_id" in data %}
  {% if ("component_uuid" in data and data.component_uuid is not none and data.component_uuid != config['FEDERATION_UUID']) %}
    {% set component = get_component_by_uuid(data.component_uuid) %}
    {% if component is none %}
      {% set component_name = _('Unknown database (%(uuid)s)', uuid=data.component_uuid[:8]) %}
    {% else %}
      {% set component_name = component.get_name() %}
    {% endif %}
    {% set current_value_text = _('Object') + ' #' + (data.object_id | string) + ' @ ' + component_name %}
    {% set current_value_data_icon = 'fa fa-share-alt' %}
    {% set current_value_is_fed = true %}
  {% elif "eln_source_url" in data and data.eln_source_url is not none %}
    {% set current_value_text = _('Object') + ' #' + (data.object_id | string) + ' @ ' + (data.eln_source_url | string) %}
    {% set current_value_is_fed = false %}
  {% else %}
    {% set current_value_text = none %}
    {% set current_value_is_fed = false %}
  {% endif %}
{% else %}
  {% set current_value_text = none %}
  {% set current_value_is_fed = false %}
{% endif %}

{% if form_data.get(id_prefix+'_oid') %}
  {% set selected_id = form_data[id_prefix+'_oid'] %}
{% elif data is not none and "object_id" in data %}
  {% if current_value_text is not none %}
    {% set selected_id = "-1" %}
  {% else %}
    {% set selected_id = data.object_id | string %}
  {% endif %}
{% else %}
  {% set selected_id = none %}
{% endif %}

{% if not filter_operator %}
  {% set filter_operator = 'and' %}
{% endif %}
{% if schema_action_ids is none and schema_action_type_ids is none %}
  {% set valid_action_ids = none %}
{% else %}
  {% set valid_action_ids = [] %}
  {% for action_id in action_type_id_by_action_id %}
    {% if filter_operator == 'or' and ((schema_action_ids is none or action_id in schema_action_ids) or (schema_action_type_ids is none or action_type_id_by_action_id[action_id] in schema_action_type_ids)) %}
      {% do valid_action_ids.append(action_id) %}
    {% elif (schema_action_ids is none or action_id in schema_action_ids) and (schema_action_type_ids is none or action_type_id_by_action_id[action_id] in schema_action_type_ids) %}
      {% do valid_action_ids.append(action_id) %}
    {% endif %}
  {% endfor %}
  {% if valid_action_ids | length == 0 %}
    {% do valid_action_ids.append(-100) %}
  {% endif %}
{% endif %}

{% if config['USE_TYPEAHEAD_FOR_OBJECTS'] %}
  <div class="{% if is_template %} template-typeahead-container {% else %} objectpicker-container {% endif %}">
    <input type="text" class="form-control {% if not is_template %} typeahead {% else %} template-typeahead {% endif %}" name="typeahead_text_{{ id_prefix }}" style="width: 100%" value="{% if selected_id is not none %}#{{ selected_id }}{% endif %}" placeholder="{{ _('No object selected') }}" data-sampledb-valid-action-ids="{% if valid_action_ids is none %}[]{% else %}{{ valid_action_ids | tojson }}{% endif %}" data-sampledb-default-selected="{{ selected_id }}" data-sampledb-required-perm="1" {% if current_value_text %}data-sampledb-current-value-text="{{ current_value_text }}"{% endif %} {% if current_value_is_fed %}data-sampledb-current-value-is-fed="true"{% endif %}/>
    <span class="objectpicker-caret"></span>
    <input type="hidden" name="{{ id_prefix+'_oid' }}" value="{% if selected_id is not none %}{{ selected_id }}{% endif %}" />
  </div>
{% else %}
  <select {% if is_template %} class="template-select" data-template-class="selectpicker {% if valid_action_ids is none or valid_action_ids | length > 1 %}objectpicker{% endif %} form-control selectpicker" {% else %} class="selectpicker {% if valid_action_ids is none or valid_action_ids | length > 1 %}objectpicker{% endif %} form-control selectpicker" {% endif %} name="{{ id_prefix }}_oid" data-none-selected-text="&mdash;" data-hide-disabled="true" data-live-search="true" data-sampledb-valid-action-ids="{% if valid_action_ids is none %}[]{% else %}{{ valid_action_ids | tojson }}{% endif %}" data-sampledb-default-selected="{{ selected_id }}" data-sampledb-required-perm="1">
    <option value="">&mdash;</option>
  {% if selected_id == "-1" and current_value_text is not none %}
    <option value="-1" selected="selected" {% if current_value_is_fed %}data-icon="fa fa-share-alt"{% endif %}>{{ current_value_text }}</option>
  {% endif %}
  {% if selected_id and current_value_text is none %}
    {% set obj = get_object_if_current_user_has_read_permissions(selected_id | int) %}
    {% if (obj is not none) and (valid_action_ids is none or obj.action_id in valid_action_ids) %}
      <option {% if obj.component_id is not none %}data-icon="fa fa-share-alt"{% elif obj.eln_object_id is not none %}data-icon="fa fa-file-archive-o"{% endif %} value="{{ obj.object_id }}" {% if obj.action_id is not none %}data-action-id="{{ obj.action_id }}" {% endif %}{% if selected_id == obj.object_id | string %}selected="selected"{% endif %} {% if obj.data is not none and 'tags' in obj.data and obj.data.tags._type == "tags" and obj.data.tags.tags %}data-tokens="{% for tag in obj.data.tags.tags %}#{{ tag }} {% endfor %}{{ obj.name | get_translated_text }}"{% endif %}>{% if obj.name | get_translated_text == '' %}&mdash;{% else %}{{ obj.name | get_translated_text }}{% endif %} (#{{ obj.object_id }}{% if obj.component_id is not none %}, #{{ obj.fed_object_id }} @ {{ obj.component.get_name() }}{% endif %})</option>
    {% endif %}
  {% endif %}
  </select>
  {% if valid_action_ids is none or valid_action_ids | length > 1 %}
    <select {% if is_template %} class="template-select" data-template-class="selectpicker objectpicker-actionpicker" {% else %} class="selectpicker objectpicker-actionpicker" {% endif %} data-live-search="true" data-width="100%">
      <option selected="selected" value="">{{ _('All Actions') }}</option>
      {% for action in sorted_actions %}
        {% if valid_action_ids is none or action.id in valid_action_ids %}
      <option value="{{ action.id }}" {% if action.component_id is not none %}data-icon="fa fa-share-alt"{% endif %}>
          {% if action.user is not none %}
            {{ action.user.name }} /
          {% endif %}
          {% if action.instrument is not none and not config['DISABLE_INSTRUMENTS'] %}
            {{ action.instrument.name | get_translated_text(default=_('Unnamed Instrument')) }} &mdash;
          {% endif %}
          {{ action.name | get_translated_text(default=_('Unnamed Action')) }} (#{{ action.id }}{% if action.component_id is not none %}, #{{ action.fed_id }} @ {{ get_component(action.component_id).get_name() }}{% endif %})
        {% endif %}
      </option>
      {% endfor %}
    </select>
  {% endif %}
{% endif %}
