<%
// Sanitize judge results for backward compatibility and clarity
if (!judge.result.subtasks) {
  judge.result.subtasks = [
    { case_num: judge.result.case_num, status: judge.result.status, score: judge.result.score }
  ];
  for (let i = 0; i < judge.result.case_num; ++i) {
    judge.result.subtasks[0][i] = judge.result[i];
  }
}
let runningFound = false;
for (let s of judge.result.subtasks) {
  s.pending = (s.status === 'Waiting' || s.status.startsWith('Running'));
  for (let i = 0; i < s.case_num; ++i) if (!s[i]) {
    s[i] = {
      pending: true,
      status: runningFound ? 'Waiting' : 'Running'
    };
    if (!runningFound) s.isActiveSubtask = true;
    runningFound = true;
  }
}
%>

<div class="padding">
  <table class="ui very basic center aligned table" id="status_table">
    <thead>
      <tr>
        <th>编号</th>
        <th>题目名称</th>
        <th>状态</th>
        <th>分数</th>
        <th>总时间</th>
        <th>内存</th>
        <th>代码</th>
        <th>提交者</th>
        <th>提交时间</th>
      </tr>
     </thead>
     <tbody>
       <tr>
        <td>#<%= judge.id %></td>
        <td><a href="<%= syzoj.utils.makeUrl(['problem', judge.problem_id]) %>">#<%= judge.problem_id %>. <%= judge.problem.title %></a></td>
        <% if (judge.allowedSeeResult) { %>
          <td class="status <%= getStatusMeta(judge.result.status).toLowerCase().split(' ').join('_') %>">
          <i class="<%= icon[getStatusMeta(judge.status)] || 'remove' %> icon"></i>
          <%= judge.result.status %>
          </td>
          <td class="score score_<%= parseInt(judge.result.score / 10) || 0 %>"><%= judge.result.score %></td>
          <td><%= judge.result.total_time %> ms</td>
          <td><%= parseInt(judge.result.max_memory) || 0 %> K</td>
          <td><%= syzoj.config.languages[judge.language].show %> / <%= syzoj.utils.formatSize(judge.codeLength) %></td>
        <% } else { %>
          <td>隐藏</td>
          <td>隐藏</td>
          <td>隐藏</td>
          <td>隐藏</td>
          <td>隐藏</td>
        <% } %>
        <td><a href="<%= syzoj.utils.makeUrl(['user', judge.user_id]) %>"><%= judge.user.username %></a><% if (judge.user.nameplate) { %><%- judge.user.nameplate %><% } %></td>
        <td><%= syzoj.utils.formatDate(judge.submit_time) %></td>
       </tr>
     </tbody>
  </table>
  <% if (judge.allowedSeeCode) { %>
    <div class="ui existing segment" style="position: relative; ">
      <% if (judge.allowedRejudge) { %>
        <a id="rejudge-button" href="<%= syzoj.utils.makeUrl(['submission', judge.id, 'rejudge']) %>" class="ui button" style="position: absolute; top: 0px; right: -4px; border-top-left-radius: 0; border-bottom-right-radius: 0; <% if (judge.pending) { %>display: none; <% } %>">重新评测</a>
      <% } %>
      <pre style="margin-top: 0; margin-bottom: 0; "><code id="code"><%- judge.code %></code></pre>
    </div>
    <script>
      document.addEventListener('keydown', function (event) {
        if ((event.ctrlKey || event.metaKey) && event.key === 'a') {
          var sel = window.getSelection();
          var rg = document.createRange()
          rg.selectNodeContents(document.getElementById('code'));
          sel.removeAllRanges();
          sel.addRange(rg);
          event.preventDefault();
        }
      });
    </script>
  <% } %>
