<%
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License along
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
%>

<%
# BigBlueButton open source conferencing system - http://www.bigbluebutton.org/.
# Copyright (c) 2018 BigBlueButton Inc. and by respective authors (see below).
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License along
# with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
%>

<% if @role.nil? %>
  <%= render "admins/components/manage_users_tags" %>
<% else %>
  <%= render "admins/components/admins_tags" %>
<% end %>

<div class="form-group">
  <div class="row">
    <div class="col-12">
      <div class="table-responsive">
        <table class="table table-hover table-outline table-vcenter card-table">
          <thead>
            <tr>
              <th data-header="name" data-order="<%= @order_column == "name" ? @order_direction : "none" %>">
                <%= t("administrator.users.table.name") %>
                <% if @order_column == "name" && @order_direction == "desc" %>
                 ↓
                <% elsif @order_column == "name" && @order_direction == "asc" %>
                 ↑
                <% end %>
              </th>
              <th data-header="email" data-order="<%= @order_column == "email" ? @order_direction : "none" %>">
                <%= t("administrator.users.table.username") %>
                <% if @order_column == "email" && @order_direction == "desc" %>
                 ↓
                <% elsif @order_column == "email" && @order_direction == "asc" %>
                 ↑
                <% end %>
              </th>
              <th class="text-left" data-header="provider" data-order="<%= @order_column == "provider" ? @order_direction : "none" %>">
                <%= t("administrator.users.table.authenticator") %>
                <% if @order_column == "provider" && @order_direction == "desc" %>
                 ↓
                <% elsif @order_column == "provider" && @order_direction == "asc" %>
                 ↑
                <% end %>
              </th>
              <th class="text-center">
                <%= t("administrator.users.table.role") %>
              </th>
              <th class="text-center">
              </th>
            </tr>
          </thead>
          <tbody id="users-table">
            <tr id="no_users_found" style="display: none;">
              <td colspan="7" class="text-center h4 p-6 font-weight-normal" >
                <%= t("administrator.users.table.not_found") %>
              </td>
            </tr>
            <% if @users %>
              <% @users.each do |user| %>
                <% if user != current_user %>
                  <tr data-user-uid="<%= user.uid %>">
                    <td>
                      <div><%= user.name %></div>
                      <div class="small text-muted"><%= [t("administrator.users.table.created"), ": ", user.created_at].join %></div>
                    </td>
                    <td class="user-email"><%= user.email && user.email != "" ? user.email : user.username%></td>
                    <td><%= user.provider %></td>
                    <td class="text-center">
                      <% roles = user.roles().pluck(:name) %>
                      <%= render "admins/components/admins_role", role: user.highest_priority_role %>
                    </td>
                    <td>
                      <% if !roles.include?("super_admin") %>
                        <div class="item-action dropdown">
                          <a href="javascript:void(0)" data-toggle="dropdown" class="icon">
                            <i class="fas fa-ellipsis-v px-4"></i>
                          </a>
                          <div class="dropdown-menu dropdown-menu">
                            <% if user.deleted? %>
                              <%= button_to admin_undelete_path(user_uid: user.uid), class: "dropdown-item", "data-disable": "" do %>
                                <i class="dropdown-icon fas fa-recycle"></i> <%= t("administrator.users.settings.undelete") %>
                              <% end %>
                              <button class="delete-user dropdown-item" data-path="<%= delete_user_path(user_uid: user.uid, permanent: "true") %>" data-toggle="modal" data-target="#deleteAccountModal">
                                <i class="dropdown-icon fas fa-skull-crossbones"></i> <%= t("administrator.users.settings.perm_delete") %>
                              </button>
                            <% elsif roles.include?("denied") %>
                              <%= button_to admin_unban_path(user_uid: user.uid), class: "dropdown-item", "data-disable": "" do %>
                                <i class="dropdown-icon fas fa-lock-open"></i> <%= t("administrator.users.settings.unban") %>
                              <% end %>
                              <button class= "delete-user dropdown-item" data-path="<%= delete_user_path(user_uid: user.uid) %>" data-delete="temp-delete" data-toggle="modal" data-target="#deleteAccountModal">
                                <i class="dropdown-icon fas fa-user-minus"></i> <%= t("administrator.users.settings.delete") %>
                              </button>
                            <% elsif roles.include?("pending") %>
                              <%= button_to admin_approve_path(user_uid: user.uid), class: "dropdown-item", "data-disable": "" do %>
                                <i class="dropdown-icon far fa-check-circle"></i> <%= t("administrator.users.settings.approve") %>
                              <% end %>
                              <%= button_to admin_ban_path(user_uid: user.uid), class: "dropdown-item", "data-disable": "" do %>
                                <i class="dropdown-icon far fa-times-circle"></i> <%= t("administrator.users.settings.decline") %>
                              <% end %>
                            <% else %>
                              <%= link_to admin_edit_user_path(user_uid: user.uid), class: "dropdown-item" do %>
                                <i class="dropdown-icon fas fa-user-edit"></i> <%= t("administrator.users.settings.edit") %>
                              <% end %>
                              <button class= "merge-user dropdown-item" data-path="<%= merge_user_path(user_uid: user.uid) %>" data-info="<%= user.slice(:name, :email, :uid).to_json %>" data-toggle="modal" data-target="#mergeUserModal">
                                <i class="dropdown-icon fas fa-user-friends"></i> <%= t("administrator.users.settings.merge") %>
                              </button>
                              <%= button_to admin_ban_path(user_uid: user.uid), class: "dropdown-item", "data-disable": "" do %>
                                <i class="dropdown-icon fas fa-lock"></i> <%= t("administrator.users.settings.ban") %>
                              <% end %>
                              <button class= "delete-user dropdown-item" data-path="<%= delete_user_path(user_uid: user.uid) %>" data-delete="temp-delete" data-toggle="modal" data-target="#deleteAccountModal">
                                <i class="dropdown-icon fas fa-user-minus"></i> <%= t("administrator.users.settings.delete") %>
                              </button>
                            <% end %>
                          </div>
                        </div>
                      <% end %>
                    </td>
                  </tr>
                <% end %>
              <% end %>
            <% else %>
              <tr>
                <td colspan="7" class="text-center h4 p-6 font-weight-normal" >
                  <%= t("administrator.users.table.no_users") %>
                </td>
              </tr>
            <% end %>
          </tbody>
        </table>
        <div class="float-right mt-4">
          <%== pagy_bootstrap_nav(@pagy) %>
        </div>
      </div>
    </div>
  </div>
</div>

<%= render "shared/modals/invite_user_modal" %>
<%= render "shared/modals/delete_account_modal", delete_location: relative_root %>
<%= render "shared/modals/merge_user_modal" %>