<!-- view/devices/_hwinfo.html.erb starts -->
<%= start_box t('hwinfo.title'), extraClass='fullWidth' %>
<% unless rawInfo %>
<p class="genericNotice"><%= t('hwinfo.no_info') %></p>
<% else %>
<%
begin
  @info = JSON.parse(rawInfo)
rescue StandardError => e
  @info = nil
  @message = e.to_s
end
%>

<% unless @info %>
<p class="genericError"><%= t('hwinfo.json_failed', :msg => @message) %></p>
<% else %>

<div class="hardwareInfo">

<p class="timestamp"><strong><%= t('hwinfo.received_timestamp') %>:</strong> <%= Time.at(@info['timestamp']).localtime.strftime('%Y-%m-%d %H:%M:%S') %></p>

<% if @school %>
<a href="<%= device_raw_hardware_info_path(@school, @device) %>"><%= t('hwinfo.download_raw') %></a><br><br>
<% else %>
<a href="<%= server_raw_hardware_info_path(@server) %>"><%= t('hwinfo.download_raw') %></a><br><br>
<% end %>

<table class="infoTable">

<!-- General -->

<tr>
  <th class="main"><%= t('hwinfo.image.name') %></th>
  <td colspan="2"><%= @info['this_image'] || '?' %></td>
</tr>

<tr>
  <th class="main"><%= t('hwinfo.image.release') %></th>
  <td colspan="2"><span class="releaseName"><%= @info['this_release'] || '?' %></span></td>
</tr>

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.image.kernel') %></th>
  <td colspan="2"><%= @info['kernelrelease'] || '?' %></td>
</tr>

<% if @info.include?('extra_system_contents') %>

<% extra = @info['extra_system_contents'] %>

<tr>
  <th class="main"><%= t('hwinfo.abitti.title') %></th>
  <td colspan="2">
  <% if extra.include?('Abitti') %>
  <%= extra['Abitti'] %>
  <% else %>
  <%= t('hwinfo.abitti.unknown') %>
  <% end %>
  </td>
</tr>

<% end %>

<!-- Windows infection -->

<% if @info.include?('windows') %>

<tr>
  <th class="main"><%= t('hwinfo.windows.installed') %></th>
  <td colspan="2"><%= @info['windows'].fetch('is_installed', true) ? t('helpers.boolean_true') : t('helpers.boolean_false') %></td>
</tr>

<% end %>

<% if @info.include?('windows_license') && !@info['windows_license'].nil? %>

<tr>
  <th class="main"><%= t('hwinfo.windows.license') %></th>
  <td colspan="2"><%= @info['windows_license'].gsub("\n", '<br>').html_safe %></td>
</tr>

<% end %>

<!-- CPU -->

<tr>
  <th class="main" rowspan="2"><%= t('hwinfo.cpu.title') %></th>
  <th><%= t('hwinfo.cpu.cores') %></th>
  <td><%= @info['processorcount'] || '?' %></td>
</tr>

<tr class="sectionEnd">
  <th><%= t('hwinfo.cpu.name') %></th>
  <td><%= @info['processor0'] || '?' %></td>
</tr>

<!-- Memory -->

<tr>
  <th class="main" rowspan="2"><%= t('hwinfo.memory.title') %></th>
  <th><%= t('hwinfo.memory.total') %></th>
  <td><%= (@info['memorysize_mb'] || '0').to_i %> MiB</td>
</tr>

<tr class="sectionEnd">
  <th><%= t('hwinfo.memory.modules.title') %></th>
  <td>
    <ol>
    <% Array(@info['memory'] || []).each_with_index do |mem, index| %>
      <li>
        <% if mem['size'].to_i > 0 %>
        <strong><%= t('hwinfo.memory.modules.size') %>:</strong> <%= mem['size'] %> MiB
        <strong><%= t('hwinfo.memory.modules.slot') %>:</strong> <%= mem['slot'] %>
        <strong><%= t('hwinfo.memory.modules.product') %>:</strong> <%= mem['product'] %>
        <strong><%= t('hwinfo.memory.modules.vendor') %>:</strong> <%= mem['vendor'] %>
        <% else %>
        <%= t('hwinfo.memory.modules.empty') %>
        <% end %>
      </li>
    <% end %>
    </ol>
  </td>
