<% page_title t('titles.schools'), @school.displayName, t('titles.groups_rename') %>

<div class="formContainer">

<h1><%= t('rename_groups.title') %></h1>

<%= form_for("renamge_groups", :url => rename_groups_path(@school) ) do |f| %>

<% if @groups.empty? %>
  <p><%= t('rename_groups.no_groups_to_rename') %></p>
<% else %>

  <p><%= t('rename_groups.school_grades') %>: <b><%= @first_group_class_number %> - <%= @last_group_class_number %></b></p>
  <p><%= t('rename_groups.all_grades') %> <%= (@all_group_class_found ? t('rename_groups.found') : '<strong>' + t('rename_groups.not_found') + '</strong>').html_safe %>.</p>

  <p><%= t('rename_groups.grades_title') %>
  <% if @first_class_number == @first_group_class_number && @last_class_number == @last_group_class_number %>
    <%= t('rename_groups.grades_match') %>
  <% else %>
    <strong><%= t('rename_groups.grades_no_match') %></strong>
  <% end %>
  </p>

  <table class="editForm massGroupRenameTable">
    <tr>
      <th><%= t('rename_groups.old_name') %></th>
      <th><%= t('rename_groups.new_name') %></th>
    </tr>

    <% @groups.each do |group| %>
    <tr>
      <td><%= group.displayName %> (<%= group.cn %>)</td>
      <td>
        <%= hidden_field_tag "group_puavo_id[]", group.puavoId %>

        <% if group.displayName.match(/\d+/)[0].to_i == @last_class_number %>
          <% new_name = Time.now.year.to_s + " poistuvat, " + group.displayName %>
        <% else %>
          <% new_name = increase_numeric_value_of_string(group.displayName) %>
        <% end %>

        <%= text_field_tag "group_display_name[]", new_name, size: 40 %>
      </td>
    </tr>
    <% end %>
  </table>

  <br><br>
  <h4><%= t('rename_groups.will_create_new_groups') %>:</h4>

  <% if @new_group_name_already_used %>
    <p style="color: #f00;"><%= t('rename_groups.abbreviation_already_in_use') %></p><br>
  <% end %>

  <table class="editForm massGroupRenameTable">
    <tr>
      <th><%= t('rename_groups.abbreviation') %></th>
      <th><%= t('rename_groups.name') %></th>
    </tr>

    <% @first_class_groups.each do |group| %>
    <tr>
      <td><%= text_field_tag "new_groups_cn[]", increase_numeric_value_of_string(group.cn) %></td>
      <td><%= text_field_tag "new_groups_display_name[]", group.displayName %></td>
    </tr>
    <% end %>
  </table>
<% end %>

<br>
<%= f.submit t('rename_groups.submit'), class: 'btn' %>

<% end %>

</div>
