{% extends "community/_base.html" %}

{% from "macros/box.html" import  m_box_content, m_box_menu with context %}
{% from "macros/recent.html" import m_recent_items %}

{% macro m_users_table(users_and_roles, controls) %}
  {% set table_id = uuid() %}
  <form method="POST">
    {{ csrf.field() }}

    <table class="table table-condensed table-striped" id="{{ table_id }}">
      <thead>
      <tr>
        <th></th>
        <th class="hide">Last Name</th>
        <th class="hide">First Name</th>
        <th>{{ _("Name") }}</th>
        <th>{{ _("Role") }}</th>
        {% if controls %}
          <th>{{ _("Action") }}</th>
        {% endif %}
      </tr>
      </thead>

      <tbody>
      {% for user, role, has_access in users_and_roles %}
        <tr>
          <td>{%- if not user.can_login %}
            <i class="glyphicon glyphicon-lock" title="{{ _('account disabled') }}"></i>
          {%- elif has_access %}
            <i class="fa fa-check">&nbsp;</i>
          {%- endif %}
          </td>
          <td class="hide">{{ user.last_name }}</td>
          <td class="hide">{{ user.first_name }}</td>
          <td>
            {%- if not user.can_login %}<s title="{{ _('account disabled') }}">{%- endif %}
            <a href="{{ url_for("social.user", user_id=user.id) }}">{{ user.name }}</a>
            ({{ user.email }})
            {%- if not user.can_login %}</s>{%- endif %}
          </td>
          <td>{{ role }}</td>
          {%- if controls %}
            <td>
              <button type="submit" name="delete-user-role"
                      value="{{ "%s:%s" % (role, user.id) }}"
                      class="btn btn-small btn-danger"><i class="fa fa-trash"></i>
              </button>
            </td>
          {%- endif %}
        </tr>
      {% endfor %}
      </tbody>
    </table>
  </form>

  {%- deferJS %}
    <script>
      $(document).ready(function () {
        var id = "#{{ table_id }}"; // Should be unique enough
        $(id).dataTable({
          aaSorting: [[3, "asc"]],
          aoColumns: [
            {bSortable: false}, // 0: has_access
            {bVisible: false},  // 1: last_name
            {bVisible: false}, // 2: first_name
            {aDataSort: [1, 2], asSorting: ["asc", "desc"]}, // 3: fullname
            {aDataSort: [4, 1, 2], asSorting: ["asc", "desc"]}, // 4: role - sort by role then name
            {%- if controls %}
              {bSortable: false}
            {% endif %}
          ],
          sPaginationType: "bootstrap",
          bFilter: true,
          bLengthChange: false
        });
      });
    </script>
  {%- enddeferJS %}

  <div style="clear: both;"></div>
{% endmacro %}

{% macro m_groups_table(groups_and_roles, controls) %}
  {% set table_id = uuid() %}
  <form method="POST">
    {{ csrf.field() }}
    <table class="table table-condensed table-striped" id="{{ table_id }}">
      <thead>
      <tr>
        <th>{{ _("Name") }}</th>
        <th>{{ _("Role") }}</th>
        {% if controls %}
          <th>{{ _("Action") }}</th>
        {% endif %}
      </tr>
      </thead>

      <tbody>
      {% for group, role in groups_and_roles %}
        <tr>
          <td><a
              href="{{ url_for("social.group_home", group_id=group.id) }}">{{ group.name }}</a>
          </td>
          <td>{{ role }}</td>
          {%- if controls %}
            <td>
              <button type="submit" name="delete-group-role"
                      value="{{ role }}:{{ group.id }}"
                      class="btn btn-small btn-danger"><i class="fa fa-trash"></i>
              </button>
            </td>
          {% endif %}
        </tr>
      {% endfor %}
      </tbody>
    </table>
  </form>

  {%- deferJS %}
    <script>
      $(document).ready(function () {
        var id = "#{{ table_id }}"; // Should be unique enough
        $(id).dataTable({
          aoColumns: [
            {asSorting: ["asc", "desc"]},
            {asSorting: ["asc", "desc"]},
            {%- if controls %}
              {asSorting: ["asc", "desc"]}
            {% endif %}
          ],
          sPaginationType: "bootstrap",
          bFilter: true,
          bLengthChange: false
        });
      });
    </script>
  {%- enddeferJS %}
  <div style="clear: both;"></div>
{% endmacro %}


