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

<form class="dataset-form form-horizontal" method="post" data-module="basic-form" enctype="multipart/form-data">
  {% block error_summary %}
    {{ form.errors(error_summary) }}
  {% endblock %}

  {% block basic_fields %}
    {% set attrs = {'data-module': 'slug-preview-target'} %}
    {{ form.input('title', label=_('Name'), id='field-name', placeholder=_('My Organization'), value=data.title, error=errors.title, classes=['control-full'], attrs=attrs) }}

    {# Perhaps these should be moved into the controller? #}
    {% set prefix = h.url_for(controller='organization', action='read', id='') %}
    {% set domain = h.url_for(controller='organization', action='read', id='', qualified=true) %}
    {% set domain = domain|replace("http://", "")|replace("https://", "") %}
    {% set attrs = {'data-module': 'slug-preview-slug', 'data-module-prefix': domain, 'data-module-placeholder': '<organization>'} %}

    {{ form.prepend('name', label=_('URL'), prepend=prefix, id='field-url', placeholder=_('my-organization'), value=data.name, error=errors.name, attrs=attrs, is_required=true) }}

    {{ form.markdown('description', label=_('Description'), id='field-description', placeholder=_('A little information about my organization...'), value=data.description, error=errors.description) }}

    {% set is_upload = data.image_url and not data.image_url.startswith('http') %}
    {% set is_url = data.image_url and data.image_url.startswith('http') %}

    {{ form.image_upload(data, errors, is_upload_enabled=h.uploads_enabled(), is_url=is_url, is_upload=is_upload) }}

  {% endblock %}

  {% block custom_fields %}
    {% for extra in data.extras %}
      {% set prefix = 'extras__%d__' % loop.index0 %}
      {{ form.custom(
        names=(prefix ~ 'key', prefix ~ 'value', prefix ~ 'deleted'),
        id='field-extras-%d' % loop.index,
        label=_('Custom Field'),
        values=(extra.key, extra.value, extra.deleted),
        error=errors[prefix ~ 'key'] or errors[prefix ~ 'value']
      ) }}
    {% endfor %}

    {# Add a max if 3 empty columns #}
    {% for extra in range(data.extras|count, 3) %}
      {% set index = (loop.index0 + data.extras|count) %}
      {% set prefix = 'extras__%d__' % index %}
      {{ form.custom(
        names=(prefix ~ 'key', prefix ~ 'value', prefix ~ 'deleted'),
        id='field-extras-%d' % index,
        label=_('Custom Field'),
        values=(extra.key, extra.value, extra.deleted),
        error=errors[prefix ~ 'key'] or errors[prefix ~ 'value']
      ) }}
    {% endfor %}
  {% endblock %}

  {{ form.required_message() }}

  <div class="form-actions">
    {% block delete_button %}
      {% if h.check_access('organization_delete', {'id': data.id})  %}
        {% set locale = h.dump_json({'content': _('Are you sure you want to delete this Organization? This will delete all the public and private datasets belonging to this organization.')}) %}
        <a class="btn btn-danger pull-left" href="{% url_for controller='organization', action='delete', id=data.id %}" data-module="confirm-action" data-module-i18n="{{ locale }}">{% block delete_button_text %}{{ _('Delete') }}{% endblock %}</a>
      {% endif %}
    {% endblock %}
    <button class="btn btn-primary" name="save" type="submit">{% block save_text %}{{ _('Save Organization') }}{% endblock %}</button>
  </div>
</form>
