<% 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>
    <% if !disable_private_filter %>
      <div class="col-xs-6">
        <div class="form-group">
          <label for="is_private">Visibility</label>
          <label class="checkbox-inline">
            <input name="is_private[]" type="checkbox" value=true <%= is_private_filter.is_a?(Array) && is_private_filter.include?("true") ? "checked" : nil %>>Private
          </label>
          <label class="checkbox-inline">
            <input name="is_private[]" type="checkbox" value=false <%= is_private_filter.is_a?(Array) && is_private_filter.include?("false") ? "checked" : nil %>>Public
          </label>
        </div>
      </div>
    <% end %>
  </div>
  <div class="form-group">
    <button type="submit" class="btn btn-default" autocomplete="off">
      Apply Filters
    </button>
  </div>
</form>
<form action="/user/<%= params[:user] %>/test/publish">
  <% if !disable_private_filter %>
  <p>Select private tests to publish</p>
      <div class="form-group">
        <button type="submit" class="btn btn-default" autocomplete="off">
          Publish
        </button>
      </div>
  <% end %>
  <table class="table">
    <tr>
      <td>
      <input id="select_all_box" type="checkbox" onclick="toggleAll()" autocomplete="off"/>
      </td>
      <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'>Visibility</td>
      <td colspan='2'>Results</td>
    </tr>
    <% Array(results.records).each do |test| %>
    <tr>
      <td>
      <% if test.is_private %>
      <input class="testbox" name="run_ids[]" type="checkbox" value="<%= test.id %>" autocomplete="off"/>
      <% end %>
      </td>
      <td><%= test.date %></td>
      <td><a href="<%= package_url(test.package) %>"><%= test.package.name %></a></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>
      <td>
        <% if test.is_private %>
          <span class='fa fa-shield' title='Private'></span>
          private
        <% else %>
          <span class='fa fa-user' title='Public'></span>
          public
        <% end %>
      </td>
      <td>
      <a href="/user/:user/retry/<%= test.run_id %>" ><i class='fa fa-recycle'></i></a>
      </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.files_purged? %>
        <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>
          <%== file_link(test.log_size, "<a href=\"/data/autopkgtest/#{package_dir}/#{test.run_id}/log.gz\">test log</a> <small>(%s)</small>") %>
        </td>
        <td>
          <%== file_link(test.artifacts_size, "<a href=\"/data/autopkgtest/#{package_dir}/#{test.run_id}/artifacts.tar.gz\">artifacts</a> <small>(%s)</small>") %>
        </td>
      <% end %>
    </tr>
    <% end %>
  </table>
</form>

<script type="text/javascript">
  window.onload = function show_select_all()
  {
    var no_of_checked_boxes = $(".testbox").length
    if (no_of_checked_boxes == 0)
    {
      $("#select_all_box").css('display', "none")
    }
    else
    {
      $("#select_all_box").css("display", "block")
    }
  }

  function toggleAll()
  {
    $(".testbox").each(function ()
    {
      $(this).prop('checked', ($("#select_all_box").prop('checked')))
    })
  }
</script>

<%== erb(:pagination, locals: {results: results}) %>
