<% cache ['tags-table', @all_columns, @tags] do %>
  <table class="table table-striped mb-0"
    data-behavior="dradis-datatable"
    data-default-columns="<%= @default_columns.to_json %>"
    data-item-name="tags"
    data-local-storage-key="project.ce.<%= dom_id(current_project) %>.tags_datatable">
    <thead>
      <tr>
        <% @all_columns.each do |column| %>
          <th class="no-sort" data-column-visible="<%= column != 'Sort' %>">
            <span class="<%= class_names('visually-hidden': column == 'Sort'  )%>"><%= column %></span>
          </th>
        <% end %>
        <th class="no-sort" data-column-visible="false"><span class="visually-hidden">Actions</span></th>
      </tr>
    </thead>
    <tbody data-behavior='ui-sortable', data-sort-url="<%= sort_project_tags_path(current_project) %>">
      <% @tags.each do |tag| %>
        <% cache [tag, @all_columns, 'tags-table'] do %>
          <tr id="tag-<%= tag.id %>">
            <% @all_columns.each do |column| %>
              <%
               content =
              case column
              when 'Sort'
                  content_tag(:i, nil, class: 'fa-solid fa-grip-vertical w-100') + content_tag(:span, 'Change sort order', class: 'visually-hidden')
              when 'Name'
                  content_tag(:span, class: 'tag', style: "color: #{tag.color}") do
                    content_tag(:i, nil, class: 'fa-solid fa-tag fa-fw me-1') + tag.display_name
                  end
              when 'Color'
                content_tag(:span, tag.color, style:  "color: #{tag.color}")
              when 'Created'
                local_time_ago(tag.created_at)
              when 'Updated'
                local_time_ago(tag.updated_at)
              end
              %>
              <%= content_tag :td, content, class: class_names('text-secondary text-center': column == 'Sort') %>
            <% end  %>
            <td class="column-actions">
              <%= link_to edit_project_tag_path(current_project, tag), remote: true do %>
                <i class="fa-solid fa-pencil"></i> Edit
              <% end %>
              <%= link_to project_tag_path(current_project, tag), method: :delete, data: { confirm: "Are you sure?\n\nProceeding will delete this tag. Any issues with this tag will be untagged." }, class: 'text-error-hover' do %>
                <i class="fa-solid fa-trash"></i> Delete
              <% end %>
            </td>
          </tr>
        <% end %>
      <% end %>
    </tbody>
  </table>
<% end %>
