<% if current_user.aeic? && JournalFeatures.tracks? %>
<div class="card" id="track-info">
  <div class="card-header">Track</div>
  <div class="card-body">
  <% if paper.meta_review_issue_id %>

    <% if paper.track %>
      <p class="card-text">
        This paper is assigned to the track: <strong><%= paper.track.name %></strong>.
      </p>
      <p class="card-text">
        This track is managed by:
        <ul>
          <% paper.track.aeics.each do |aeic| %>
            <li><%= aeic.full_name %> (<%= aeic.login %>)</li>
          <% end %>
          <%= "Track without AEiC" if paper.track.aeics.empty? %>
        </ul>
      </p>
    <% else %>
    <p class="card-text">
      This paper is <strong>not assigned to any track</strong>
      </p>
    <% end %>
  <% else %>
    <% if paper.suggested_subject %>
      <p class="card-text">
        Author suggested this paper' subject is <strong><%= paper.suggested_subject %></strong>
      </p>
    <% else %>
      <p class="card-text">
        Author didn't suggest any subject for this paper.
      </p>
    <% end %>

    <% if paper.track_id.present? %>
      <p class="card-text">
        This submission will be assigned to the track:  <strong><%= paper.track.name %></strong>
      </p>
      <p class="card-text">
        <%= paper.track.aeics.empty? ? "Track without AEiC" : "Track managed by:" %>
        <ul>
          <% paper.track.aeics.each do |aeic| %>
            <li><%= aeic.full_name %> (<%= aeic.login %>)</li>
          <% end %>
        </ul>
      </p>
      <% end %>
  <% end %>

  <%= link_to "Change track &raquo;".html_safe, "#changeTrack", title: "Change submission's track", data: { toggle: 'collapse'} %>

    <div class="row collapse" id="changeTrack">
      <div class="col-md-6">
        <%= form_tag(change_track_paper_url(paper), class: "left") do %>
          <%= select_tag :track_id, options_from_collection_for_select(Track.where.not(id: paper.track_id), "id", "name"), prompt: "Select new track", class: "form-control left" %>
          <%= submit_tag "Change paper's track", class: "btn btn-primary left change-track" %>
        <% end %>
      </div>
    </div>
  </div>
</div>
<br />
<% end %>

<% if current_user.aeic? %>
<div class="card border-danger" id="edit-metadata">
  <div class="card-header text-danger">&#9888; Edit paper metadata</div>
  <div class="card-body">
    <div class="alert alert-danger">
      <strong>&#9888; Warning:</strong> Directly editing paper metadata bypasses normal submission processes. Only use this to correct data errors (e.g. encoding issues in the title or tags). Any changes take effect immediately on the live site.
    </div>
    <%= link_to "Edit metadata &raquo;".html_safe, "#editMetadata", data: { toggle: 'collapse' } %>
    <div class="row collapse" id="editMetadata">
      <div class="col-md-9" style="margin-top: 1em;">
        <%= form_tag(update_metadata_paper_url(paper)) do %>
          <div class="form-group">
            <%= label_tag :title, "Title" %>
            <%= text_field_tag :title, paper.title, class: "form-control", placeholder: "Leave blank to keep current value" %>
          </div>
          <div class="form-group" style="margin-top: 0.75em;">
            <%= label_tag :tags, "Tags (comma-separated)" %>
            <%= text_field_tag :tags, paper.metadata.dig('paper', 'tags')&.join(', '), class: "form-control", placeholder: "Leave blank to keep current value" %>
          </div>
          <div class="form-group" style="margin-top: 0.75em;">
            <%= label_tag :citation_string, "Citation string" %>
            <%= text_field_tag :citation_string, paper.citation_string, class: "form-control", placeholder: "Leave blank to keep current value" %>
          </div>
          <%= submit_tag "Update metadata", class: "btn btn-danger", style: "margin-top: 0.75em;", data: { confirm: "Are you sure you want to directly edit this paper's metadata?" } %>
        <% end %>
      </div>
    </div>
  </div>
</div>
<br />
<% end %>

<% if current_user.aeic? || (current_user == paper.submitting_author) %>
  <div class="card">
    <div class="card-header">
      Paper actions
    </div>
    <div class="card-body">
      <p class="card-text">Available actions for you on this paper (note to editors that if you use these buttons, you'll still have to manually close the review issue on GitHub).</p>

      <div class="row">
        <% if current_user.aeic? %>
          <% unless paper.meta_review_issue_id %>
          <div class="col-md-5">
            <%= form_tag(start_meta_review_paper_url(paper), class: "left") do %>
              Editor: <%= select_tag :editor, options_for_select(Repository.editors, selected: nil), prompt: "Suggest editor (optional)", class: "form-control left" %>
              <%= submit_tag "Start pre review", class: "btn btn-primary left start-review" %>
            <% end %>
          </div>
          <div class="col-md-1">
          </div>
          <% end %>

          <% unless paper.review_issue_id %>
          <div class="col-md-3">
            <%= link_to "Reject paper", reject_paper_path(paper), data: { turbo_method: :post, turbo_confirm: "Rejecting. Are you sure?" }, form_class: "left", class: "btn btn-danger" %>
          </div>
          <% end %>
        <% end %>

        <% if current_user.aeic? %>
          <div class="col-md-3">
            <%= link_to "Withdraw paper", withdraw_paper_path(paper), data: { turbo_method: :post, turbo_confirm: "Withdrawing. Are you sure?" }, form_class: "left", class: "btn btn-danger" %>
          </div>
        <% end %>
      </div>
    </div>
  </div>
  <br />
<% end %>
