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

<div class="dashboard">

<div class="contentBox schoolImage">
<%= image_tag( @school.jpegPhoto.nil? ? "school.png" : image_school_path(@school),
               class: "image", skip_pipeline: true ) %>
</div>

<%= start_box t('.contacts') %>
  <%= h @school.displayName %><br>
  <%= h @school.street %><br>
  <%= h @school.postalCode %> <%= h @school.postalAddress %><br>
  <%= h @school.postOfficeBox %><br>
  <%= link_to t('activeldap.attributes.school.puavoSchoolHomePageURL'),
              @school.puavoSchoolHomePageURL,
              class: 'schoolHomepage' %>
<%= end_box %>

<hr>

<!-- Various bits and pieces -->
<%= start_box t('.info') %>
  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoLocale') %></div>
    <div class="value"><%= h language_by_locale(@school.puavoLocale) %><br>
    <em><%= t("old_preferred_language", :language => @school.preferredLanguage) %></em>
    </div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoDeviceImage') %></div>
    <div class="value">
      <% img = value_or_default_value_by_parent(@school, "puavoDeviceImage") %>
      <% if @releases.include?(img.gsub(/<[^>]+>/, "").split(" ")[0]) %>
        <%= h img %> (<span class="releaseName"><%= @releases[img.gsub(/<[^>]+>/, "").split(" ")[0]] %></span>)</li>
      <% else %>
        <%= h img %>
      <% end %>
    </div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoImageSeriesSourceURL') %></div>
    <div class="value"><%= h value_or_default_value_by_parent(@school, "puavoImageSeriesSourceURL") %></div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoAllowGuest') %></div>
    <div class="value"><%= translate_boolean_value(@school.puavoAllowGuest) %></div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoAutomaticImageUpdates') %></div>
    <div class="value"><%= translate_boolean_value(@school.puavoAutomaticImageUpdates) %></div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoTag') %></div>
    <div class="value"><%= @school.puavoTag %></div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoConf') %></div>

    <select id="changeVisibleConfig" class="margin-left-25px margin-bottom-5px" autocomplete="off">
      <option value="directly_set"><%= t('puavoconf_table.show.directly_set') %></option>
      <option value="effective"><%= t('puavoconf_table.show.effective') %></option>
      <option value="effective_overrides"><%= t('puavoconf_table.show.effective_overrides') %></option>
      <option value="all"><%= t('puavoconf_table.show.all') %></option>
    </select>

    <button id="copyPuavoconf"><%= t('link.copy_to_clipboard') %></button>

    <div class="puavoConfOutput margin-left-25px flex-rows gap-5px">
      <%= render partial: 'shared/puavoconf', locals: { puavoconf: @full_puavoconf, type: 'sch' } %>
    </div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoTimezone') %></div>
    <div class="value"><%= h humanize_timezone(@school.puavoTimezone) %></div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoDeviceAutoPowerOffMode') %></div>
    <div class="value">
        <%= h t('puavoDeviceAutoPowerOffMode_' + (@school.puavoDeviceAutoPowerOffMode or 'default'),
                :scope => "activeldap.attributes.#{ @school.class.human_name }") %>
    </div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoDeviceOnHour') %></div>
    <div class="value"><%= @school.puavoDeviceOnHour %></div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoDeviceOffHour') %></div>
    <div class="value"><%= @school.puavoDeviceOffHour %></div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.bootservers') %></div>
    <div class="value">
      <ul>
        <% if current_user.organisation_owner? %>
        <% @school.boot_servers.each do |bs| %>
        <li><%= link_to bs.puavoHostname, server_path(bs) %></li>
        <% end %>
        <% else %>
        <% @school.boot_servers.each do |bs| %>
        <li><%= h bs.puavoHostname %></li>
        <% end %>
        <% end %>
      </ul>
    </div>
  </div>

  <% if @puavomenu_data %>
  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.puavoMenuDataPreview') %></div>
    <div class="value">
      <ul>
        <% if @puavomenu_data[:categories] %>
        <li><strong><%= t('puavomenu_editor.preview.categories_title') %>:</strong> <%= @puavomenu_data[:categories].join(', ') %></li>
        <% end %>
        <% if @puavomenu_data[:menus] %>
        <li><strong><%= t('puavomenu_editor.preview.menus_title') %>:</strong> <%= @puavomenu_data[:menus].join(', ') %></li>
        <% end %>
        <% if @puavomenu_data[:programs] %>
        <li><strong><%= t('puavomenu_editor.preview.programs_title') %>:</strong> <%= @puavomenu_data[:programs].join(', ') %></li>
        <% end %>
      </ul>
    </div>
  </div>
  <% end %>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.description') %></div>
    <div class="value"><%= h @school.description %></div>
  </div>

  <div class="multilineItem">
    <div class="title"><%= t('activeldap.attributes.school.puavoNotes') %></div>
    <div class="value"><%= format_notes(@school.puavoNotes) %></div>
  </div>

