{% extends "base.html" %}

{% block title %}{{ _('Object Permissions') }} — {{ service_name }}{% endblock %}

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

{% block template_values %}
  {% do set_template_value("component_users", component_users) %}
  {% do set_template_value("policies", policies) %}
  {% do set_template_value("translations.project_group", _('Project Group')) %}
  {% do set_template_value("translations.project_groups", _('Project Groups')) %}
  {% do set_template_value("translations.basic_group", _('Basic Group')) %}
  {% do set_template_value("translations.basic_groups", _('Basic Groups')) %}
  {% do set_template_value("translations.user", _('User')) %}
  {% do set_template_value("translations.users", _('Users')) %}
  {% do set_template_value("translations.not_a_valid_id", _('Not a valid ID. Expected a positive integer.')) %}
  {% do set_template_value("translations.project_group_already_added", _('Project Group has already been added.')) %}
  {% do set_template_value("translations.basic_group_already_added", _('Basic Group has already been added.')) %}
  {% do set_template_value("translations.user_already_added", _('User has already been added.')) %}

  {{ super() }}
{% endblock %}

{% block scripts %}
  {{ super() }}
  <script src="{{ fingerprinted_static('bootstrap-select/js/bootstrap-select.min.js') }}"></script>
  <script src="{{ fingerprinted_static('bootstrap-toggle/js/bootstrap-toggle.min.js') }}"></script>
  <script src="{{ fingerprinted_static('sampledb/js/treepicker.js') }}" type="module"></script>
  <script src="{{ fingerprinted_static('sampledb/js/sampledb-load-objects.js') }}" type="module"></script>
  <script src="{{ fingerprinted_static('sampledb/js/page/object-permissions.js') }}" type="module"></script>
{% endblock %}

