<% provide(:head_title, t("users.settings.organizations.head_title")) %>

<%= render partial: "users/settings/navigation.html.erb" %>
<div class="tab-content">
  <div class="tab-pane tab-pane-settings" role="tabpanel"></div>
  <div class="tab-pane tab-pane-settings active" role="tabpanel">

    <%= render partial: "users/settings/organizations/breadcrumbs.html.erb" %>

    <div>
      <% if @member_of > 0 %>
        <%= t("users.settings.organizations.index.member_of", count: @member_of) %>
      <% else %>
        <em><%= t("users.settings.organizations.index.no_organizations") %></em>
      <% end %>
      <%= link_to new_organization_path, class: "btn btn-default", style: "margin-left: 30px;" do %>
        <span class="glyphicon glyphicon-plus"></span>
        <span class="hidden-xs">
          <%= t("users.settings.organizations.index.new_organization") %>
        </span>
      <% end %>
    </div>

    <% if @member_of > 0 %>
      <table class="table">
        <thead>
          <tr>
            <th><%=t "users.settings.organizations.index.thead_name" %></th>
            <th><%=t "users.settings.organizations.index.thead_role" %></th>
            <th class="hidden-xs"><%=t "users.settings.organizations.index.thead_created_at" %></th>
            <th class="hidden-xs"><%=t "users.settings.organizations.index.thead_joined_on" %></th>
            <th><%=t "users.settings.organizations.index.thead_members" %></th>
            <th style="width: 1%;">&nbsp;</th>
          </tr>
        </thead>
        <tbody>
          <% @user_orgs.each do |user_org| %>
            <% org = user_org.organization %>
            <tr>
              <td>
                <% if user_org.admin? %>
                  <%= link_to org.name, organization_path(org) %>
                <% else %>
                  <%= org.name %>
                <% end %>
              </td>
              <td><%= user_org.role_str %></td>
              <td class="hidden-xs">
                <% if user_org.guest? %>
                  <%= t("users.settings.organizations.index.na") %>
                <% else %>
                  <%= l(org.created_at, format: :full) %>
                <% end %>
              </td>
              <td class="hidden-xs"><%= l(user_org.created_at, format: :full) %></td>
              <td>
                <% if user_org.guest? %>
                  <%= t("users.settings.organizations.index.na") %>
                <% else %>
                  <%= org.users.count %>
                <% end %>
              </td>
              <td>
                <!-- If user is last admin of organization, don't allow
                him/her to leave organization -->
                <% if user_org.admin? && org.user_organizations.where(role: 2).count <= 1 %>
                  <div class="btn btn-default btn-xs" type="button" disabled="disabled">
                    <span class="glyphicon glyphicon-log-out"></span>
                    <span class="hidden-xs">
                      <%= t("users.settings.organizations.index.leave") %>
                    </span>
                  </div>
                <% else %>
                  <%= link_to leave_user_organization_html_path(user_org, format: :json), remote: true, class: "btn btn-default btn-xs", type: "button", data: { action: "leave-user-organization" } do %>
                    <span class="glyphicon glyphicon-log-out"></span>
                    <span class="hidden-xs">
                      <%= t("users.settings.organizations.index.leave") %>
                    </span>
                  <% end %>
                <% end %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    <% else %>
      <br />
    <% end %>
  </div>
</div>

<%= render partial: "users/settings/organizations/leave_user_organization_modal.html.erb" %>
<%= javascript_include_tag "users/settings/organizations" %>