</tr>

<!-- Storage -->

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.hard_drive.title') %></th>
  <td colspan="2">
    <% if @info['blockdevice_sda_model'] %>
    <%= @info['blockdevice_sda_model'] || '?' %>,
    <%= ((@info['blockdevice_sda_size'].to_f || 0.0) / (1024.0 * 1024.0)).to_i %> MiB
    <% if @info['ssd'] == "1" %>[SSD]<% end %>
    <% else %>
    <%= t('hwinfo.hard_drive.none') %>
    <% end %>
  </td>
</tr>

<!-- SMART data -->
<tr class="sectionEnd">
  <th class="main">SMART</th>
  <td colspan="2">

<% if @info['blockdevice_sda_smart'] %>
<% smart = @info['blockdevice_sda_smart'] %>
  <h3><%= t('hwinfo.smart.general_title') %></h3>
  <table class="smart">
    <tr>
      <th><%= t('hwinfo.smart.model_name') %></th>
      <td><%= smart['model_name'] %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.model_family') %></th>
      <td><%= smart['model_family'] %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.serial_number') %></th>
      <td><%= smart['serial_number'] %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.firmware_version') %></th>
      <td><%= smart['firmware_version'] %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.block_size') %></th>
      <td><%= smart['logical_block_size'] %> / <%= smart['physical_block_size'] %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.wwn') %></th>
      <td>NAA: <%= smart.dig('wwn', 'naa') %><br>OUI: <%= smart.dig('wwn', 'oui') %><br>ID: <%= smart.dig('wwn', 'id') %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.trim_supported') %></th>
      <td><%= smart.dig('trim', 'supported') %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.ata_version') %></th>
      <td><%= smart.dig('ata_version', 'string') %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.sata_version') %></th>
      <td><%= smart.dig('sata_version', 'string') %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.interface_speed') %></th>
      <td><%= smart.dig('interface_speed', 'current', 'string') %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.time') %></th>
      <td><%= smart.dig('local_time', 'asctime') %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.self_test') %></th>
      <td><%= smart.dig('ata_smart_data', 'self_test', 'status', 'string') %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.power_on_time') %></th>
      <td><%= smart.dig('power_on_time', 'hours') %>:<%= smart.dig('power_on_time', 'minutes') %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.power_cycles') %></th>
      <td><%= smart['power_cycle_count'] %></td>
    </tr>
    <tr>
      <th><%= t('hwinfo.smart.temperature') %></th>
      <td><%= smart.dig('temperature', 'current') %></td>
    </tr>
  </table>

  <% if smart['ata_smart_attributes'] %>
  <br>
  <h3><%= t('hwinfo.smart.attributes_title') %></h3>
  <table class="smart">
  <thead>
    <tr>
      <th><%= t('hwinfo.smart.attr_attribute') %></th>
      <th><%= t('hwinfo.smart.attr_value') %></th>
      <th><%= t('hwinfo.smart.attr_worst') %></th>
      <th><%= t('hwinfo.smart.attr_threshold') %></th>
      <th><%= t('hwinfo.smart.attr_raw') %></th>
    </tr>
  </thead>
  <tbody>
  <% Array(smart['ata_smart_attributes']['table']).each do |row| %>
  <tr>
    <th>(<%= row['id'] %>) <%= row['name'] %></th>
    <td><%= row['value'] %></td>
    <td><%= row['worst'] %></td>
    <td><%= row['thresh'] %></td>
    <td><%= row.dig('raw', 'string') %></td>
  </tr>
  <% end %>
  </tbody>
  </table>
  <% end %>

  <% if smart['nvme_smart_health_information_log'] %>
  <% health = smart['nvme_smart_health_information_log'] %>
  <br>
  <h3><%= t('hwinfo.smart.nvme_title') %></h3>
  <table class="smart">
  <thead>
    <tr>
      <th><%= t('hwinfo.smart.nvme_attr') %></th>
      <th><%= t('hwinfo.smart.nvme_value') %></th>
    </tr>
  </thead>
  <tbody>
  <% health.keys.each do |attr| %>
  <tr>
    <th><%= attr %></th>
    <td><%= health[attr] %></td>
  </tr>
  <% end %>
  </tbody>
  </table>
  <% end %>
