{% extends "package/resource_edit_base.html" %}

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

{% set acl = c.acl_dict %}

{% block primary_content_inner %}
  {% link_for _('Back to all resource acl'), controller='ckanext.resourceauthorizer.controller:ResourceAuthorizerController', action='resource_acl', dataset_id=dataset_id, resource_id=resource_id, class_='btn btn-default pull-right', icon='arrow-left' %}
  <h1 class="page-heading">
    {% block page_heading %}{{ _('Edit Resource Acl') if acl else _('Add Resource Acl') }}{% endblock %}
  </h1>
  {% block form %}
  <form class="dataset-form add-member-form" method='post' id="add-member-form">
    <div class="row-fluid">
      {% if not acl %}
        <div class="control-group control-medium">
          <label class="control-label" for="username">
            {{ _('User') }}
          </label>
          <span>
            {{ _('If you wish to add an existing user, search for their username below.') }}
          </span>
          <div class="controls">
            <input id="username" type="text" name="username" placeholder="Username"
            value="" class="form-control control-medium" data-module="autocomplete"
            data-module-source="/api/2/util/user/autocomplete?q=?">
          </div>
        </div>
        <div class="add-member-or">
          {{ _('or') }}
        </div>
        <div class="control-group control-medium">
          <label class="control-label" for="orgname">
            {{ _('Organization') }}
          </label>
          <span>
            {{ _('If you wish to add an existing organization, search for their organization below.') }}
          </span>
          <div class="controls">
            <input id="organization" type="text" name="organization" placeholder="Organization"
            value="" class="form-control control-medium" data-module="autocomplete"
            data-module-source="/api/2/util/organization/autocomplete?q=?">
          </div>
        </div>
      {% else %}
        {% if acl['auth_type'] == 'user' %}
          <div class="control-group control-medium">
            <label class="control-label" for="username">
              {{ _('User') }}
            </label>
            <div class="controls">
              <input type="hidden" name="username" value="{{ acl['auth_id'] }}" />
              <input id="username" name="username" type="text" value="{{ c.auth['name'] }}"
                disabled="True" class="form-control control-medium">  
            </div>
          </div>
        {% else %}
          <div class="control-group control-medium">
            <label class="control-label" for="username">
              {{ _('Organization') }}
            </label>
            <div class="controls">
              <input type="hidden" name="organization" value="{{ acl['auth_id'] }}" />
              <input id="organization" name="organization" type="text" value="{{ c.auth['name'] }}"
                disabled="True" class="form-control control-medium">  
            </div>
          </div>
        {% endif %}
      {% endif %}
    </div>
    {% set format_attrs = {'data-module': 'autocomplete'} %}
    {{ form.select('permission', label=_('Permission'), options=c.permissions, selected=c.acl_permission, error='', attrs=format_attrs) }}
    <div class="form-actions">
      {% if acl %}
        <a href="{% url_for controller='ckanext.resourceauthorizer.controller:ResourceAuthorizerController', action='resource_acl_delete', id=acl.id, dataset_id=dataset_id, resource_id=resource_id %}" class="btn btn-danger pull-left" data-module="confirm-action" data-module-content="{{ _('Are you sure you want to delete this acl?') }}">{{ _('Delete') }}</a>
        <button class="btn btn-primary" type="submit" name="submit" >
          {{ _('Save') }}
        </button>
      {% else %}
        <button class="btn btn-primary" type="submit" name="submit" >
          {{ _('Add Resource Acl') }}
        </button>
      {% endif %}
    </div>
  </form>
  {% endblock %}
{% endblock %}

{% block secondary_content %}
  {{ super() }}
  <div class="module module-narrow module-shallow">
    <h2 class="module-heading">
      <i class="fa fa-lg fa-info-circle"></i>
      {{ _('What are permissions?') }}
    </h2>
    <div class="module-content">
      {% trans %}
         <p><strong>None:</strong> Cannot access the resource</p>
         <p><strong>Read:</strong> Can access the resource</p>
      {% endtrans %}
    </div>
  </div>
{% endblock %}