<% content_for :title, 'Import Issues' %>

<% content_for :sidebar do %>
  <%= render 'sidebar'%>
<% end %>

<ol class="breadcrumb">
  <li class="breadcrumb-item">
    <%= link_to current_project.name, project_path(current_project) %>
  </li>
  <li class="breadcrumb-item active">
    Import Issues
  </li>
</ol>

<!-- Main content -->
<div class="content-container">
  <div id="issue-viewer">
    <div class="results">
      <div class="page page-none">

        <div class="header">
          <div class="header-inner">
            <h4 class="header-underline">Search results</h4>
          </div>
        </div>

        <% if @import_issues.any? %>
          <table class="table table-striped mb-0"
            data-behavior="dradis-datatable"
            data-default-columns="<%= @default_columns.to_json %>"
            data-item-name="issue"
            data-local-storage-key="project.ce.<%= dom_id(current_project) %>.issues_import_datatable"
          >
            <thead>
              <tr>
                <% @all_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>
              <% @import_issues.each do |issue| %>
                <tr id="issue-<%= issue.id %>">
                  <% @all_columns.each do |column| %>
                    <%
                    sort, display =
                    case column
                    when 'Title'
                      [issue.title, issue.title]
                    when 'Result Tags'
                      [issue_tags(issue), issue_tags(issue)]
                    when 'Result State'
                      [Issue.states[issue.state], issue.state.humanize]
                    else
                      [issue.fields.fetch(column, ''), markup(issue.fields.fetch(column, ''))]
                    end
                    %>
                    <%= content_tag :td,
                        display,
                        class: class_names('text-break-spaces': column == 'Affected', 'text-muted': issue.state.present? && !issue.published?),
                        data: {
                          behavior: class_names(tag: column == 'Tags'),
                          sort: sort
                        }
                    %>
                  <% end  %>
                  <td class="column-actions">
                    <%# Display the Add Issue link for plugins that don't provide a state %>
                    <%# For plugins that do provide a state show only for published %>
                    <% if issue.state.nil? || issue.published? %>
                      <% default_state = issue.state ? 'published' : 'draft' %>
                      <div class="btn-group btn-states" data-behavior="btn-states">
                        <%= link_to project_issues_path(current_project, format: :js), class: 'btn btn-primary', data: { entry_id: issue.id, text: issue.text, state: default_state, behavior: 'add-issue' } do %>
                          <i class="fa-solid fa-plus"></i> Add issue (<span data-behavior="state-button"><%= default_state.humanize %></span>)
                        <% end %>
                        <a href="#" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                          <span class="visually-hidden">Toggle Dropdown</span>
                        </a>
                        <div class="dropdown-menu">
                          <%= collection_radio_buttons(issue, :state, Issue.states, :first, :first) do |b| %>
                            <span>
                              <%= b.label class: 'state' do %>
                                <%= b.radio_button(class: 'd-none', data: { behavior: 'state-radio' }, checked: b.value == default_state) %>
                                <i class="fa-solid fa-check fa-fw"></i>
                                <div class="state-label">
                                  <p data-behavior="state-label"><%= b.text.humanize %></p>
                                  <% case b.value %>
                                  <% when 'draft' %>
                                    <span>Still not ready for review or the report.</span>
                                  <% when 'ready_for_review' %>
                                    <span>All done on this one, ready for QA.</span>
                                  <% when 'published' %>
                                    <span>Content is final, ready for the report.</span>
                                  <% end %>
                                </div>
                              <% end %>
                            </span>
                          <% end %>
                        </div>
                      </div>
                    <% else %>
                      <div class="btn-group" title="This result is still in a <%= issue.state.humanize.titleize %> state. Publish it in the library before importing to the project." data-bs-toggle="tooltip">
                        <a href="javascript:void(0)" class="btn btn-primary disabled" disabled="disabled">
                          <i class="fa-solid fa-plus"></i> Add Issue (Published)
                        </a>
                        <a
                          href="javascript:void(0)"
                          class="btn btn-primary dropdown-toggle dropdown-toggle-split disabled"
                          disabled="disabled"
                          role="presentation"
                        ></a>
                      </div>
                    <% end %>
                  </td>
                </tr>
              <% end %>
            </tbody>
          </table>
        <% else %>

          <div class="empty-state mt-3">
            <div class="empty-state-icon">
              <%= image_tag "icons/#{rand(9)}.svg" %>
            </div>
            <div class="empty-state-body">
              <div class="container">
                <h4 class="empty-state-title">This query yielded no results</h4>
                <div class="empty-state-actions">
                  <a href="https://dradis.com/support/guides/issuelibrary/add_to_project.html" class="empty-state-docs-link" target="_blank">More about importing issues</a>
                </div>
              </div>
            </div>
          </div>
        <% end %>
      </div>
    </div>
  </div>
</div>