<% else %>
  <%= t('hwinfo.smart.no_data') %>
<% end %>
  </td>
</tr>

<!-- Free disk space on various partitions -->
<% if @info['free_space'] %>
<% partitions = @info['free_space'].map{ |key, value| [key, value] } %>

<% unless partitions.empty? %>

<tr>
  <th class="main" rowspan="<%= partitions.size %>"><%= t('hwinfo.df.title') %></th>
  <th><%= partitions[0][0] %></th>
  <td><%= (partitions[0][1] / (1024 * 1024)).to_i %></td>
</tr>

<% partitions.shift %>

<% partitions.each do |part, free| %>
<tr>
  <th><%= part %></th>
  <td><%= (free / (1024 * 1024)).to_i %></td>
</tr>
<% end %>

<% end %>

<% end %>

<!-- UEFI installation -->
<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.uefi.title') %></th>
  <td colspan="2"><%= @info.has_key?('uefi') ? translate_boolean_value(@info['uefi']) : '?' %></td>
</tr>

<!-- WiFi -->

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.wifi.title') %></th>
  <td colspan="2"><%= @info['wifi'] || '?' %></td>
</tr>

<!-- Product -->

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.product.title') %></th>
  <td colspan="2"><%= @info['productname'] || '?' %></td>
</tr>

<!-- BIOS -->

<tr>
  <th class="main" rowspan="3"><%= t('hwinfo.bios.title') %></th>
  <th><%= t('hwinfo.bios.vendor') %></th>
  <td><%= @info['bios_vendor'] || '?' %></td>
</tr>

<tr>
  <th><%= t('hwinfo.bios.version') %></th>
  <td><%= @info['bios_version'] || '?' %></td>
</tr>

<tr class="sectionEnd">
  <th><%= t('hwinfo.bios.date') %></th>
  <td><%= @info['bios_release_date'] || '?' %></td>
</tr>

<!-- Serial numbers -->

<tr>
  <th class="main" rowspan="3"><%= t('hwinfo.serial.title') %></th>
  <th><%= t('hwinfo.serial.device') %></th>
  <td><%= @info['serialnumber'] || '?' %></td>
</tr>

<tr>
  <th><%= t('hwinfo.serial.mainboard') %></th>
  <td><%= @info['boardserialnumber'] || '?' %></td>
</tr>

<tr class="sectionEnd">
  <th>SKU</th>
  <td><%= @info['sku'] || '?' %></td>
</tr>

<!-- lspci values -->

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.lspci.title') %></th>
  <td colspan="2">
    <code>
    <% Array(@info['lspci_values'] || []).each do |lspci| %>
    <%= lspci %><br>
    <% end %>
    </code>
  </td>
</tr>

<!-- lsusb values -->

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.lsusb.title') %></th>
  <td colspan="2">
    <code>
    <% Array(@info['lsusb_values'] || []).each do |lsusb| %>
    <%= lsusb %><br>
    <% end %>
    </code>
  </td>
</tr>

<!-- xrandr output -->

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.xrandr.title') %></th>
  <% if !@info.include?('xrandr') || @info['xrandr'].empty? %>
  <td colspan="2"><%= t('hwinfo.xrandr.no_xrandr') %>
  <% else %>
  <td colspan="2">
    <pre>
<% Array(@info['xrandr']).each do |xr| %><%= xr %><% end %></pre>
  <% end %>
  </td>
</tr>

<!-- battry info -->

<% if @info.include?('battery') %>

<% battery = @info['battery'] %>

<tr>
  <th class="main" rowspan="7"><%= t('hwinfo.battery.title') %></th>
  <th><%= t('hwinfo.battery.vendor') %></th>
  <td><%= battery['vendor'] || '?' %></td>
</tr>

<tr>
  <th><%= t('hwinfo.battery.serial') %></th>
  <td><%= battery['serial'] || '?' %></td>
