<% content_for :title, 'Merge issues' %>

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

<% content_for :breadcrumbs do %>
  <nav>
    <ol class="breadcrumb">
      <li class="breadcrumb-item">
        <%= link_to current_project.name, project_path(current_project) %>
      </li>
      <li class="breadcrumb-item">
        <%= link_to 'All issues', project_issues_path(current_project) %>
      </li>
      <li class="breadcrumb-item active">Merge issues</li>
    </ol>
  </nav>
<% end %>

<div class="content-container" id="issues_editor">
  <div class="note-text-inner">
      <h4 class="header-underline">You're merging <%= @issues.count %> Issues into a target Issue</h4>

      <%= form_tag project_merge_index_path(current_project) do %>
        <p>
          Select an existing Issue to merge into, or create a new one.
        </p>

        <p>
          Evidence from the original Issues will be added to the target Issue.
          Then, the original Issues will be moved to the trash.
        </p>

        <p>
          <a href="https://dradis.com/support/guides/projects/merge_issues.html">Learn
            more about merging issues</a>.
        </p>

        <div class="accordion">

          <div class="accordion-group">
            <div class="accordion-heading w-100">
              <div class="radio">
                <%= label_tag nil, :class => 'w-100' do %>
                  <%= radio_button_tag "new", nil, false, id: "new" %>
                  Merge into a new issue
                  <%= link_to raw('<i class="fa-solid fa-chevron-down"></i>'), '#preview_issue_new', class: 'issue-toggle pull-right', data: { bs_toggle: 'collapse' } %>
                <% end %>
              </div>
            </div>
            <div id="<%= "preview_issue_new" %>" class="accordion-body p-1 collapse">
              <div id="new_issue">
                <%= text_area_tag "issue[text]", nil, class: 'textile form-control', data: { paths: editor_paths } %>
              </div>
            </div>
          </div>

          <% @issues.each_with_index do |issue, i| %>
            <div class="accordion-group">
              <div class="accordion-heading w-100">
                <div class="radio">
                  <%= label_tag nil, :class => 'w-100' do %>
                    <%= radio_button_tag "id", issue.id, i == 0 %>
                    Merge into <%= issue.title %>
                    <%= link_to raw('<i class="fa-solid fa-chevron-down"></i>'), "#preview_issue_#{issue.id}", class: 'issue-toggle pull-right', data: { bs_toggle: 'collapse' } %>
                  <% end %>
                </div>
              </div>
              <div id="<%= "preview_issue_#{issue.id}" %>" class="accordion-body p-0 collapse">
                <div class="tab-content">
                  <ul class="nav nav-pills pull-right">
                    <li class="nav-item"><a href="#normal_<%= issue.id %>" data-bs-toggle="pill" class="nav-link active">Preview</a></li>
                    <li><a href="#textile_<%= issue.id %>" data-bs-toggle="pill" class="nav-link">Source</a></li>
                  </ul>
                  <div id="normal_<%= issue.id %>" class="tab-pane active">
                    <%= markup(issue.text) %>
                  </div>
                  <div id="textile_<%= issue.id %>" class="tab-pane">
                    <pre><%= issue.text %></pre>
                  </div>
                </div>
              </div>
            </div>
          <% end %>
        </div>

        <% @issues.each do |issue| %>
          <%= hidden_field_tag 'sources[]', issue.id %>
        <% end %>

        <div class="form-actions">
          <%= button_tag 'Merge issues', class: 'btn btn-primary' %> or
          <%= link_to 'Cancel', project_issues_path(current_project), class: 'cancel-link' %>
        </div>
      <% end %>
    </div>
</div>
