<% current_job = pj[:job] if pj[:job] != {} and pj[:job][:uuid] %>
<div class="panel panel-default">
  <div class="panel-heading">
    <div class="container-fluid">
      <div class="row-fluid">
        <%# column offset 0 %>
        <div class="col-md-3" style="word-break:break-all;">
          <h4 class="panel-title">
            <a data-toggle="collapse" href="#collapse<%= i %>">
              <%= pj[:name] %> <span class="caret"></span>
            </a>
          </h4>
        </div>

        <%# column offset 3 %>
        <div class="col-md-2 pipeline-instance-spacing">
          <%= pj[:progress_bar] %>
        </div>

        <% if current_job %>
          <%# column offset 5 %>
          <% if current_job[:state] != "Queued" %>
          <div class="col-md-3">
            <% if current_job[:started_at] %>
              <% walltime = ((if current_job[:finished_at] then current_job[:finished_at] else Time.now() end) - current_job[:started_at]) %>
              <% cputime = (current_job[:runtime_constraints].andand[:min_nodes] || 1) *
                           ((current_job[:finished_at] || Time.now()) - current_job[:started_at]) %>
              <%= render_runtime(walltime, false) %>
              <% if cputime > 0 %> / <%= render_runtime(cputime, false) %> (<%= (cputime/walltime).round(1) %>&Cross;)<% end %>
            <% end %>
          </div>
          <% end %>

          <% if current_job[:state] == "Queued" %>
            <%# column offset 5 %>
            <div class="col-md-6">
              <% queuetime = Time.now - Time.parse(current_job[:created_at].to_s) %>
              Queued for <%= render_runtime(queuetime, false) %>.
              <% begin %>
                <% if current_job[:queue_position] == 0 %>
                  This job is next in the queue to run.
                <% elsif current_job[:queue_position] == 1 %>
                  There is 1 job in the queue ahead of this one.
                <% elsif current_job[:queue_position] %>
                  There are <%= current_job[:queue_position] %> jobs in the queue ahead of this one.
                <% end %>
              <% rescue %>
              <% end %>
            </div>
          <% elsif current_job[:state] == "Running" %>
            <%# column offset 8 %>
            <div class="col-md-3">
              <span class="task-summary-status">
                <%= current_job[:tasks_summary][:done] %>&nbsp;<%= "task".pluralize(current_job[:tasks_summary][:done]) %> done,
                <%= current_job[:tasks_summary][:failed] %>&nbsp;failed,
                <%= current_job[:tasks_summary][:running] %>&nbsp;running,
                <%= current_job[:tasks_summary][:todo] %>&nbsp;pending
              </span>
            </div>
          <% elsif current_job[:state].in? ["Complete", "Failed", "Cancelled"] %>
            <%# column offset 8 %>
            <div class="col-md-4 text-overflow-ellipsis">
              <% if pj[:output_uuid] %>
                <%= link_to_arvados_object_if_readable(pj[:output_uuid], 'Output data not available', friendly_name: true) %>
              <% elsif current_job[:output] %>
                <%= link_to_arvados_object_if_readable(current_job[:output], 'Output data not available', link_text: "Output of #{pj[:name]}") %>
              <% else %>
                No output.
              <% end %>
            </div>
          <% end %>

          <% if current_job[:state].in? ["Queued", "Running"] and @object.editable? %>
            <%# column offset 11 %>
            <div class="col-md-1 pipeline-instance-spacing">
              <%= form_tag "/jobs/#{current_job[:uuid]}/cancel", remote: true, style: "display:inline; padding-left: 1em" do |f| %>
                <%= hidden_field_tag :return_to, url_for(@object) %>
                <%= button_tag "Cancel", {class: 'btn btn-xs btn-danger', id: "cancel-job-button"} %>
              <% end %>
            </div>
          <% end %>
        <% end %>
      </div>
    </div>
  </div>

  <div id="collapse<%= i %>" class="panel-collapse collapse <%= if expanded then 'in' end %>">
    <div class="panel-body">
      <div class="container">
        <% current_component = (if current_job then current_job else pj end) %>
        <div class="row">
          <div class="col-md-6">
            <table>
              <% # link to repo tree/file only if the repo is readable
                 # and the commit is a sha1...
                 repo =
                 (/^[0-9a-f]{40}$/ =~ current_component[:script_version] and
                 Repository.where(name: current_component[:repository]).first)

                 # ...and the api server provides an http:// or https:// url
                 repo = nil unless repo.andand.http_fetch_url
                 %>
              <% [:script, :repository, :script_version, :supplied_script_version, :nondeterministic].each do |k| %>
                <tr>
                  <td style="padding-right: 1em">
                    <%= k.to_s %>:
                  </td>
                  <td>
                    <% if current_component[k].nil? %>
                      (none)
                    <% elsif repo and k == :repository %>
                      <%= link_to current_component[k], show_repository_tree_path(id: repo.uuid, commit: current_component[:script_version], path: '/') %>
                    <% elsif repo and k == :script %>
                      <%= link_to current_component[k], show_repository_blob_path(id: repo.uuid, commit: current_component[:script_version], path: 'crunch_scripts/'+current_component[:script]) %>
                    <% elsif repo and k == :script_version %>
                      <%= link_to current_component[k], show_repository_commit_path(id: repo.uuid, commit: current_component[:script_version]) %>
                    <% else %>
                      <%= current_component[k] %>
                    <% end %>
                  </td>
                </tr>
              <% end %>
              <% if current_component[:runtime_constraints].andand[:docker_image] and current_component[:docker_image_locator] %>
                <tr>
                  <td style="padding-right: 1em">
                    docker_image:
                  </td>
                  <td>
                    <%= current_component[:runtime_constraints][:docker_image] %>
                  </td>
                </tr>
                <tr>
                  <td style="padding-right: 1em">
                    docker_image_locator:
                  </td>
                  <td>
                    <%= link_to_arvados_object_if_readable(current_component[:docker_image_locator],
                      current_component[:docker_image_locator], friendly_name: true) %>
                  </td>
                </tr>
              <% else %>
                <tr>
                  <td style="padding-right: 1em">
                    docker_image:
                  </td>
                  <td>
                    Not run in Docker
                  </td>
                </tr>
              <% end %>
            </table>
          </div>
          <div class="col-md-5">
            <table>
              <% [:uuid, :modified_by_user_uuid, :priority, :created_at, :started_at, :finished_at].each do |k| %>
                <tr>
                  <td style="padding-right: 1em">
                    <%= k.to_s %>:
                  </td>
                  <td>
                    <% if k == :uuid %>
                      <%= link_to_arvados_object_if_readable(current_component[k], current_component[k], link_text: current_component[k]) %>
                    <% elsif k.to_s.end_with? 'uuid' %>
                      <%= link_to_arvados_object_if_readable(current_component[k], current_component[k], friendly_name: true) %>
                    <% elsif k.to_s.end_with? '_at' %>
                      <%= render_localized_date(current_component[k]) %>
                    <% else %>
                      <%= current_component[k] %>
                    <% end %>
                  </td>
                </tr>
              <% end %>
            </table>
          </div>
        </div>
        <div class="row">
          <div class="col-md-6">
            <p>script_parameters:</p>
            <pre><%= JSON.pretty_generate(current_component[:script_parameters]) rescue nil %></pre>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>