<% 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| %>
    <% if s[:valid] %>
    <p class="infoNotice"><%= t('.user_is_a_school_admin', school_name: s[:name]) %></p>
    <% else %>
    <p class="infoError"><%= t('.user_is_a_school_admin_error', school_dn: s[:dn]) %></p>
    <% end %>
    <% 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 %>

    <% unless @teacher_permissions.empty? %>
    <p class="infoTitle infoNotice"><%= t('.teacher_permissions.title') %></p>
    <p class="infoValue infoNotice"><%= @teacher_permissions.collect { |p| t(".teacher_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.to_s.split(',').join(',<wbr>')).html_safe %></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.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>

  <% if @citrix_license %>
  <tr>
    <th><%= t('.user_citrix_license.title') %></th>
    <td>
      <table>
        <tr>
          <th><%= t('.user_citrix_license.created') %></th>
          <td><%= @citrix_license.include?('created') ? Time.parse(@citrix_license['created']).localtime.strftime('%Y-%m-%d %H:%M:%S') : '?' %></td>
        </tr>
        <tr>
          <th><%= t('.user_citrix_license.last_used') %></th>
          <td><%= @citrix_license.include?('last_used') ? Time.parse(@citrix_license['last_used']).localtime.strftime('%Y-%m-%d %H:%M:%S') : '?' %></td>
        </tr>
        <tr>
          <th><%= t('.user_citrix_license.first_name') %></th>
          <td><%= h @citrix_license['first_name'] %></td>
        </tr>
        <tr>
          <th><%= t('.user_citrix_license.last_name') %></th>
          <td><%= h @citrix_license['last_name'] %></td>
        </tr>
        <tr>
          <th><%= t('.user_citrix_license.username') %></th>
          <td><%= h @citrix_license['username'] %></td>
        </tr>
      </table>
    </td>
  </tr>
  <% end %>

  <tr>
    <th><%= t('last_kerberos_auth_date') %></th>
    <td class="noWrap"><%= h @user.kerberos_last_successful_auth %></td>
  </tr>

  <tr>
    <th><%= t('last_ldap_auth_date') %></th>
    <td class="noWrap"><%= h @user['authTimestamp'] %></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.puavoEduPersonAccountExpirationTime %>
  <% if Time.now >= @user.puavoEduPersonAccountExpirationTime %>
  <tr class="invalidData">
  <% else %>
  <tr>
  <% end %>
    <th><%= t('activeldap.attributes.user.puavoEduPersonAccountExpirationTime') %></th>
    <td class="noWrap"><%= h @user.puavoEduPersonAccountExpirationTime.localtime.strftime('%Y-%m-%d %H:%M:%S') %></td>
  </tr>
  <% end %>

  <% 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 :page_tools do %>
<%=
toolbar do |tb|
  tb.link t('link.edit'),
          href: edit_user_path(@school, @user),
          icon: 'pencil'

  tb.dropdown t('link.more'), right: true do |menu|
    if current_user.organisation_owner?
      menu.link t('.change_school'),
                href: change_schools_path(@school, @user),
                icon: 'exchange',
                confirm: school_has_integration?(@organisation_name, @school.id.to_i, 'primus') ? t('.primus_school_change_warning') : nil,
                owners: true

      unless @user.puavoDoNotDelete
        menu.link t('.prevent_removal'),
                  href: prevent_deletion_path(@school, @user),
                  icon: 'block',
                  confirm: t('.prevent_removal_confirm'),
                  owners: true
      end

      menu.separator

      if @is_admin
        menu.link t('.edit_admin_permissions'),
                  href: edit_admin_permissions_path(@school, @user),
                  icon: 'sliders',
                  owners: true
      end

      if @is_teacher
        menu.link t('.edit_teacher_permissions'),
                  href: edit_teacher_permissions_path(@school, @user),
                  icon: 'sliders',
                  owners: true
      end

      if Array(@user.mail || []).count > 0
        menu.link t('.request_password_reset'),
                  href: request_password_reset_path(@school, @user),
                  icon: 'mail-alt',
                  confirm: t('general_confirm'),
                  owners: true

        if @have_sso_sessions
          menu.link t('.reset_sso_session'),
                    href: reset_sso_session_path(@school, @user),
                    icon: 'mail-key',
                    owners: true
        end
      end
    end

    menu.separator

    if @user.puavoLocked == false || @user.puavoLocked.nil?
      menu.link t('.lock'),
                href: lock_user_path(@school, @user),
                icon: 'lock'
    elsif @user.puavoLocked == true
      menu.link t('.unlock'),
                href: unlock_user_path(@school, @user),
                icon: 'lock-open'
    end

    if @user.puavoEduPersonAccountExpirationTime && @permit_edit_expiration_times
      menu.link t('.remove_expiration_time'),
                href: remove_expiration_time_path(@school, @user),
                icon: 'flag-empty'
    end

    if @user.puavoRemovalRequestTime
      menu.link t('.unmark_for_removal'),
                href: unmark_user_for_deletion_path(@school, @user),
                icon: 'flag-empty'
    elsif !@own_page && !@user.puavoDoNotDelete
      menu.link t('.mark_for_removal'),
                href: mark_user_for_deletion_path(@school, @user),
                icon: 'flag'
    end

    menu.separator

    if @permit_user_deletion && !@user.puavoDoNotDelete && !@own_page
      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

      menu.link t('link.delete_user'),
                href: user_path(@school, @user),
                confirm: confirm_msg,
                method: 'delete',
                icon: 'trash'
    end
  end
end %>
<% end %>
