<% content_for :javascript do %>
<script>
const SUPERTABLE_WORKER_FILE = "<%= javascript_path 'supertable3_worker' %>";
let GLOBAL_SUPERTABLE_INSTANCE = null;
</script>

<%= javascript_include_tag 'javascripts/modal_popup' %>
<%= javascript_include_tag 'javascripts/supertable3' %>
<% end %>

<%= render 'shared/supertable_controls' %>
<div class="superTable" id="tableDevices"></div>

<% @is_organisation = false %>
<% @is_servers = false %>

<% content_for :post_load_javascript do %>
<script>
"use strict;"

<%= render partial: 'shared/supertable_translations', formats: [:js] %>
<%= render partial: 'shared/device_tables_common', formats: [:js] %>

const DEFAULT_COLUMNS = ["hn", "display_name", "type", "tags", "mac", "mfer", "model", "serial", "image", "current_image"];
const DEFAULT_SORTING = { column: "hn", dir: ST.SortOrder.ASCENDING };

document.addEventListener("DOMContentLoaded", () => {
    modalPopup.setupGlobalEvents();

    GLOBAL_SUPERTABLE_INSTANCE = new ST.SuperTable(
        // Main container DIV
        document.getElementById("tableDevices"),

        // Settings
        {
            id: "devices",
            locale: "<%= supertable_sorting_locale %>",
            csvPrefix: `<%= LdapOrganisation.current.o %>-<%= @school.cn %>-devices`,
            columnDefinitions: COLUMN_DEFINITIONS,
            columnOrder: COLUMN_ORDER,
            actions: userActions,
            openCallback: openCallback,
            massSelects: MASS_SELECTS,
            massOperations: MASS_OPERATIONS,
            massOperationsEndpoint: "<%= devices_mass_operations_path %>",
            defaultColumns: DEFAULT_COLUMNS,
            defaultSorting: DEFAULT_SORTING,
            filterPresets: FILTER_PRESETS,
            dynamicData: "/devices/<%= @school.id %>/get_school_devices_list",
            preFilterFunction: preprocessDeviceData,
        }
    );
});

</script>
<% end %>

<% if @permit_device_creation %>
<% content_for :page_tools do %>
  <li id="addNewDeviceTitle">
    <%= form_for(@device, url: new_device_path(@school), html: { method: :get }) do |f| %>
      <span><%= t('devices.index.add_new_device') %>:</span>
      <%= select_tag 'device_type', options_for_select(@device_types) %>
      <%= f.submit t('link.add'), id: 'device_submit' %>
    <% end %>
  </li>
<% end %>
<% end %>
