<% this.title = '题库' %>
<% include header %>
<%
let tagIDs = [];
if (typeof tags !== 'undefined') tagIDs = tags.map(x => x.id);
%>
<script src="//cdn.bootcss.com/js-cookie/2.1.4/js.cookie.min.js"></script>
<div class="padding">
  <div class="ui grid">
    <div class="row">
      <div class="seven wide column">
        <% if (typeof tags !== 'undefined') { %>
          <%
          tags.sort((a, b) => {
            return a.color > b.color ? 1 : -1;
          });
          %>
          <% for (let tag of tags) { %>
              <% let tagListRemovedThisTag = tagIDs.filter(x => x != tag.id).sort().join(','); %>
              <% let url = tagListRemovedThisTag ? syzoj.utils.makeUrl(['problems', 'tag', tagListRemovedThisTag]) : syzoj.utils.makeUrl(['problems']); %>
              <a href="<%= url %>" class="ui tiny <%= tag.color %> label">
                <%= tag.name %>
              </a>
          <% } %>
        <% } %>
      </div>
      <div class="nine wide right aligned column">
        <div class="ui toggle checkbox" id="show_tag">
          <style id="show_tag_style"></style>
          <script>
          if (Cookies.get('show_tag') === '1') {
            document.write('<input type="checkbox" checked>');
            document.getElementById('show_tag_style').innerHTML = '.show_tag_controled { visibility: visible; }';
          } else {
            document.write('<input type="checkbox">');
            document.getElementById('show_tag_style').innerHTML = '.show_tag_controled { visibility: hidden; }';
          }
          </script>

          <script>
          $(function () {
            $('#show_tag').checkbox('setting', 'onChange', function () {
              let checked = $('#show_tag').checkbox('is checked');
              Cookies.set('show_tag', checked ? '1' : '0');
              if (checked) {
                document.getElementById('show_tag_style').innerHTML = '.show_tag_controled { visibility: visible; }';
              } else {
                document.getElementById('show_tag_style').innerHTML = '.show_tag_controled { visibility: hidden; }';
              }
            });
          });
          </script>
          <label>显示分类标签</label>
        </div>
        <div style="margin-left: 10px; display: inline-block; ">
          <% if (user && user.is_admin) { %>
            <% if (typeof tags !== 'undefined' && tags.length === 1) { %>
              <a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['problems', 'tag', tags[0].id, 'edit']) %>" class="ui labeled icon mini blue button"><i class="write icon"></i> 编辑标签</a>
            <% } %>
            <a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['problems', 'tag', 0, 'edit']) %>" class="ui labeled icon mini green button"><i class="plus icon"></i> 添加标签</a>
          <% } %>
          <% if (user) { %>
            <div style="margin-left: 10px; " class="ui mini buttons">
              <div class="ui labeled icon mini dropdown button" id="add_problem_dropdown"><i class="plus icon"></i> 添加题目
              <div class="menu">
                <a class="item" href="<%= syzoj.utils.makeUrl(['problem', 0, 'edit']) %>"><i class="file icon"></i> 新建题目</a>
                <a class="item" href="<%= syzoj.utils.makeUrl(['problem', 0, 'import']) %>"><i class="cloud download icon"></i> 导入题目</a>
              </div>
              </div>
            </div>
          <% } %>
        </div>
      </div>
    </div>
  </div>

  <table class="ui very basic center aligned table">
    <thead>
      <tr>
        <% if (user) { %>
        <th class="one wide">提交状态</th>
        <% } %>
        <th class="one wide">编号</th>
        <th class="left aligned">题目名称</th>
        <th class="right aligned">&nbsp;</th>
        <th class="one wide">通过</th>
        <th class="one wide">提交</th>
        <th class="one wide">通过率</th>
      </tr>
    </thead>
    <tbody>
    <% for (let problem of problems) { %>
        <% if (problem.is_public || problem.allowedEdit) { %>
        <tr>
            <% if (user) { %>
                <td>
                <% if (problem.judge_state) { %>
                  <a href="<%= syzoj.utils.makeUrl(['submission', problem.judge_state.id]) %>">
                    <span class="status <%= problem.judge_state.status.toLowerCase().split(' ').join('_') %>">
                      <i class="<%= icon[problem.judge_state.status] || 'remove' %> icon"></i>
                    </span>
                  </a>
                <% } %>
                </td>
            <% } %>
            <td><%= problem.id %></td>
            <td class="left aligned"><a href="<%= syzoj.utils.makeUrl(['problem', problem.id]) %>"><%= problem.title %>
              <% if (!problem.is_public) { %><span class="ui header"><span class="ui tiny red label">未公开</span></span><% } %>
            </a></td>
            <td class="right aligned">
              <div class="show_tag_controled">
                <%
                if (problem.tags) {
                  for (let tag of problem.tags) {
                    let tagListToggledThisTag;
                    if (!tagIDs.includes(tag.id)) tagListToggledThisTag = tagIDs.concat([tag.id]);
                    else tagListToggledThisTag = tagIDs.filter(x => x != tag.id);
                    tagListToggledThisTag = tagListToggledThisTag.sort().join(',');

                    let url = tagListToggledThisTag ? syzoj.utils.makeUrl(['problems', 'tag', tagListToggledThisTag]) : syzoj.utils.makeUrl(['problems']);
                  %>
                  <span class="ui header">
                    <a href="<%= url %>" class="ui tiny <%= tag.color %> label">
                      <%= tag.name %>
                    </a>
                  </span>
                  <%
                  }
                }
                %>
              </div>
            </td>
            <td><%= problem.ac_num %></td>
            <td><%= problem.submit_num %></td>
            <td><%= ((problem.ac_num / problem.submit_num * 100) || 0).toFixed(2) %>%</td>
        </tr>
        <% } %>
    <% } %>
    </tbody>
  </table><br>
  <% include page  %>
<script>
document.addEventListener('keydown', function (event) {
  if (event.keyCode === 39) document.getElementById('page_next').click();
  else if (event.keyCode === 37) document.getElementById('page_prev').click();
});

$(function () {
  $('#add_problem_dropdown').dropdown();
});
</script>
</div>
<% include footer %>