% use DateTime;
% layout 'bootstrap';
% title 'Test summary';
% use OpenQA::Jobs::Constants;
% use List::Util qw(sum);

% content_for 'ready_function' => begin
  window.overviewParallelChildrenCollapsableResultsSel = '<%= $parallel_children_collapsable_results_sel %>';
  setupOverview();
% end

% if ($limit_exceeded) {
  <div class="alert alert-warning" id="max-jobs-limit" role="alert">
    <i class="fa fa-exclamation-circle"></i> Only <%= $limit_exceeded %> results included, please narrow down your search parameters.
  </div>
% }
<div>
    <h2>Test result overview</h2>
    <div id="summary" class="card <%= ($aggregated->{failed} + $aggregated->{not_complete}) ? 'border-danger' : 'border-success' %>">
        <div class="card-header">
            Overall Summary of
            % if (@$summary_parts) {
                <strong><% for my $part (@$summary_parts) { %><%= $part %><% } %></strong>
            % }
            % else {
                multiple distri/version
            % }
            % if ($build) {
                build <%= $build %>
            % }
            <div class="time-params">
                % if ($until) {
                    from <abbr class="timeago" title="<%= $until %>Z"><%= $until %>Z</abbr>,
                    <a href="<%= url_with->query({t => undef}) %>">show latest jobs</a>
                % }
                % else {
                    showing latest jobs,
                    <a href="<%= url_with->query({t => format_time(DateTime->now)}) %>">overview fixed to the current time</a>
                %}
            </div>
        </div>
        <div class="card-body">
            % my @badges = qw(success secondary  warning     danger info      primary light   light);
            % my @labels = qw(Passed  Incomplete Soft-Failed Failed Scheduled Running Aborted None);
            % my $index  = 0;
            % for my $category (qw(passed not_complete softfailed failed scheduled running aborted none)) {
                % my ($label, $badge) = ($labels[$index], $badges[$index++]);
                % next unless my $count = delete $aggregated->{$category};
                <%= $label %>: <span class="badge badge-<%= $badge %>"><%= $count %></span>
            % }
            % if (my $unknown_count = sum values %$aggregated) {
                Unknown: <span class="badge badge-light"><%= $unknown_count %></span>
            % }
        </div>
    </div>
    <div class="card" id="filter-panel">
        <div class="card-header"><strong>Filter</strong>
            <%= help_popover('Help for the <em>test overview</em>' => '
                    <p>This page shows an overview of job results in a matrix
                    view. Only the latest job for each scenario is shown. The
                    view can be configured based on query parameters which can
                    be set within this filter box.</p>
                    <p><b>Caveat:</b> Based on the parameters the resulting
                    job query might consider jobs as latest which do not
                    represent the complete picture for a corresponding
                    "latest" build so be careful with a more advanced
                    selection of checkboxes with the interpretation of the
                    results.</p>
                    <p>Additional tweaking of the query is possible by leaving
                    out query parameters completely or specifying them
                    multiple times equivalent to an logical "or" search.</p>',
                    'https://open.qa/docs/#_allow_group_overview_query_by_result_gh531' => 'the documentation')
                %>
            <span>no filter present, click to toggle filter form</span>
        </div>
        <div class="card-body">
            <form action="#" type="get" id="filter-form">
                <div class="form-group" id="filter-results">
                    <strong>Job result</strong>
                    % for my $result (OpenQA::Jobs::Constants::RESULTS) {
                        <label><input value="<%= $result %>" name="result" type="checkbox" id="filter-<%= $result %>"> <%= ucfirst $result =~ s/_/ /r %></label>
                    % }
                </div>
                <div class="form-group" id="filter-states">
                    <strong>Job state</strong>
                    % for my $state (OpenQA::Jobs::Constants::STATES) {
                        <label><input value="<%= $state %>" name="state" type="checkbox" id="filter-<%= $state %>"> <%= ucfirst $state =~ s/_/ /r %></label>
                    % }
                </div>
                <div class="row" id="filter-arch-flavor">
                    <div class="col-5">
                        <div class="form-group">
                            <strong>Architecture</strong>
                            <input type="text" class="form-control" name="arch" placeholder="any" id="filter-arch">
                        </div>
                    </div>
                    <div class="col-5">
                        <div class="form-group">
                            <strong>Flavor</strong>
                            <input type="text" class="form-control" name="flavor" placeholder="any" id="filter-flavor">
                        </div>
                    </div>
                </div>
                <div class="row" id="filter-machine-test">
                    <div class="col-5">
                        <div class="form-group">
                            <strong>Machines</strong>
                            <input type="text" class="form-control" name="machine" placeholder="any" id="filter-machine">
                        </div>
                    </div>
                    <div class="col-5">
                        <div class="form-group">
                            <strong>Test</strong>
                            <input type="text" class="form-control" name="test" placeholder="any" id="filter-test">
                        </div>
                    </div>
                </div>
                <div class="row" id="filter-modules">
                    <div class="col-4">
                        <div class="form-group">
                            <label for="filter-modules"><strong>Module name</strong></label>
                            <%= help_popover('Help for the <em>Module name</em> filter' => '
                            <p>Shows jobs that contain the specified modules</p>') %>
                            <input name="modules" type="text" id="modules"
                            placeholder="modules, comma separated, e.g. mod1,mod2"
                            class="form-control">
                        </div>
                    </div>
                    <div class="col-4">
                        <div class="form-group">
                            <label for="filter-module-re"><strong>Module code</strong></label>
                            <%= help_popover('Help for the <em>Module code</em> filter' => '
                            <p>Shows jobs that contain the modules found by grep command over module source code</p>') %>
                            <input type="text" class="form-control" name="module_re" placeholder="regular expression" id="filter-module-re">
                        </div>
                    </div>
                    <div class="col-2">
                        <div class="form-group">
                            <label for="modules_result"><strong>Module result</strong></label>
                            <%= help_popover('Help for the <em>Module result</em> filter' => '
                            <p>Show jobs with selected result.</p>') %>'
                            <select name="modules_result" id="modules_result" data-placeholder="any" class="chosen-select" multiple>
                                % for my $modules_result (OpenQA::Jobs::Constants::MODULE_RESULTS) {
                                    <option><%= $modules_result %></option>
                                % }
                            </select>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <strong>Misc</strong>
                    <label><input value="1" name="todo" type="checkbox" id="filter-todo"> TODO</label>
                    %= help_popover_todo
                </div>
                <button type="submit" class="btn btn-default">Apply</button>
            </form>
        </div>
    </div>
    %= include 'layouts/info'
    % for my $distri (sort keys %$results) {
        % my $type_prefix_distri = $only_distri ? '' : "Distri: $distri / ";
        % my $version_results = $results->{$distri};
        % my $only_version = scalar keys %$version_results == 1;
        % for my $version (sort keys %$version_results) {
            % my $type_prefix = $type_prefix_distri . ($only_version && $only_distri ? '' : " Version: $version / ");
            % my $type_archs = $archs->{$distri}{$version};
            % for my $type (sort keys %$type_archs) {
                % my $type_result = $version_results->{$version}{$type};
                <h3><%= $type_prefix %>Flavor: <%= $type %></h3>
                %= include 'test/overview_result_table', type => $type, type_results => $type_result, type_archs => $type_archs->{$type}
            % }
        % }
    % }
</div>
