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

<div class="basicInfo">
  <div class="basicInfoImage">
    <%= image_tag(default_image_or_user_image_path(
                  image_user_path(@user.primary_school.id, @user),
                  @user), class: 'image', skip_pipeline: true ) %>
  </div>

  <div class="basicInfoDetails">
    <h1><%= h @user.displayName %></h1>

    <% if @user.puavoMFAEnabled %>
    <p class="infoNotice"><%= t('.mfa_enabled') %></p>
    <% end %>

    <% if @user_is_owner %>
    <p class="infoNotice"><%= t('.user_is_an_organisation_owner') %></p>
    <% end %>

    <% unless @admin_in_schools.empty? %>
    <% @admin_in_schools.each do |s| %>
    <p class="infoNotice"><%= t('.user_is_a_school_admin', :school_name => s.displayName) %></p>
    <% end %>
    <% end %>

    <% unless @admin_permissions.empty? %>
    <p class="infoTitle infoNotice"><%= t('.admin_permissions.title') %></p>
    <p class="infoValue infoNotice"><%= @admin_permissions.collect { |p| t(".admin_permissions.#{p.to_s}") }.join(', ') %></p>
    <% end %>

    <% if @user.puavoLocked == true %>
    <p class="infoWarn"><%= t('.user_is_locked') %></p>
    <% end %>

    <% unless @user.puavoRemovalRequestTime.nil? %>
    <p class="infoWarn"><%= t('.user_is_marked_for_deletion') %></p>
    <% end %>

    <% if @user.puavoDoNotDelete %>
    <p class="infoNotice"><%= t('.user_deletion_prevented') %></p>
    <% end %>

    <p class="infoTitle"><%= t('activeldap.attributes.user.uid') %></p>
    <p class="infoValue"><%= h @user.uid %></p>

    <p class="infoTitle"><%= t('activeldap.attributes.user.puavoEduPersonAffiliation') %></p>
    <p class="infoValue"><%= Array(@user.puavoEduPersonAffiliation).map { |value| t('puavoEduPersonAffiliation_' + value) }.join(', ') %></p>

    <p class="infoTitle"><%= t('activeldap.attributes.user.puavoLearnerId') %></p>
    <p class="infoValue"><%= h @user.puavoLearnerId || t('missing_data') %></p>

    <% if @user.puavoEduPersonAffiliation.include?('student') %>
    <p class="infoTitle"><%= t('activeldap.attributes.user.teaching_group') %></p>
    <p class="infoValue"><%= h @user.teaching_group&.displayName || t('missing_data') %></p>
    <p class="infoTitle"><%= t('activeldap.attributes.user.year_class') %></p>
    <p class="infoValue"><%= h @user.year_class&.displayName || t('missing_data') %></p>
    <% end %>

    <% if @mpass_materials_charge %>
    <p class="infoTitle"><%= t('activeldap.attributes.user.mpass_materials_charge') %></p>
    <p class="infoValue"><%= h @mpass_materials_charge %></p>
    <% end %>

    <% unless @user_schools.empty? %>
    <p class="infoTitle"><%= t ('.schools') %></p>
    <ul class="infoValue" id="schoolList">
      <% if @viewer_is_an_owner %>
      <% @user_schools.each do |s| %>
        <li><%= link_to(s.displayName, school_path(s)) %> <%= s.dn == @primary_school_dn ? t('.primary_school') : '' %></li>
      <% end %>
      <% else %>
      <% @user_schools.each do |s| %>
        <li><%= s.displayName %></li>
      <% end %>
      <% end %>
    </ul>
    <% end %>
  </div>
</div>

<%= start_box t('.contact_information') %>
<table id="contactInformation">
  <tr>
    <th><%= t('activeldap.attributes.user.mail') %></th>
    <td class="noWrap">
      <% if @user.mail %>
      <ul class="no-list-bullets margin-0 padding-0">

      <% Array(@user.mail || []).each do |e| %>
        <% primary = (e == @user.puavoPrimaryEmail) %>
        <li>
        <% if primary %><strong><% end %>
        <%= h e %>
        <%
        attr = []
        attr << t('.verified_email') if @verified_addresses.include?(e)
        attr << t('.primary_email') if primary
        %>
        <% unless attr.empty? %> (<%= attr.join(', ') %>)<% end %>
        <% if primary %></strong><% end %>
        </li>
      <% end %>
      </ul>
      <% end %>
    </td>
  </tr>

  <tr>
    <th><%= t('activeldap.attributes.user.telephoneNumber') %></th>
    <td class="noWrap"><%= h Array(@user.telephoneNumber).map{ |telephoneNumber| telephoneNumber }.join("<br>").html_safe %></td>
  </tr>
