<% page_title t('titles.schools'), @current_schools[0].displayName, t('titles.users'), @user.displayName, t('.title') %>

<div class="formContainer">

<h1><%= t('.header', :name => link_to( @user.displayName, "./") ).html_safe %></h1>

<p><%= t('.sync_warning') %></p>
<p><%= t('.instant_save_warning') %></p>

<table class="schoolsTable">
  <tr><th colspan="3"><span><%= t('.current_title', :count => @current_schools.count) %></span></th></tr>

  <% @current_schools.each do |s| %>
  <% classes = ["school"] %>
  <% classes << "primary" if s.dn == @primary_school_dn %>
  <tr class="<%= classes.join(' ') %>" id="current-<%= s.cn %>">
    <td>
      <%= link_to s.displayName, school_path(s) %> <%= s.dn == @primary_school_dn ? "<strong>(#{t(".primary_school")})</strong>".html_safe : '' %>
      <% if s.puavoExternalId %>
      <br>(<%= t('activeldap.attributes.user.puavoExternalId') %>: <%= s.puavoExternalId %>)
      <% end %>
      <% if @admin_in_schools.include?(s.dn.to_s) %>
      <br><span class="admin"><%= t('.is_school_admin') %></span>
      <% end %>
    </td>

    <td>
      <% if s.dn == @primary_school_dn %>
      <% if @current_schools.count == 1 %>
      <p><%= t('.cant_remove_from_primary_school_one').html_safe %></p>
      <% else %>
      <p><%= t('.cant_remove_from_primary_school_multiple').html_safe %></p>
      <% end %>
      <% else %>
      <%= link_to remove_user_from_school_path(s, @user, s.puavoId), class: 'btn btn-danger', id: "remove-#{s.cn}" do %><i class='icon-minus'></i><%= t('.remove_from_this_school') %><% end %>
      <%= link_to set_user_primary_school_path(s, @user, s.puavoId), class: 'btn', id: "swap-primary-#{s.cn}" do %><i class='icon-exchange'></i><%= t('.swap_primary_school') %><% end %>
      <% end %>
    </td>
  </tr>
  <% end %>

  <% unless @available_schools.empty? %>
  <tr><th colspan="3"><br><span><%= t('.available_title', :count => @available_schools.count) %></span></th></tr>
  <% @available_schools.each_with_index do |s, index| %>
  <tr class="school" id="available-<%= s.cn %>">
    <td>
      <%= link_to s.displayName, school_path(s) %>
      <% if s.puavoExternalId %>
      <br>(Ulkoinen ID: <%= s.puavoExternalId %>)
      <% end %>
    </td>

    <td>
      <%= link_to add_user_to_school_path(s, @user, s.puavoId), class: 'btn', id: "add-#{s.cn}" do %><i class='icon-plus'></i><%= t('.add_to_this_school') %><% end %>
      <% if @current_schools.count == 1 %>
      <%= link_to move_to_school_path(s, @user, s.puavoId), class: 'btn', id: "set-primary-#{s.cn}" do %><i class='icon-exchange'></i><%= t('.move_to_school') %><% end %>
      <% end %>
      <%= link_to add_and_set_user_primary_school_path(s, @user, s.puavoId), class: 'btn', id: "set-primary-#{s.cn}" do %><i class='icon-exchange'></i><%= t('.add_as_primary_school') %><% end %>
    </td>
  </tr>
  <% end %>
  <% end %>
</table>

<br><p><%= link_to user_path(@current_schools[0], @user), class: 'btn' do %>
  <i class='icon-ok'></i><%= t('.back') %>
<% end %></p>

</div>
