{% extends "base.html" %}

{% block title %}Objects — {{ service_name }}{% endblock %}

{% block stylesheets %}
  {{ super() }}
    <link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap-select.min.css') }}" />
{% endblock %}

{% block scripts %}
  {{ super() }}
  <script src="{{ url_for('static', filename='js/bootstrap-select.min.js') }}"></script>
  {% if config['LOAD_OBJECTS_IN_BACKGROUND'] %}
    <script src="{{ url_for('static', filename='js/sampledb-load-objects.js') }}"></script>
  {% endif %}
  <script>
    $(function () {
      $('[data-toggle="tooltip"]').tooltip()
    })
  </script>
{% endblock %}

{% block content %}
  <h1>{{ instrument.name }}</h1>
  <h2>Permissions</h2>
  {% set user_may_edit = (form is not none) %}
  {% if user_may_edit %}
    <form action="{{ url_for('.update_object_permissions', object_id=object.object_id) }}" method="post">
    {{ form.csrf_token }}
  {% endif %}
  <table class="table table-striped">
    <thead>
      <tr>
        <th scope="col" style="width: 40%">Special Groups</th>
        <th scope="col" class="text-center">None</th>
        <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to view objects and their properties, files and comments.">Read <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
        <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to edit objects and their properties and add files and comments. Includes Read permissions.">Write <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
        <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to grant permissions to other users. Includes Write permissions.">Grant <i class="fa fa-question-circle" aria-hidden="true"></i></span></th></tr>
    </thead>
    <tbody>
      <tr>
        <td style="vertical-align: middle">Administrators</td>
        <td></td>
        <td></td>
        <td></td>
        {% for possible_permissions in [Permissions.GRANT] %}
          <td class="text-center" style="vertical-align: middle">
            <label for="permissions_instrument_responsible_users_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
            <input type="radio" id="permissions_instrument_responsible_users_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" checked disabled />
          </td>
        {% endfor %}
      </tr>
      {% if instrument is not none %}
      <tr>
        <td style="vertical-align: middle">Instrument Scientists</td>
        <td></td>
        <td></td>
        <td></td>
        {% for possible_permissions in [Permissions.GRANT] %}
          <td class="text-center" style="vertical-align: middle">
            <label for="permissions_instrument_responsible_users_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
            <input type="radio" id="permissions_instrument_responsible_users_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" checked disabled />
          </td>
        {% endfor %}
      </tr>
      {% endif %}
      {% if user_may_edit %}
        <tr>
          <td style="vertical-align: middle">Everyone</td>
          {% for possible_permissions in [Permissions.NONE, Permissions.READ] %}
            <td class="text-center" style="vertical-align: middle">
              <label for="permissions_public_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
              <input type="radio" name="{{ form.public_permissions.name }}" id="permissions_public_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if public_permissions == possible_permissions %}checked{% endif %} {% if not user_may_edit %}disabled{% endif %} />
            </td>
          {% endfor %}
          <td></td>
          <td></td>
        </tr>
        {% if form.project_permissions | length > 0 %}
        <tr>
          <th scope="col" style="width: 40%">Projects</th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
        </tr>
        {% endif %}
        {% for project_permissions_form in form.project_permissions %}
        {% set project_id = project_permissions_form.project_id.data | int %}
        {% set project = get_project(project_id) %}
          <tr>
            <td>
              <a href="{{ url_for('.project', project_id=project_id) }}">{{ project.name }}</a>
              {{ project_permissions_form.csrf_token }}
              <input type="hidden" name="{{ project_permissions_form.project_id.name }}" value="{{ project_id }}">
            </td>
            {% for possible_permissions in [Permissions.NONE, Permissions.READ, Permissions.WRITE, Permissions.GRANT] %}
              <td class="text-center" style="vertical-align: middle">
                <label for="permissions_project_{{ project_id }}_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
                <input type="radio" name="{{ project_permissions_form.permissions.name }}" id="permissions_project_{{ project_id }}_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if project_permissions[project_id] == possible_permissions %}checked{% endif %} />
              </td>
            {% endfor %}
          </tr>
        {% endfor %}
        {% if form.group_permissions | length > 0 %}
        <tr>
          <th scope="col" style="width: 40%">Groups</th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
        </tr>
        {% endif %}
        {% for group_permissions_form in form.group_permissions %}
        {% set group_id = group_permissions_form.group_id.data | int %}
        {% set group = get_group(group_id) %}
          <tr>
            <td>
              <a href="{{ url_for('.group', group_id=group_id) }}">{{ group.name }}</a>
              {{ group_permissions_form.csrf_token }}
              <input type="hidden" name="{{ group_permissions_form.group_id.name }}" value="{{ group_id }}">
            </td>
            {% for possible_permissions in [Permissions.NONE, Permissions.READ, Permissions.WRITE, Permissions.GRANT] %}
              <td class="text-center" style="vertical-align: middle">
                <label for="permissions_group_{{ group_id }}_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
                <input type="radio" name="{{ group_permissions_form.permissions.name }}" id="permissions_group_{{ group_id }}_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if group_permissions[group_id] == possible_permissions %}checked{% endif %} />
              </td>
            {% endfor %}
          </tr>
        {% endfor %}
        {% if form.user_permissions | length > 0 %}
        <tr>
          <th scope="col" style="width: 40%">Users</th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
        </tr>
        {% endif %}
        {% for user_permissions_form in form.user_permissions %}
          {% set user_id = user_permissions_form.user_id.data | int %}
          {% set user = get_user(user_id) %}
          {% set permissions = user_permissions.get(user_id, Permissions.NONE) %}
          <tr>
            <td>
              <a href="{{ url_for('.user_profile', user_id=user_id) }}">{{ user.name }}</a>
              {{ user_permissions_form.csrf_token }}
              <input type="hidden" name="{{ user_permissions_form.user_id.name }}" value="{{ user_id }}">
            </td>
            {% for possible_permissions in [Permissions.NONE, Permissions.READ, Permissions.WRITE, Permissions.GRANT] %}
              <td class="text-center" style="vertical-align: middle">
                <label for="permissions_user_{{ user_id }}_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
                <input type="radio" name="{{ user_permissions_form.permissions.name }}" id="permissions_user_{{ user_id }}_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if user_permissions[user_id] == possible_permissions %}checked{% endif %} />
              </td>
            {% endfor %}
          </tr>
        {% endfor %}
      {% else %}
        <tr>
          <td style="vertical-align: middle">Everyone</td>
          {% for possible_permissions in [Permissions.NONE, Permissions.READ] %}
            <td class="text-center" style="vertical-align: middle">
              <label for="permissions_public_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
              <input type="radio" name="permissions_public" id="permissions_public_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if public_permissions == possible_permissions %}checked{% endif %} {% if not user_may_edit %}disabled{% endif %} />
            </td>
          {% endfor %}
          <td></td>
          <td></td>
        </tr>
        {% if project_permissions.items() | length > 0 %}
        <tr>
          <th scope="col" style="width: 40%">Projects</th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
        </tr>
        {% endif %}
        {% for project_id, permissions in project_permissions.items() %}
          {% if project_id is not none %}
            {% set project = get_project(project_id) %}
            <tr>
              <td style="vertical-align: middle">
                <a href="{{ url_for('.project', project_id=project_id) }}">{{ project.name }}</a>
              </td>
              {% for possible_permissions in [Permissions.NONE, Permissions.READ, Permissions.WRITE, Permissions.GRANT] %}
                <td class="text-center" style="vertical-align: middle">
                  <label for="permissions_project_{{ project_id }}_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
                  <input type="radio" name="permissions_project_{{ project_id }}" id="permissions_project_{{ project_id }}_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if project_permissions[project_id] == possible_permissions %}checked{% endif %} disabled />
                </td>
              {% endfor %}
            </tr>
          {% endif %}
        {% endfor %}
        {% if group_permissions.items() | length > 0 %}
        <tr>
          <th scope="col" style="width: 40%">Groups</th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
        </tr>
        {% endif %}
        {% for group_id, permissions in group_permissions.items() %}
          {% if group_id is not none %}
            {% set group = get_group(group_id) %}
            <tr>
              <td style="vertical-align: middle">
                <a href="{{ url_for('.group', group_id=group_id) }}">{{ group.name }}</a>
              </td>
              {% for possible_permissions in [Permissions.NONE, Permissions.READ, Permissions.WRITE, Permissions.GRANT] %}
                <td class="text-center" style="vertical-align: middle">
                  <label for="permissions_group_{{ group_id }}_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
                  <input type="radio" name="permissions_group_{{ group_id }}" id="permissions_group_{{ group_id }}_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if group_permissions[group_id] == possible_permissions %}checked{% endif %} disabled />
                </td>
              {% endfor %}
            </tr>
          {% endif %}
        {% endfor %}
        {% if user_permissions.items() | length > 0 %}
        <tr>
          <th scope="col" style="width: 40%">Users</th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
          <th scope="col" class="text-center"></th>
        </tr>
        {% endif %}
        {% for user_id, permissions in user_permissions.items() %}
          {% if user_id is not none %}
            {% set user = get_user(user_id) %}
            <tr>
              <td style="vertical-align: middle">
                <a href="{{ url_for('.user_profile', user_id=user_id) }}">{{ user.name }}</a>
              </td>
              {% for possible_permissions in [Permissions.NONE, Permissions.READ, Permissions.WRITE, Permissions.GRANT] %}
                <td class="text-center" style="vertical-align: middle">
                  <label for="permissions_user_{{ user_id }}_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
                  <input type="radio" name="permissions_user_{{ user_id }}" id="permissions_user_{{ user_id }}_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if user_permissions[user_id] == possible_permissions %}checked{% endif %} disabled />
                </td>
              {% endfor %}
            </tr>
          {% endif %}
        {% endfor %}
      {% endif %}
    </tbody>
  </table>
  {% if user_may_edit %}
    <div class="container">
      <button type="submit" class="btn btn-primary col-md-offset-9 col-md-3" name="edit_user_permissions" value="edit_user_permissions">Save</button>
    </div>
    </form>
    {% if users %}
      <h3 id="add_user">Add User</h3>
      <form action="{{ url_for('.update_object_permissions', object_id=object.object_id) }}" method="post">
      {{ add_user_permissions_form.csrf_token }}
      <table class="table table-striped">
        <thead>
          <tr>
            <th scope="col" style="width: 40%">User</th>
            <th scope="col" class="text-center">None</th>
            <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to view objects and their properties, files and comments.">Read <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
            <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to edit objects and their properties and add files and comments. Includes Read permissions.">Write <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
            <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to grant permissions to other users. Includes Write permissions.">Grant <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
          </tr>
        </thead>
        <tbody>
        <tr>
          <td>
            <select class="selectpicker" name="{{ add_user_permissions_form.user_id.name }}" data-width="100%" data-live-search="true">
            {% for user in users %}
              <option value="{{ user.id }}" {% if suggested_user_id == user.id %}selected="selected"{% endif %}>{{ user.name }} (#{{ user.id }})</option>
            {% endfor %}
            </select>
          </td>
          {% for possible_permissions in [Permissions.NONE, Permissions.READ, Permissions.WRITE, Permissions.GRANT] %}
            <td class="text-center" style="vertical-align: middle">
              <label for="permissions_new_user_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
              <input type="radio" name="{{ add_user_permissions_form.permissions.name }}" id="permissions_new_user_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if possible_permissions == Permissions.READ %}checked{% endif %}  {% if possible_permissions == Permissions.NONE %}disabled{% endif %} />
            </td>
          {% endfor %}
        </tr>
        </tbody>
      </table>
      <div class="container">
        <button type="submit" class="btn btn-success col-md-offset-9 col-md-3" name="add_user_permissions" value="add_user_permissions">Add</button>
      </div>
      </form>
    {% endif %}
    {% if groups %}
      <h3>Add Group</h3>
      <form action="{{ url_for('.update_object_permissions', object_id=object.object_id) }}" method="post">
      {{ add_group_permissions_form.csrf_token }}
      <table class="table table-striped">
        <thead>
          <tr>
            <th scope="col" style="width: 40%">Group</th>
            <th scope="col" class="text-center">None</th>
            <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to view objects and their properties, files and comments.">Read <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
            <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to edit objects and their properties and add files and comments. Includes Read permissions.">Write <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
            <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to grant permissions to other users. Includes Write permissions.">Grant <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
          </tr>
        </thead>
        <tbody>
        <tr>
          <td>
            <select class="selectpicker" name="{{ add_group_permissions_form.group_id.name }}" data-width="100%" data-live-search="true">
            {% for group in groups %}
              <option value="{{ group.id }}">{{ group.name }} (#{{ group.id }})</option>
            {% endfor %}
            </select>
          </td>
          {% for possible_permissions in [Permissions.NONE, Permissions.READ, Permissions.WRITE, Permissions.GRANT] %}
            <td class="text-center" style="vertical-align: middle">
              <label for="permissions_new_group_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
              <input type="radio" name="{{ add_group_permissions_form.permissions.name }}" id="permissions_new_group_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if possible_permissions == Permissions.READ %}checked{% endif %}  {% if possible_permissions == Permissions.NONE %}disabled{% endif %} />
            </td>
          {% endfor %}
        </tr>
        </tbody>
      </table>
      <div class="container">
        <button type="submit" class="btn btn-success col-md-offset-9 col-md-3" name="add_group_permissions" value="add_group_permissions">Add</button>
      </div>
      </form>
    {% endif %}
    {% if projects_by_id %}
      <h3>Add Project</h3>
      <form action="{{ url_for('.update_object_permissions', object_id=object.object_id) }}" method="post">
      {{ add_project_permissions_form.csrf_token }}
      <table class="table table-striped">
        <thead>
          <tr>
            <th scope="col" style="width: 40%">Project</th>
            <th scope="col" class="text-center">None</th>
            <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to view objects and their properties, files and comments.">Read <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
            <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to edit objects and their properties and add files and comments. Includes Read permissions.">Write <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
            <th scope="col" class="text-center"><span data-toggle="tooltip" data-placement="bottom" title="Permission to grant permissions to other users. Includes Write permissions.">Grant <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
          </tr>
        </thead>
        <tbody>
        <tr>
          <td>
            <select class="selectpicker" name="{{ add_project_permissions_form.project_id.name }}" data-width="100%" data-live-search="true">
            {% for level, project_id, enabled in project_id_hierarchy_list %}
              <option value="{{ project_id }}" {% if not enabled %}disabled="disabled"{% endif %}>{{ ('&nbsp;' * (4 * level)) | safe }}{{ projects_by_id[project_id].name }} (#{{ project_id }})</option>
            {% endfor %}
            </select>
          </td>
          {% for possible_permissions in [Permissions.NONE, Permissions.READ, Permissions.WRITE, Permissions.GRANT] %}
            <td class="text-center" style="vertical-align: middle">
              <label for="permissions_new_project_{{ possible_permissions.name.lower() }}" class="sr-only">{{ possible_permissions.name.lower() }}</label>
              <input type="radio" name="{{ add_project_permissions_form.permissions.name }}" id="permissions_new_project_{{ possible_permissions.name.lower() }}" value="{{ possible_permissions.name.lower() }}" {% if possible_permissions == Permissions.READ %}checked{% endif %}  {% if possible_permissions == Permissions.NONE %}disabled{% endif %} />
            </td>
          {% endfor %}
        </tr>
        </tbody>
      </table>
      <div class="container">
        <button type="submit" class="btn btn-success col-md-offset-9 col-md-3" name="add_project_permissions" value="add_project_permissions">Add</button>
      </div>
      </form>
    {% endif %}
    {% if copy_permissions_form %}
    <div class="container text-right" id="copyPermissionsLoading" style="display: none;">
      <span class="btn btn-primary col-md-offset-9 col-md-3 disabled" style="margin-top: 1em;">
        <i class="fa fa-spinner fa-spin"></i>
        <span class="sr-only">Loading...</span>
      </span>
     </div>
    <div class="container text-right" id="copyPermissionsBtnDiv">
      <button type="button" class="btn btn-primary col-md-offset-9 col-md-3" id="copyPermissionsBtn" style="margin-top: 1em;" data-toggle="modal" data-target="#copyPermissionsModal">
        Copy Permissions
      </button>
     </div>
    {% endif %}
    <div class="container">
     <a href="{{ url_for('.object', object_id=object.object_id) }}" class="btn btn-default col-md-offset-9 col-md-3" style="margin-top: 1em; margin-bottom: 1em;">Cancel</a>
    </div>
  {% endif %}

  {% if copy_permissions_form %}
  <div class="modal fade" id="copyPermissionsModal" tabindex="-1" role="dialog" aria-labelledby="copyPermissionsModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <form method="post" action="{{ url_for('.object_permissions', object_id=object.id) }}">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="copyPermissionsModalLabel">Copy permissions from other Object</h4>
          </div>
          <div class="modal-body">
            <p class="text-muted">You can replace the current permissions by copying the permissions from another object.</p>
              {{ copy_permissions_form.hidden_tag() }}
              <select class="selectpicker" name="{{ copy_permissions_form.object_id.name }}" data-live-search="true" data-width="100%" data-sampledb-remove="{{ object.id }}" data-sampledb-required-perm="3" data-sampledb-start-show="#copyPermissionsLoading" data-sampledb-start-hide="#copyPermissionsBtnDiv" data-sampledb-empty-disable="#copyPermissionsBtn" data-sampledb-stop-hide="#copyPermissionsLoading" data-sampledb-stop-show="#copyPermissionsBtnDiv">
                {% for object_id, object_name in copy_permissions_form.object_id.choices %}
                  <option value="{{ object_id }}">{{ object_name }} (#{{ object_id }})</option>
                {% endfor %}
              </select>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            <input type="submit" class="btn btn-primary" name="copy_permissions" value="Copy Permissions" />
          </div>
        </form>
      </div>
    </div>
  </div>
  {% endif %}
{% endblock %}
