{% import 'macros/form.html' as form %}

{% set selected = h.relationship_get_current_relations_list(field, data) %}
{% if not selected %}
  {% set selected = request.args[field.field_name] %}
{% endif %}

{% set selected_json = h.relationship_get_selected_json(selected) %}

{% set attrs = {
    "data-module": "relationship-autocomplete",
    "data-module-key": "name",
    "data-module-label": "title",
    "data-module-tags": "true",
    "data-module-createtags": "false",
    "data-module-source": "/api/2/util/relationships/autocomplete?incomplete=?&current_entity_id=%s&entity_type=%s&updatable_only=%s&owned_only=%s&check_sysadmin=%s&format_autocomplete_helper=%s"|format(data.get('id'),field.related_entity_type, field.get('updatable_only', false), field.get('owned_only', false), field.get('check_sysadmin', false), field.get('format_autocomplete_helper')),
    "data-module-selected": selected_json,
} %}

{% call form.input(
    field.field_name,
    id='field-' + field.field_name,
    label=h.scheming_language_text(field.label),
    placeholder=h.scheming_language_text(field.form_placeholder),
    value=selected|join(","),
    error=errors[field.field_name],
    attrs=dict(attrs, **(field.get('form_attrs', {}))),
    is_required=h.scheming_field_required(field)
    ) %}
  {% snippet 'scheming/form_snippets/help_text.html', field=field %}
{% endcall %}