</table>
<%= end_box %>

<%= start_box t('.system_information') %>
<table>
  <tr>
    <th><%= t('activeldap.attributes.user.uidNumber') %></th>
    <td><%= h @user.uidNumber %></td>
  </tr>

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

  <tr>
    <th>UUID</th>
    <td><code><%= h @user.puavoUuid %></code></td>
  </tr>

  <tr>
    <th><%= t('users.show.groups_by_schools') %></th>
    <td class="noWrap">
      <ul class="userShowGroups" id="groups_by_roles">
      <% @user_groups.each do |s| %>
        <li><%= s[:accessible] ? link_to(s[:school].displayName, school_path(s[:school])) : s[:school].displayName %>:<ul>
          <% s[:groups].each do |g| %>
            <li>
              <%= s[:accessible] ? link_to(g.displayName, group_path(g.school.puavoId, g)) : g.displayName %>
              <% if g.puavoEduGroupType %>
              <small>(<%= h humanize_group_type(g.puavoEduGroupType) %>)</small>
              <% end %>
            </li>
          <% end %>
        </ul></li>
      <% end %>
      </ul>
    </td>
  </tr>

  <tr>
    <th><%= t('activeldap.attributes.user.puavoLocale') %></th>
    <td><%= h language_by_locale(@user.puavoLocale) %></td>
  </tr>

  <tr>
    <th><%= t('activeldap.attributes.user.puavoAllowRemoteAccess') %></th>
    <td><%= h t("activeldap.attributes.user.puavoAllowRemoteAccess_#{@user.puavoAllowRemoteAccess}") %></td>
  </tr>

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

  <% if current_user.organisation_owner? %>
  <tr>
    <th><%= t('activeldap.attributes.user.puavoSshPublicKey') %></th>
    <td><%= fingerprint @user.puavoSshPublicKey %></td>
  </tr>
  <% end %>

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

  <tr>
    <th><%= t('activeldap.attributes.ldap_organisation.puavoNotes')  %></th>
    <td class="noWrap"><%= format_notes(@user.puavoNotes) %></td>
  </tr>

  <tr>
    <th><%= t('.user_devices') %></th>
    <td class="value_td">
      <% @user_devices.each do |device| %>
      <%= link_to device.puavoHostname, device_path(device.school_id, device) %><br />
      <% end %>
    </td>
  </tr>

  <tr>
    <th><%= t('.user_licenses') %></th>
    <td>
      <% if @licenses %>
      <% if @licenses_ok %>
      <ul>
        <% @licenses.keys.sort.each do |l| %>
        <li><%= l %>: <%= @licenses[l] %></li>
        <% end %>
      </ul>
      <% else %>
        <p class="missingData"><%= t('activeldap.errors.messages.user.invalid_license_data') %>: <code><%= h @licenses %></code></p>
      <% end %>
      <% end %>
    </td>
  </tr>

  <tr>
    <th><%= t('ldap_create_time') %></th>
    <td class="noWrap"><%= h @user['createTimestamp'] %></td>
  </tr>

  <tr>
    <th><%= t('ldap_modify_time')  %></th>
    <td class="noWrap"><%= h @user['modifyTimestamp'] %></td>
  </tr>

  <% if @user.puavoRemovalRequestTime %>
  <tr>
    <th><%= t('.marked_for_removal') %></th>
    <td class="userMarkedForDeletion noWrap"><%= h @user.puavoRemovalRequestTime %></td>
  </tr>
  <% end %>
</table>
<%= end_box %>

<hr>