{% block content %}
  <h1>{{ _('Object') }} #{{ object.object_id }}{% if object.data %}: {{ object.data.name.text | get_translated_text(default="—") }}{% endif %}</h1>
  <h2>{{ _('Permissions') }}</h2>
  {% set user_may_edit = (permissions_form is not none) %}
  {% set form_url = url_for('.update_object_permissions', object_id=object.object_id) %}
  {% set fixed_user_permissions = {}  %}
  {% set show_administrators = True %}
  {% set show_instrument_scientists = instrument is not none %}
  {% set show_all_users = True %}
  {% set show_anonymous_users = config['ENABLE_ANONYMOUS_USERS'] %}
  {% set read_permissions_description = _('Permission to view objects and their properties, files and comments.') %}
  {% set write_permissions_description = _('Permission to edit objects and their properties and add files and comments. Includes Read permissions.') %}
  {% set grant_permissions_description = _('Permission to grant permissions to other users. Includes Write permissions.') %}
  {%  include "permissions/permissions.html" %}
  {% if user_may_edit %}
    {% if object.component_id is none and (possible_new_components or federation_shares)%}
      <h3 id="other-databases">{{ _('Other databases') }}</h3>
      {% if federation_shares %}
      <form action="{{ url_for('.update_object_permissions', object_id=object.object_id) }}" method="post">
        {{ edit_component_policy_form.csrf_token }}
        <div class="well">
          <label class="control-label">{{ _('Database') }}</label>
          <select class="selectpicker" id="edit-share-component-picker" name="{{ edit_component_policy_form.component_id.name }}" data-width="100%" data-live-search="true">
          {% for _, share in federation_shares.items() %}
            <option value="{{ share.component.id }}">{{ share.component.get_name() }} {% if share.component.address is not none %}({{ share.component.address }}){% endif %}</option>
          {% endfor %}
          </select>
          <label class="control-label" style="margin-top: 20px;">{{ _('Access') }}</label>
          <div class="well" style="display: flex; flex-direction: row; flex-wrap: wrap; justify-content: left;">
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Data') }}</label>
              <div class="checkbox" style="">
                <label>
                  <input type="checkbox" name="{{ edit_component_policy_form.data.name }}" id="policy-edit-data" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if edit_component_policy_form.data.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Data') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Action') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ edit_component_policy_form.action.name }}" id="policy-edit-action" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if edit_component_policy_form.action.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Action') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Users') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ edit_component_policy_form.users.name }}" id="policy-edit-users" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if edit_component_policy_form.users.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Users') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Comments') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ edit_component_policy_form.comments.name }}" id="policy-edit-comments" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if edit_component_policy_form.comments.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Comments') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Locations') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ edit_component_policy_form.object_location_assignments.name }}" id="policy-edit-object-location-assignments" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if edit_component_policy_form.object_location_assignments.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Locations') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Files') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ edit_component_policy_form.files.name }}" id="policy-edit-files" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if edit_component_policy_form.files.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Files') }}</span>
                </label>
              </div>
            </div>
          </div>
          <label class="control-label">{{ _('Permissions') }}</label>
          <div class="well">
            <table class="table table-striped" id="edit_policy_users_table">
              <thead>
                <tr style="background-color: #f9f9f9">
                  <th scope="col" class="text-center"></th>
                  <th scope="col" style="width: 40%">{{ _('Special Groups') }}</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 style="background-color: transparent">
                  <td></td>
                  <td>
                    {{ _('Administrators') }}
                  </td>
                  <td></td>
                  <td></td>
                  <td class="text-center">
                    <input type="radio" disabled="disabled" checked="checked">
                  </td>
                </tr>
              </tbody>
              <tbody id="edit_policy_projects_tbody">
              </tbody>
              <tbody id="edit_policy_groups_tbody">
              </tbody>
              <tbody id="edit_policy_users_tbody">
              </tbody>
            </table>
            <h4>{{ _('Add User') }}</h4>
            <div class="form-group">
              <label>
                {{ _('Select User') }} <span  data-toggle="tooltip" data-placement="bottom" title="{{ _('If a user is missing from this list, please enter their ID below.') }}"><i class="fa fa-question-circle"></i></span>
              </label>
              <div class="input-group">
                <select class="selectpicker" id="edit_share_user_picker" name="edit_share_user_picker" data-live-search="true" data-width="100%"></select>
                <span class="input-group-btn">
                  <button type="button" class="btn btn-success select-group-button" id="edit_component_policy_user_select_btn" name="edit_component_policy_user_select_btn">{{ _('Add') }}</button>
                </span>
              </div>
            </div>
            <div style="margin-bottom: 5px; margin-top: -10px;" class="text-muted">
              {{ _('or') }}
            </div>
            <div class="form-group">
              <label>
                {{ _('Enter User ID') }} <span  data-toggle="tooltip" data-placement="bottom" title="{{ _('To find the user ID, check the user profile in the other database.') }}"><i class="fa fa-question-circle"></i></span>
              </label>
              <div class="input-group">
                <input type="text" id="edit_share_user_text" class="form-control" placeholder="{{ _('User ID') }}" style="width: 100%">
                <span class="input-group-btn">
                  <button type="button" class="btn btn-success" id="edit_component_policy_user_input_btn" name="edit_share_user_input_btn">{{ _('Add') }}</button>
                </span>
              </div>
              <span id="edit_share_user_text_help_block" style="display: none;" class="help-block"></span>
            </div>
            <div class="help-block">
              <strong>{{ _('Note') }}:</strong> {{ _('Objects from other databases are currently read-only, but <strong>WRITE</strong> permissions may allow users to edit the object or add comments or files to it in the future.') }}
            </div>
            <h4>{{ _('Add Basic Group') }}</h4>
            <div class="form-group">
              <label>
                {{ _('Enter Basic Group ID') }}
              </label>
              <div class="input-group">
                <input type="text" id="edit_share_group_text" class="form-control" placeholder="{{ _('Basic Group ID') }}" style="width: 100%">
                <span class="input-group-btn">
                  <button type="button" class="btn btn-success"  id="edit_component_policy_group_input_btn" name="edit_share_group_input_btn">{{ _('Add') }}</button>
                </span>
              </div>
              <span id="edit_share_group_text_help_block" style="display: none;" class="help-block"></span>
            </div>
            <h4>{{ _('Add Project Group') }}</h4>
            <div class="form-group">
              <label>
                {{ _('Enter Project Group ID') }}
              </label>
              <div class="input-group">
                <input type="text" id="edit_share_project_text" class="form-control" placeholder="{{ _('Project Group ID') }}" style="width: 100%">
                <span class="input-group-btn">
                  <button type="button" class="btn btn-success"  id="edit_component_policy_project_input_btn" name="edit_share_project_input_btn">{{ _('Add') }}</button>
                </span>
              </div>
              <span id="edit_share_project_text_help_block" style="display: none;" class="help-block"></span>
            </div>
          </div>
        </div>
        <div class="container">
          <button type="submit" class="btn btn-success col-md-offset-9 col-md-3" name="edit_component_policy" value="edit_component_policy">{{ _('Save') }}</button>
        </div>
      </form>
      {% endif %}
      {% if possible_new_components %}
      <h4>{{ _('Add database') }}</h4>
      <form action="{{ url_for('.update_object_permissions', object_id=object.object_id) }}" method="post">
        {{ add_component_policy_form.csrf_token }}
        <div class="well">
          <label class="control-label">{{ _('Database') }}</label>
          <select class="selectpicker" id="add_share_component_picker" name="{{ add_component_policy_form.component_id.name }}" data-width="100%" data-live-search="true">
          {% for component in possible_new_components %}
            <option value="{{ component.id }}">{{ component.get_name() }}{% if component.address is not none %} ({{ component.address }}){% endif %}</option>
          {% endfor %}
          </select>
          <label class="control-label" style="margin-top: 20px;">{{ _('Access') }}</label>
          <div class="well" style="display: flex; flex-direction: row; flex-wrap: wrap; justify-content: left;">
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Data') }}</label>
              <div class="checkbox" style="">
                <label>
                  <input type="checkbox" name="{{ add_component_policy_form.data.name }}" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if add_component_policy_form.data.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Data') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Action') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ add_component_policy_form.action.name }}" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if add_component_policy_form.action.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Action') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Users') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ add_component_policy_form.users.name }}" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if add_component_policy_form.users.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Users') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Comments') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ add_component_policy_form.comments.name }}" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if add_component_policy_form.comments.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Comments') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Locations') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ add_component_policy_form.object_location_assignments.name }}" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if add_component_policy_form.object_location_assignments.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Locations') }}</span>
                </label>
              </div>
            </div>
            <div style="width: 155px; padding: 7px;">
              <label class="control-label">{{ _('Files') }}</label>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="{{ add_component_policy_form.files.name }}" data-onstyle="default" data-toggle="toggle" data-width="130" data-on="{{ _('Share') }}" data-off="{{ _('Do not share') }}"{% if add_component_policy_form.files.data %} checked{% endif %}>
                  <span class="sr-only">{{ _('Files') }}</span>
                </label>
              </div>
            </div>
          </div>
          <label class="control-label">{{ _('Permissions') }}</label>
          <div class="well">
            <table class="table table-striped" id="new_policy_users_table">
              <thead>
                <tr>
                  <th scope="col" class="text-center"></th>
                  <th scope="col" style="width: 40%">{{ _('Special Groups') }}</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 style="background-color: transparent">
                  <td></td>
                  <td>
                    {{ _('Administrators') }}
                  </td>
                  <td></td>
                  <td></td>
                  <td class="text-center">
                    <input type="radio" disabled="disabled" checked="checked">
                  </td>
                </tr>
              </tbody>
              <tbody id="new_policy_projects_tbody">
              </tbody>
              <tbody id="new_policy_groups_tbody">
              </tbody>
              <tbody id="new_policy_users_tbody">
              </tbody>
            </table>
            <h4>{{ _('Add User') }}</h4>
            <div class="form-group">
              <label>
                {{ _('Select User') }} <span  data-toggle="tooltip" data-placement="bottom" title="{{ _('If a user is missing from this list, please enter their ID below.') }}"><i class="fa fa-question-circle"></i></span>
              </label>
              <div class="input-group">
                <select class="selectpicker" id="add_share_user_picker" data-live-search="true" data-width="100%">
                </select>
                <span class="input-group-btn">
                  <button type="button" class="btn btn-success select-group-button" id="add_component_policy_user_select_btn">{{ _('Add') }}</button>
                </span>
              </div>
            </div>
            <div style="margin-bottom: 5px; margin-top: -10px;" class="text-muted">
              {{ _('or') }}
            </div>
            <div class="form-group">
              <label>
                {{ _('Enter User ID') }} <span  data-toggle="tooltip" data-placement="bottom" title="{{ _('To find the user ID, check the user profile in the other database.') }}"><i class="fa fa-question-circle"></i></span>
              </label>
              <div class="input-group">
                <input type="text" class="form-control" id="add_share_user_text" placeholder="{{ _('User ID') }}">
                <span class="input-group-btn">
                  <button type="button" class="btn btn-success" id="add_component_policy_user_input_btn" name="add_component_policy_user_input_btn" value="add_component_policy_user_input_btn">{{ _('Add') }}</button>
                </span>
              </div>
              <span id="add_share_user_text_help_block" style="display: none;" class="help-block"></span>
            </div>
            <div class="help-block">
              <strong>{{ _('Note') }}:</strong> {{ _('Objects from other databases are currently read-only, but <strong>WRITE</strong> permissions may allow users to edit the object or add comments or files to it in the future.') }}
            </div>
            <h4>{{ _('Add Basic Group') }}</h4>
            <div class="form-group">
              <label>
                {{ _('Enter Basic Group ID') }}
              </label>
              <div class="input-group">
                <input type="text" id="add_share_group_text" class="form-control" placeholder="{{ _('Basic Group ID') }}" style="width: 100%">
                <span class="input-group-btn">
                  <button type="button" class="btn btn-success" id="add_component_policy_group_input_btn" name="add_component_policy_group_input_btn">{{ _('Add') }}</button>
                </span>
              </div>
              <span id="add_share_group_text_help_block" style="display: none;" class="help-block"></span>
            </div>
            <h4>{{ _('Add Project Group') }}</h4>
            <div class="form-group">
              <label>
                {{ _('Enter Project Group ID') }}
              </label>
              <div class="input-group">
                <input type="text" id="add_share_project_text" class="form-control" placeholder="{{ _('Project Group ID') }}" style="width: 100%">
                <span class="input-group-btn">
                  <button type="button" class="btn btn-success" id="add_component_policy_project_input_btn" name="add_component_policy_project_input_btn">{{ _('Add') }}</button>
                </span>
              </div>
              <span id="add_share_project_text_help_block" style="display: none;" class="help-block"></span>
            </div>
          </div>
        </div>
        <div class="container">
          <button type="submit" class="btn btn-success col-md-offset-9 col-md-3" name="add_component_policy" value="add_component_policy">{{ _('Add') }}</button>
        </div>
      </form>
      {% endif %}
    {% 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">
              </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 %}
