<div class="container">
  <div class="col-7">
    <div class="welcome">Editors in Chief Dashboard</div>
    <div class="hero-small dashboard">
      <div class="hero-title">
        <%= image_tag "icon_papers.svg", height: "32px" %><h1>Onboarding</h1>
      </div>
    </div>
  </div>
</div>

<%= render partial: "aeic_dashboard/menu" %>
<div class="container">

  <div class="generic-content-item">
    <h1>Invitations to join the editorial team</h1>

    <%= form_for(OnboardingInvitation.new, url: onboardings_path) do |f| %>
        <div class="form-group">
          <div class="row">
            <div class="col">
              <%= f.label :email, "New editor's email address" %>
              <%= f.email_field :email, placeholder: "Email",class: "form-control" %>
            </div>
            <div class="col">
              <%= f.label :name, "Name (optional)" %>
              <%= f.text_field :name, placeholder: "New editor's name",class: "form-control" %>
            </div>
          </div>
        </div>

          <div class="form-actions">
            <%= submit_tag("Invite", class: "btn action-btn") %>
          </div>
    <% end %>

    <% if @onboardings.empty? %>
      There are <b>no onboarding invitations</b> to show
    <% else %>
      <h3>Editor invitations</h3>
      <table id="onboarding-invitations" class="dashboard-table sortable">
        <thead>
          <tr class="text-nowrap">
            <th scope="col">Email</th>
            <th scope="col">Name</th>
            <th scope="col" class="sorttable_nosort">Token</th>
            <th scope="col">Sent</th>
            <th scope="col" colspan="2" class="sorttable_nosort">Actions</th>
          </tr>
        </thead>

        <tbody>
          <%- @onboardings.each do |onboarding_invitation| %>
          <tr>
            <td><%= onboarding_invitation.email %></td>
            <td><%= onboarding_invitation.name %></td>
            <td><%= onboarding_invitation.token %></td>
            <td sorttable_customkey=<%= onboarding_invitation.last_sent_at %>><%= time_ago_in_words(onboarding_invitation.last_sent_at) %> ago</td>
            <td>
              <%= link_to("Delete", onboarding_path(onboarding_invitation), data: { turbo_method: :delete, turbo_confirm: "Are you sure you want to delete this invitation?" }) %>
            </td>
            <td>
              <%= link_to("Re-invite", resend_invitation_onboarding_path(onboarding_invitation), data: { turbo_method: :post, turbo_confirm: "This will re-send the invitation email" }) %>
            </td>
          </tr>
          <%- end %>
        </tbody>
      </table>
    <% end %>
  </div>

  <div class="generic-content-item" id="editors">
    <h1>Pending editors</h1>

    <% if @pending_editors.empty? %>
      There are <b>no pending editors</b>
    <% else %>
      <table id="pending-editors" class="dashboard-table sortable">
        <thead>
          <tr class="text-nowrap">
            <th scope="col">Name</th>
            <th scope="col">GitHub user</th>
            <th scope="col">Status</th>
            <th scope="col" colspan="2" class="sorttable_nosort">Actions</th>
          </tr>
        </thead>

        <tbody>
          <%- @pending_editors.each do |editor| %>
          <tr>
            <td><%= editor.full_name %></td>
            <td sorttable_customkey=<%= editor.login.downcase %>>
              <%= link_to(image_tag(avatar(editor.login), size: "24x24", class: "avatar", title: "GitHub profile"), github_user_link(editor.login), target: "_blank") %>
              <%= link_to editor.login, editor, title: "View info" %></td>
            </td>
            <td>
              <% if Repository.editors.include?("@#{editor.login}") %>
                Joined GitHub organization and editors team!
              <% elsif editor.onboarding_invitation&.invited_to_team? %>
                Invitation to join organization sent. Pending acceptance.
              <% else %>
                Information registered. Ready to be invited to GitHub team
              <% end %>
            </td>
            <td>
              <%= link_to("Approve", accept_editor_onboardings_path(editor_id: editor.id), data: { turbo_method: :post, turbo_confirm: "This will make #{editor.login} a topic editor" }) %>
            </td>
            <td>
              <% if editor.onboarding_invitation&.invited_to_team? %>
                <%= link_to("Re-send invitation to join GitHub organization", invite_to_editors_team_onboardings_path(editor_id: editor.id), data: { turbo_method: :post }) %>
              <% else %>
                <%= link_to("Send invitation to join GitHub team", invite_to_editors_team_onboardings_path(editor_id: editor.id), data: { turbo_method: :post }) %>
              <% end %>
            </td>
          </tr>
          <%- end %>
        </tbody>
      </table>
    <% end %>
  </div>
</div>