<% content_for :tools do %>
<ul>
  <li>
    <%= link_to edit_user_path(@school, @user), class: 'btn' do %>
      <i class='icon-pencil'></i><%=t('link.edit')%>
    <% end %>
  </li>

  <li class="haveDropdown">
    <span class="btn"><i class='icon-collapse'></i><%= t('link.more') %></span>
    <div class="dropdown dropRight">
      <% if current_user.organisation_owner? %>
       <ul>
        <li class="ownersOnly">
           <% if school_has_integration?(@organisation_name, @school.id.to_i, 'primus') %>
          <%= link_to change_schools_path(@school, @user), data: { confirm: t('.primus_school_change_warning') } do %>
             <i class='icon-exchange'></i><%= t('.change_school') %>
           <% end %>
           <% else %>
          <%= link_to change_schools_path(@school, @user) do %>
             <i class='icon-exchange'></i><%= t('.change_school') %>
           <% end %>
           <% end %>
         </li>
       </ul>
      <% end %>

      <% unless @user.puavoDoNotDelete %>
      <% if current_user.organisation_owner? %>
      <ul>
        <li class="ownersOnly">
          <%= link_to prevent_deletion_path(@school, @user), data: { confirm: t('.prevent_removal_confirm') } do %>
            <i class='icon-block'></i><%= t('.prevent_removal') %>
          <% end %>
        </li>
      </ul>
      <% end %>
      <% end %>

      <% if current_user.organisation_owner? && Array(@user.puavoEduPersonAffiliation || []).include?('admin') %>
      <ul>
        <li class="ownersOnly">
          <%= link_to edit_admin_permissions_path(@school, @user) do %>
            <i class='icon-sliders'></i><%= t('.edit_admin_permissions') %>
          <% end %>
        </li>
      </ul>
      <% end %>

      <% if current_user.organisation_owner? && Array(@user.mail || []).count > 0 %>
      <ul>
        <li class="ownersOnly">
          <%= link_to request_password_reset_path(@school, @user) do %>
            <i class='icon-mail-alt'></i><%= t('.request_password_reset') %>
          <% end %>
        </li>
      </ul>
      <% end %>

      <% if @have_sso_sessions %>
      <ul>
        <li class="ownersOnly">
          <%= link_to reset_sso_session_path(@school, @user), data: { confirm: t('general_confirm') } do %>
            <i class='icon-key'></i><%= t('.reset_sso_session') %>
          <% end %>
        </li>
      </ul>
      <% end %>

      <ul>
        <li>
          <% if @user.puavoLocked == false || @user.puavoLocked.nil? %>
          <%= link_to lock_user_path(@school, @user) do %>
            <i class='icon-lock'></i><%= t('.lock') %>
          <% end %>
          <% elsif @user.puavoLocked == true %>
          <%= link_to unlock_user_path(@school, @user) do %>
            <i class='icon-lock-open'></i><%= t('.unlock') %>
          <% end %>
          <% end %>
        </li>
      </ul>

      <% if @user.puavoRemovalRequestTime %>
      <ul>
        <li>
          <%= link_to unmark_user_for_deletion_path(@school, @user) do %>
            <i class='icon-flag-empty'></i><%= t('.unmark_for_removal') %>
          <% end %>
        </li>
      </ul>
      <% else %>
      <% if !@own_page && !@user.puavoDoNotDelete %>
      <ul>
        <li>
          <%= link_to mark_user_for_deletion_path(@school, @user) do %>
            <i class='icon-flag'></i><%= t('.mark_for_removal') %>
          <% end %>
        </li>
      </ul>
      <% end %>
      <% end %>

      <% if !@own_page && @permit_user_deletion %>
      <% unless @user.puavoDoNotDelete %>
      <ul>
        <li>
          <%
          if @user_is_owner || !@admin_in_schools.empty?
            confirm_msg = t('destroy_confirm_admin', object_name: @user.displayName)
          else
            confirm_msg = t('destroy_confirm', object_name: @user.displayName)
          end

          if !@synchronised_deletions.empty? && @synchronised_deletions.include?(@user.primary_school.id.to_i)
            confirm_msg += "\n\n"
            confirm_msg += t('users.show.deletion_sync_warning', :systems => @synchronised_deletions[@user.primary_school.id.to_i].join(', '))
          end

          %>
          <%= link_to user_path(@school, @user.puavoId), data: { confirm: confirm_msg }, method: :delete do %>
            <i class='icon-trash'></i><%= t('link.delete_user') %>
          <% end %>
        </li>
      </ul>
      <% end %>
      <% end %>

    </div>
  </li>
</ul>
<% end %>
