<% if @user&.username == params[:user] %>
<ol class="breadcrumb">
  <li><a href="/user">Welcome <%= @user.username %>!</a></li>
  <li class="active">Job History</li>
</ol>
<% end %>
<header class="well">
  <h1><%= params[:user] %>'s Job History</h1>
</header>
<form action="/user/<%= params[:user] %>/jobs">
  <div class="row">
    <div class="col-xs-12 col-sm-6">
      <div class="form-group">
        <label for="package">Package</label>
        <em>(* accepted as wildcard)</em>
        <input class="form-control" id="package" name="package" value="<%= package_filter %>">
      </div>
    </div>
    <div class="col-xs-12 col-sm-6">
      <div class="form-group">
        <label for="trigger">Trigger</label>
        <input class="form-control" id="trigger" name="trigger" value="<%= trigger_filter %>">
      </div>
    </div>
    <div class="col-xs-6">
      <div class="form-group">
        <label for="suite">Suite</label>
        <% settings.suites.each do |suite| %>
        <label class="checkbox-inline">
          <input name="suite[]" type="checkbox" value="<%= suite %>" <%= suite_filter.is_a?(Array) && suite_filter.include?(suite) ? "checked" : nil %>><%= suite %>
        </label>
        <% end %>
      </div>
    </div>
    <div class="col-xs-6">
      <div class="form-group">
        <label for="arch">Architecture</label>
        <% settings.archs.each do |arch| %>
        <label class="checkbox-inline">
          <input name="arch[]" type="checkbox" value="<%= arch %>"<%= arch_filter.is_a?(Array) && arch_filter.include?(arch) ? "checked" : nil %>><%= arch %>
        </label>
        <% end %>
      </div>
    </div>
  </div>
  <div class="form-group">
    <button type="submit" class="btn btn-default" autocomplete="off">
      Apply Filters
    </button>
  </div>
</form>
<table class="table">
  <tr>
    <td>Date</td>
    <td>Package</td>
    <td>Suite</td>
    <td>Architecture</td>
    <td>Status</td>
    <td>Duration</td>
    <td>Trigger</td>
    <td>Pin-Packages</td>
    <td colspan='2'>Results</td>
  </tr>
  <% Array(@history).each do |test| %>
  <tr>
    <td><%= test.date %></td>
    <td><%= test.package.name %></td>
    <td><%= test.suite %></td>
    <td><%= test.arch %></td>
    <td>
      <%== icon(test.status) %>
      <%= test.status %>
    </td>
    <td>
      <%= test.duration_human %>
    </td>
    <td><%= h test.trigger %></td>
    <td>
      <% expand_pin_packages(test).each do |pin| %>
        <%= pin.truncate(40) %> <br/>
      <% end %>
    </td>
    <% package_dir = [test.suite, test.arch, test.package.prefix, test.package.name].join('/') %>
    <% if !test.status %>
      <td><span class='fa fa-clock-o' title='Results are not in yet'></span></td>
      <td><span class='fa fa-clock-o' title='Results are not in yet'></span></td>
      <td><span class='fa fa-clock-o' title='Results are not in yet'></span></td>
    <% elsif test.expired? %>
      <td><span class='fa fa-trash' title='file has been removed due to data retention policy'></span></td>
      <td><span class='fa fa-trash' title='file has been removed due to data retention policy'></span></td>
      <td><span class='fa fa-trash' title='file has been removed due to data retention policy'></span></td>
    <% else %>
      <% dirname = File.join(Debci.config.data_basedir, 'autopkgtest', package_dir, test.run_id.to_s) %>
      <td>
        <%== filesize(File.join(dirname,'log.gz'),"<a href=\"/data/autopkgtest/#{package_dir}/#{test.run_id}/log.gz\">test log</a> <small>(%s)</small>")%>
      </td>
      <td>
        <%== filesize(File.join(dirname,'artifacts.tar.gz'),"<a href=\"/data/autopkgtest/#{package_dir}/#{test.run_id}/artifacts.tar.gz\">artifacts</a> <small>(%s)</small>")%>
      </td>
    <% end %>
  </tr>
  <% end %>
</table>
<% if @total_pages > 1 %>
  <nav class="text-center" aria-label="History pagination">
    <ul class="pagination">
      <% if @current_page.to_i != 1 %>
      <li>
        <a href="?<%= URI.encode_www_form(query_params.update(page: @current_page.to_i - 1)) %>" aria-label="Previous">
          <span aria-hidden="true">&laquo;</span>
        </a>
      </li>
      <% end %>
      <% for n in @pages %>
      <li class="<%= "active" if @current_page.to_i == n %>">
        <% if n %>
          <a href="?<%= URI.encode_www_form(query_params.update(page: n)) %>"><%= n %></a>
        <% else %>
          <a>…</a>
        <% end %>
      </li>
      <% end %>
      <% if @current_page.to_i != @total_pages %>
      <li>
        <a href="?<%= URI.encode_www_form(query_params.update(page: @current_page.to_i + 1)) %>" aria-label="Next">
          <span aria-hidden="true">&raquo;</span>
        </a>
      </li>
      <% end %>
    </ul>
  </nav>
<% end %>