<% if (judge.result.compiler_output && judge.result.status === 'Compile Error' && judge.allowedSeeCode) { %>
  <h3 class="ui header">编译信息</h3>
  <div class="ui existing segment"><pre style="margin-top: 0; margin-bottom: 0; "><code><%- judge.result.compiler_output %></code></pre></div>
<% } else if (judge.allowedSeeResult) { %>
<div class="ui styled fluid accordion" id="subtasks_list">
  <% let subtask_count = 0; %>
  <% for (let subtask_cases of (judge.result.subtasks || [])) { %>
    <% if (judge.result.subtasks.length !== 1) { %>
      <div class="title auto_update auto_update_activation<% if (subtask_cases.isActiveSubtask) { %> active<% } %>">
        <div class="ui grid">
          <div class="three wide column"><i class="dropdown icon"></i>子任务 #<%= ++subtask_count %></div>
          <div class="four wide column status status_detail <%= getStatusMeta(subtask_cases.status).toLowerCase().split(' ').join('_') %>">
          <i class="<%= icon[getStatusMeta(subtask_cases.status)] || 'remove' %> icon"></i>
          <%= subtask_cases.status %></div>
          <% if (!subtask_cases.pending) { %>
            <div class="three wide column">得分:<span style="font-weight: normal; "><%= parseFloat(subtask_cases.score.toFixed(2)).toString() %></span></div>
          <% } %>
        </div>
      </div>
      <div class="content auto_update_activation<% if (subtask_cases.isActiveSubtask) { %> active<% } %>">
        <div class="accordion" style="margin-top: 0; ">
    <% } %>
    <% for (let i = 0; i < subtask_cases.case_num; i++) { %>
    <% let testcase = subtask_cases[i]; %>
    <div class="title<% if (testcase.pending) { %> pending<% } %> auto_update"<% if (testcase.pending) { %> style="cursor: auto; "<% } %>>
      <div class="ui grid">
        <div class="three wide column"><i class="dropdown icon"></i>测试点 #<%= i + 1 %></div>
        <div class="four wide column status status_detail <%= getStatusMeta(testcase.status).toLowerCase().split(' ').join('_') %>">
        <i class="<%= icon[getStatusMeta(testcase.status)] || 'remove' %> icon"></i>
        <%= testcase.status %></div>
        <% if (!testcase.pending) { %>
          <% if (!testcase.score) testcase.score = testcase.status === 'Accepted' ? 100 : 0; %>
          <div class="three wide column">得分:<span style="font-weight: normal; "><%= parseFloat(testcase.score.toFixed(2)).toString() %></span></div>
          <div class="three wide column">用时:<span style="font-weight: normal; "><%= testcase.time_used %> ms</span></div>
          <div class="three wide column">内存:<span style="font-weight: normal; "><%= testcase.memory_used %> KiB</span></div>
        <% } %>
      </div>
    </div>
    <div class="content auto_update">
    <% if (!testcase.pending) { %>
      <p>
          <strong>输入文件</strong>
          <div class="ui existing segment"><pre style="margin-top: 0; margin-bottom: 0; "><code><%= testcase.input %></code></pre></div>
          <strong>期望输出</strong>
          <div class="ui existing segment"><pre style="margin-top: 0; margin-bottom: 0; "><code><%= testcase.answer %></code></pre></div>
          <strong>你的输出</strong>
          <div class="ui existing segment"><pre style="margin-top: 0; margin-bottom: 0; "><code><%= testcase.user_out %></code></pre></div>
          <% if (testcase.spj_message) { %>
            <strong>Special Judge 信息</strong>
            <div class="ui existing segment"><pre style="margin-top: 0; margin-bottom: 0; "><code><%= testcase.spj_message %></code></pre></div>
          <% } %>
      </p>
      <% } %>
    </div>
    <% } %>
    <% if (judge.result.subtasks.length !== 1) { %>
      </div></div>
    <% } %>
  <% } %>
</div>
<% } %>
</div>

<script>
$(function() {
  $('.ui.accordion').accordion({
    selector: {
      trigger: '.title:not(.pending)'
    }
  });
});
</script>

<% if (!isPending(judge.status)) { %><div><div id="stop_ajax"></div><div id="show_rejudge"></div></div><% } %>
<script>
<% if (isPending(judge.status) && judge.allowedSeeResult) { %>
document.addEventListener('mousedown', function (event) {
  var list = $('#submission_content').find('div.auto_update_activation');
  for (var i = 0; i < list.length; ++i) {
    $(list[i]).removeClass('auto_update_activation');
  }
});
function update_submission() {
  setTimeout(function () {
    $.get('/submission/<%= judge.id %>/ajax', function (data) {
      var e = $('#submission_content'), x = $($.parseHTML(data));
      if (x.find('#show_rejudge').length) {
        try {
          document.getElementById('rejudge-button').style.display = '';
        } catch (e) {}
      }

      if (e.find('td.status').text().trim() != x.find('td.status').text().trim()) {
        var a = e.find('div.auto_update');
        if (!a.length) {
          e.html(data);
        } else {
          e.find('#status_table').html(x.find('#status_table'));
          var b = x.find('div.auto_update');
          for (var i = 0; i < a.length; i++) {
            a[i].innerHTML = b[i].innerHTML;
            if (!$(b[i]).hasClass('pending')) $(a[i]).removeClass('pending');
            if (!$(b[i]).attr('style')) $(a[i]).attr('style', '');
          }
          a = e.find('div.auto_update_activation');
          b = x.find('div.auto_update_activation');
          for (var i = 0; i < a.length; i++) {
            if ($(b[i]).hasClass('active')) $(a[i]).addClass('active'); else $(a[i]).removeClass('active');
          }

          if (!x.find('#stop_ajax').length) update_submission();
        }
      }
      else if (!x.find('#stop_ajax').length) update_submission();
    });
  }, 500);
}
update_submission();
<% } %>
</script>