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

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

  {% block basic_fields %}
    {% set attrs = {'data-module': 'slug-preview-target'} %}
    {{ form.input('title', label=_('Title'), id='field-title', 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) }}

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

    {{ form.input('image_url', label=_('Image URL'), id='field-image-url', type='url', placeholder=_('http://example.com/my-image.jpg'), value=data.image_url, error=errors.image_url, classes=['control-full']) }}

  {% 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 %}

  {#
  {% block dataset_fields %}
    {% if data.packages %}
      <div class="control-group">
        <label class="control-label">{{ _('Datasets') }}</label>
        <div class="controls">
          {% for dataset in data.packages %}
            <label class="checkbox" for="field-dataset-{{ loop.index0 }}">
              <input id="field-dataset-{{ loop.index0 }}" type="checkbox" name="packages__{{ loop.index0 }}__name" value="{{ dataset.name }}" checked="checked" />
              {{ dataset.title }}
            </label>
          {% endfor %}
        </div>
      </div>
    {% endif %}

    {% set dataset_name = 'packages__%s__name' % data.packages|length %}
    {% set dataset_attrs = {'data-module': 'autocomplete', 'data-module-source': '/dataset/autocomplete?q=?'} %}
    {{ form.input(dataset_name, label=_('Add Dataset'), id="field-dataset", value=data[dataset_name], classes=['control-medium'], attrs=dataset_attrs) }}
  {% endblock %}
  #}

  <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>
