<% table_attributes = {
  behavior: 'dradis-datatable',
  'default-columns': @default_columns.to_json,
  'item-name': 'issue',
  'local-storage-key': local_storage_key,
  'new-tag-path': new_project_tag_path(current_project),
  'can-publish': can?(:publish, current_project),
  'state-icons': local_assigns[:qa_state_icons] ? local_assigns[:qa_state_icons] : state_icons,
  'tags-path': project_tags_path(current_project)
}%>
<% table_attributes.merge!(tags: @tags.map { |t| [t.display_name, t.color, t.name] }.to_json) if local_assigns[:tags] %>
<%= tag.table class: 'table table-striped mb-0', data: table_attributes do %>
  <% cache ['issues-table', @all_columns, issues.map(&:id), @issues.map(&:updated_at).map(&:to_i).sort.last, @tags] do %>
    <thead>
      <tr>
        <th class="no-sort" data-column-visible="false"><span class="visually-hidden">Select</span></th>
        <% @all_columns.each do |column| %>
          <%= content_tag :th, column, class: class_names('reverse-sort': column == 'Tags') %>
        <% end %>
        <th class="no-sort" data-column-visible="false"><span class="visually-hidden">Actions</span></th>
      </tr>
    </thead>
    <tbody>
      <% issues.each do |issue| %>
        <% cache [issue, @all_columns, issue.try(:affected_count), issue.try(:state), 'issues-table', @tags] do %>
          <tr id="issue-<%= issue.id %>" data-tag-url="<%= project_issue_path(current_project, issue) %>">
            <td class="select-checkbox" data-behavior="select-checkbox"></td>
            <% @all_columns.each do |column| %>
              <% sort, display =
                case column
                when 'Title'
                  [issue.title, (link_to(issue.title, [current_project, issue], data: { qa_visible: false } ) + link_to(issue.title, project_qa_issue_path(current_project, issue), class: 'd-none', data: { qa_visible: true }))]
                when 'Tags'
                  [
                    issue.tags.any? ? issue.tags.first.position : false,
                    tag_and_name_for(issue)
                  ]
                when 'Affected'
                  # this count is coming from the SQL finder in the controller
                  [
                    issue.affected_count,
                    issue.affected.map { |node| link_to(node.label, [current_project, node]) }.join("\n").html_safe
                  ]
                when 'Created'
                  [issue.created_at.to_i, local_time_ago(issue.created_at)]
                when 'Created by'
                  [issue.author, issue.author]
                when 'State'
                  [issue.state_before_type_cast, issue.state.humanize]
                when 'Updated'
                  [issue.updated_at.to_i, local_time_ago(issue.updated_at)]
                else
                  [issue.fields.fetch(column, ''), markup(issue.fields.fetch(column, ''))]
                end
              %>
              <%= content_tag :td,
                  display,
                  class: class_names('text-break-spaces': column == 'Affected'),
                  data: {
                    behavior: class_names(tag: column == 'Tags'),
                    sort: sort
                  }
              %>
            <% end %>
            <td class="column-actions">
              <%= link_to edit_project_issue_path(current_project, issue), data: { qa_visible: false } do %>
                <i class="fa-solid fa-pencil"></i> Edit
              <% end %>
              <%= link_to edit_project_qa_issue_path(current_project, issue), class: 'd-none', data: { qa_visible: true } do %>
                <i class="fa-solid fa-pencil"></i> Edit
              <% end %>
              <%= link_to [current_project, issue], method: :delete, data: { confirm: "Are you sure?\n\nProceeding will delete this issue and any associated evidence.", qa_visible: false }, class: 'text-error-hover' do %>
                <i class="fa-solid fa-trash"></i> Delete
              <% end %>
            </td>
          </tr>
        <% end %>
      <% end %>
    </tbody>
  <% end %>
<% end %>