{% block content %}
  {% call m_box_content("Permissions") %}
    <h2 style="margin-top: 0;">{{ _('Manage permissions on <em>{folder}</em>').format(folder=folder.title) }}</h2>

    <a class="btn btn-default datatable-export"
       href="{{ url_for('.permissions_export', folder_id=folder.id, community_id=folder.community.slug) }}"><i
        class="fa fa-align-justify"></i>{{ _("Export to Excel") }}</a>

    <hr/>

    <h3>{{ _("Inheritance") }}</h3>

    {% if folder.inherit_security %}
      <i class="fa fa-check-circle-o"></i>{{ _("Activated") }}
      <button class="btn btn-default"
              id="deactivate_inheritance">{{ _("Deactivate") }}</button>
    {% else %}
      <i class="fa fa-ban"></i>{{ _("Deactivated") }}
      <button class="btn btn-default" id="activate_inheritance">{{ _("Activate") }}</button>
    {% endif %}

    {#
    <h3>Current local roles for groups</h3>

    {% if groups_and_local_roles %}
      {{ m_groups_table(groups_and_local_roles, controls=True) }}
    {% else %}
      {{ _("Nothing") }}.
    {% endif %}

    <p>
      <button class="btn" href="#modal-add-group-local-role"
          data-toggle="modal">
        <i class="fa fa-plus"></i>{{ _("Add new local role") }}&hellip;</button>
    </p>

    <h3>{{ _("Current inherited roles for groups") }}</h3>

    {% if folder.inherit_security %}
      {% if groups_and_inherited_roles %}
        {{ m_groups_table(groups_and_inherited_roles, controls=False) }}
      {% else %}
        {{ _("Nothing") }}.
      {% endif %}
    {% else %}
      {{ _("Deactivated") }}
    {% endif %}

    <hr>
    #}

    <h3>{{ _("Current local roles for users") }}</h3>

    {% if users_and_local_roles %}
      {{ m_users_table(users_and_local_roles, controls=True) }}
    {% else %}
      {{ _("Nothing") }}.
    {% endif %}

    <p>
      <button class="btn btn-default" href="#modal-add-user-local-role" data-toggle="modal">
        <i class="fa fa-plus"></i>{{ _("Add new local role") }}&hellip;
      </button>
    </p>

    <h3>{{ _("Current inherited roles for users") }}</h3>

    {% if folder.inherit_security %}
      {% if users_and_inherited_roles %}
        {{ m_users_table(users_and_inherited_roles, controls=False) }}
      {% else %}
        {{ _("Nothing") }}.
      {% endif %}
    {% else %}
      {{ _("Deactivated") }}
    {% endif %}

    <h3 class="main">{{ _("Audit Log") }}</h3>
    <div style="margin-bottom: 25px;">
      {% for entry in audit_entries %}
        <div>{{ entry.msg }}</div>
      {% endfor %}
    </div>

  {% endcall %}
{% endblock %}


{% block sidebar %}
  {%- set content_actions = actions.for_category('documents:content') %}
  {%- if content_actions %}
    {% call m_box_menu() %}
      <ul class="nav nav-list">
        {%- for action in content_actions %}
          <li>{{ action.render() }}</li>
        {%- endfor %}
      </ul>
    {% endcall %}
  {%- endif %}
{% endblock %}


{% block modals %}
  {% include "documents/_modals_folder.html" %}
  {% include "documents/_modals_roles.html" %}
{% endblock %}

{% block js %}
  <script>
    require(
        ['Abilian', 'jquery'],
        function (Abilian, $) {

          Abilian.fn.onAppInit(function () {
            $("#deactivate_inheritance").click(function () {
              var form = $('<form>{{ csrf.field() }}<input type="text" name="action" value="deactivate_inheritance" /></form>');
              form.attr("method", "post");
              form.attr("action", "{{ request.path }}");
              $(document.body).append(form);
              form.submit();
            });

            $("#activate_inheritance").click(function () {
              var form = $('<form>{{ csrf.field() }}<input type="text" name="action" value="activate_inheritance" /></form>');
              form.attr("method", "post");
              form.attr("action", "{{ request.path }}");
              $(document.body).append(form);
              form.submit();
            });

            $("#user-select").attr("style", "width: 220px").select2({
              minimumInputLength: 2,
              ajax: {
                url: "{{ url_for("social.users_json") }}",
                dataType: 'json',
                quietMillis: 100,
                data: function (term, page) {
                  return {q: term};
                },
                results: function (data, page) {
                  return {results: data.results, more: false};
                }
              },
              allowClear: true
            });
            $("#s2id_user-select a span").text("");
            $("input#user-select").attr("value", "");

          });
        });
  </script>
{% endblock %}
