<% class_name = items.first.class.name.underscore %>
<% default_columns = instance_variable_get("@default_#{class_name}_columns") %>

<% cache ["#{dom_class(items.first).pluralize}-table", columns, items.map(&:id), items.map(&:updated_at).map(&:to_i).sort.last] do %>
  <div class="table-wrapper">
    <table class="table table-striped"
      data-behavior="dradis-datatable"
      data-default-columns="<%= default_columns.to_json %>"
      data-item-name="<%= class_name %>"
      data-local-storage-key="project.ce.<%= dom_id(current_project) %>.<%= class_name.pluralize %>_datatable"
      >
      <thead>
        <tr>
          <th class="no-sort" data-column-visible="false"><span class="visually-hidden">Select</span></th>
          <% columns.each do |column| %>
            <th><%= column %></th>
          <% end %>
          <th class="no-sort" data-column-visible="false"><span class="visually-hidden">Actions</span></th>
        </tr>
      </thead>
      <tbody>
        <% items.each do |item| %>
          <tr id="<%= class_name %>-<%= item.id %>">
            <td class="select-checkbox" data-behavior="select-checkbox"></td>
            <% columns.each do |column| %>
              <%
              sort, display =
              case column
              when 'Label'
                [item.node.label, link_to(item.node.label, [current_project, @node, item])]
              when 'Title'
                [
                  item.title,
                  link_to([@node.project, @node, item]) do
                    icon = ''
                    if item.is_a?(Evidence)
                      icon =
                        colored_icon_for_model(item, 'fa-flag', 'list-item-icon')
                    end
                    icon + ' ' + item.title
                  end
                ]
              when 'Created'
                [item.created_at.to_i, local_time_ago(item.created_at)]
              when 'Created by'
                [item.author, item.author]
              when 'Updated'
                [item.updated_at.to_i, local_time_ago(item.updated_at)]
              else
                [item.fields.fetch(column, ''), markup(item.fields.fetch(column, ''))]
              end
              %>
              <td data-sort="<%= sort %>"><%= display %></td>
            <% end %>
            <td class="column-actions">
              <%= link_to [:edit, @node.project, @node, item] do %>
                <i class="fa-solid fa-pencil"></i> Edit
              <% end %>
              <%= link_to [@node.project, @node, item], method: :delete, data: { confirm: "Are you sure?\n\nProceeding will delete this item from the associated node." }, class: 'text-error' do %>
                <i class="fa-solid fa-trash"></i> Delete
              <% end %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
<% end %>