<%= end_box %>

<!-- School information -->
<%= start_box t('.system_information') %>
  <table>
    <tr>
      <th><%= t('activeldap.attributes.school.puavoNamePrefix') %></th>
      <td><%= h @school.cn %></td>
    </tr>

    <tr>
      <th><%= t('activeldap.attributes.school.puavoSchoolCode') %></th>
      <td><%= h @school.puavoSchoolCode %></td>
    </tr>

    <tr>
      <th><%= t('activeldap.attributes.school.puavoSchoolOID') %></th>
      <td><%= h @school.puavoSchoolOID %></td>
    </tr>

    <tr>
      <th><%= t('activeldap.attributes.school.puavoExternalId') %></th>
      <td><%= h @school.puavoExternalId %></td>
    </tr>

    <tr>
      <th><%= t('activeldap.attributes.dn') %></th>
      <td><%= h @school.dn.to_s %></td>
    </tr>

    <tr>
      <th><%= t('ldap_create_time') %></th>
      <td><%= h @school['createTimestamp'] %></td>
    </tr>

    <tr>
      <th><%= t('ldap_modify_time') %></th>
      <td><%= h @school['modifyTimestamp'] %></td>
    </tr>
  </table>
<%= end_box %>

<%= start_box t('.statistics') %>
  <!-- Users statistics -->
  <h3><%= t('.users') %> <small><%= t('.total_count', :count => @members.count) %></small></h3>
  <table>
    <% User.puavoEduPersonAffiliation_list.each do |type| %>
    <tr>
      <th><%= t('puavoEduPersonAffiliation_' + type) %></th>
      <td><%= @members.select{ |m| m.last["puavoEduPersonAffiliation"] == [type]}.count %></td>
    </tr>
    <% end %>
  </table>

  <% if @devices_by_type %>
  <!-- Device statistics -->
  <h3><%= t('.devices') %> <small><%= t('.total_count', :count => @devices_by_type.values.sum) %></small></h3>
  <table>
  <% @devices_by_type.each do |device_type, count| %>
    <tr>
      <th><%= device_type %></th>
      <td><%= count %></td>
    </tr>
  <% end %>
  </table>
  <% end %>
<%= end_box %>

<hr>

<%= start_box t('.admin_users'), extraClass="fullWidth" %>
  <% if @school.user_school_admins.empty? %>
  <p class="empty"><%= t('.no_admin_users') %></p>
  <% else %>
  <table>
    <tr class="tableHeading">
      <th><%= t('.name') %></th>
      <th><%= t('.phone') %></th>
      <th><%= t('.mail') %></th>
    </tr>
    <% @school.user_school_admins.each do |user| %>
    <% if user.displayName %>
      <tr>
        <td><%= link_to user.displayName, user_path(@school, user) %></td>
        <td><%= [user.telephoneNumber].delete_if{ |data| data.nil? }.join(", ") %></td>
        <td class="admin_emails"><%= [user.mail].delete_if{ |data| data.nil? }.join(", ") %></td>
      </tr>
    <% end %>
  <% end %>
  </table>
  <% end %>
<%= end_box %>

<hr>

<% if current_user.organisation_owner? %>
<!-- Billing information -->
<%= start_box t('.billing_info'), extraClass="fullWidth" %>
  <% if @school.puavoBillingInfo.nil? || @school.puavoBillingInfo.empty? %>
  <p class="empty"><%= t('.no_billing_info') %></p>
  <% else %>
  <% Array(@school.puavoBillingInfo).each do |billing_info| %>
  <%= h billing_info %><br>
  <% end %>
  <% end %>
<%= end_box %>
<% end %>

<hr>

<!-- Mountpoints -->
<%= render :partial => 'shared/mountpoint', :locals => { :object => @school } %>

<% content_for :page_tools do %>
<%=
toolbar do |tb|
  if @can_edit
    tb.link t('link.edit'),
            href: edit_school_path(@school),
            icon: 'pencil'
  end

  if current_user.organisation_owner?
    tb.dropdown t('link.more'), right: true do |menu|
      menu.link t('link.rename_groups'),
                href: new_rename_groups_path(@school),
                icon: 'exchange',
                owners: true

      menu.link t('link.delete_school'),
                href: school_path(@school),
                confirm: t('general_confirm'),
                method: 'delete',
                icon: 'trash',
                owners: true
    end
  end
end %>
<% end %>

</div>
<hr>

<script>
const ALL_PUAVOCONF = <%= raw JSON.pretty_generate(@full_puavoconf) %>;
</script>

<% content_for :javascript do %>
<%= render partial: 'shared/copy_text', formats: [:js] %>
<%= render partial: 'shared/puavoconf_table', formats: [:js], locals: { select: 'div.dashboard select#changeVisibleConfig', container: 'div.dashboard div.puavoConfOutput', type: 'sch' } %>
<% end %>