</tr>

<tr>
  <th><%= t('hwinfo.battery.state') %></th>
  <td><%= battery['state'] || '?' %></td>
</tr>

<tr>
  <th><%= t('hwinfo.battery.energy_full') %></th>
  <td><%= battery['energy-full-design'] || '?' %></td>
</tr>

<tr>
  <th><%= t('hwinfo.battery.voltage') %></th>
  <td><%= battery['voltage'] || '?' %></td>
</tr>

<tr>
  <th><%= t('hwinfo.battery.capacity') %></th>
  <td><%= battery['capacity'] || '?' %></td>
</tr>

<tr class="sectionEnd">
  <th><%= t('hwinfo.battery.percentage') %></th>
  <td><%= battery['percentage'] || '?' %></td>
</tr>

<% else %>

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.battery.title') %></th>
  <td colspan="2"><%= t('hwinfo.battery.no_battery') %></td>
</tr>

<% end %>

<!-- local puavo-conf values -->

<% if @info.include?('puavoconf') && @info['puavoconf'].include?('local') %>

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.local_puavoconf.title') %></th>
  <td colspan="2">
    <table class="smart">
      <thead>
        <tr>
          <th><%= t('hwinfo.local_puavoconf.key') %></th>
          <th><%= t('hwinfo.local_puavoconf.value') %></th>
        </tr>
      </thead>
      <tbody>
      <% @info['puavoconf']['local'].each do |key, value| %>
      <tr><th><%= key %></th><td><code><%= value %></code></th></tr>
      <% end %>
      </tbody>
    </table>
  </td>
</tr>

<% end %>

<!-- installed puavo-pkg packages -->

<% if @info.include?('puavopkgs') %>

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.puavopkg.title') %></th>
  <td colspan="2">
    <p class="padding-0"><%= t('hwinfo.puavopkg.totals', num_packages: @info['puavopkgs'].fetch('installed', []).count, disk_space: @info['puavopkgs'].fetch('diskspace_used', '?')) %></p>

    <% packages = @info['puavopkgs'].fetch('installed', {}) %>
    <% unless packages.empty? %>
    <table class="smart margin-top-10px">
      <thead>
        <tr>
          <th><%= t('hwinfo.puavopkg.package_name') %></th>
          <th><%= t('hwinfo.puavopkg.version') %></th>
          <th><%= t('hwinfo.puavopkg.package_id') %></th>
          <th><%= t('hwinfo.puavopkg.upstream_hash') %></th>
        </tr>
      </thead>
      <tbody>
      <% packages.keys.sort.each do |key| %>
      <% value = packages[key] %>
      <tr>
        <th><%= key %></th>
        <td><%= value.fetch('version', nil) %></td>
        <td><code><%= value.fetch('package-id', nil) %></code></td>
        <td><code><%= value.fetch('upstream_pack', nil) %></code></td>
      </tr>
      <% end %>
      </tbody>
    </table>
    <% end %>
  </td>
</tr>

<% end %>

<!-- last login -->

<% if @info.include?('logins') %>

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.logins.title') %></th>
  <td colspan="2">
    <% last = @info['logins'].fetch('last', {}) %>
    <% if last['uid'].nil? || last['timestamp'].nil? %>
    <p><%= t('hwinfo.logins.last_login', username: '?', at: '?') %></p>
    <% else %>
    <p><%= t('hwinfo.logins.last_login', username: last['uid'], at: (Time.parse(last['timestamp']).localtime.strftime('%Y-%m-%d %H:%M:%S') rescue "?")) %></p>
    <% end %>
  </td>
</tr>

<% end %>

<!-- uptime -->

<% if @info.include?('uptime') %>

<tr class="sectionEnd">
  <th class="main"><%= t('hwinfo.uptime.title') %></th>
  <td colspan="2"><%= @info['uptime'] %>s (<%= format_uptime(@info['uptime']) %>)</td>
</tr>

<% end %>

</table>

</div>

<% end %>

<% end %>
<%= end_box %>
<!-- view/devices/_hwinfo.html.erb ends -->
