{% extends 'layouts/master-page' %}
{% block title %}Admin page{% endblock %}
{% block head %}
  <!-- css -->
  <link href="/static/css/custom/admin.css" rel="stylesheet" />

  <!-- javascript -->
  <script type="text/javascript" src="/static/js/custom/admin.js"></script>
  <script>
    {% if status %}
      data = {status: "{{status}}"}
      parseStatus(data);
    {% endif %}
  </script>
{% endblock %}
{% block content %}
  <div id='stats' class="well well-small">
    <span>Database info for database <b>{{stats['stats']['dbName']}}</b></span>
    <table class="table table-hover table-striped table-nonfluid table-condensed">
      <thead>
        <tr class="warning"><td>Collection</td><td>#records</td><td>Last update</td></tr>
      </thead>
      <tbody>
        <tr><td>CVES</td>      <td>{{stats['data']['cves']['size']}}    </td>
            <td>{% if stats['data']['cves']['last_update']     is not none %} {{stats['data']['cves']['last_update'].strftime('%d-%m-%Y - %H:%M')}}     {% else %}Not updated{% endif %}</td><tr>
        <tr><td>CPE</td>       <td>{{stats['data']['cpe']['size']}}     </td>
            <td>{% if stats['data']['cpe']['last_update']      is not none %} {{stats['data']['cpe']['last_update'].strftime('%d-%m-%Y - %H:%M')}}      {% else %}Not updated{% endif %}</td><tr>
        <tr><td>CPE-other</td> <td>{{stats['data']['cpeOther']['size']}}</td>
            <td>{% if stats['data']['cpeOther']['last_update'] is not none %} {{stats['data']['cpeOther']['last_update'].strftime('%d-%m-%Y - %H:%M')}} {% else %}Not updated{% endif %}</td><tr>
        <tr><td>Capec</td>     <td>{{stats['data']['capec']['size']}}   </td>
            <td>{% if stats['data']['capec']['last_update']    is not none %} {{stats['data']['capec']['last_update'].strftime('%d-%m-%Y - %H:%M')}}    {% else %}Not updated{% endif %}</td><tr>
        <tr><td>CWE</td>       <td>{{stats['data']['cwe']['size']}}     </td>
            <td>{% if stats['data']['cwe']['last_update']      is not none %} {{stats['data']['cwe']['last_update'].strftime('%d-%m-%Y - %H:%M')}}      {% else %}Not updated{% endif %}</td><tr>
        <tr><td>via4</td>      <td>{{stats['data']['via4']['size']}}    </td>
            <td>{% if stats['data']['via4']['last_update']     is not none %} {{stats['data']['via4']['last_update'].strftime('%d-%m-%Y - %H:%M')}}     {% else %}Not updated{% endif %}</td><tr>
      </tbody>
    </table>
    <span id="wl_rules">Whitelist: {{stats['data']['whitelist']['size']}} rules</span><br />
    <span id="bl_rules">Blacklist: {{stats['data']['blacklist']['size']}} rules</span><br /><br />
    <span>Database size: {{'%0.2f' % (stats['stats']['db_size']/1024**2)}}MB ({{'%0.2f' % (stats['stats']['db_size']/1024**3)}}GB)</span><br />
    <span>Database size on disk: {{'%0.2f' % (stats['stats']['size_on_disk']/1024**2)}}MB ({{'%0.2f' % (stats['stats']['size_on_disk']/1024**3)}}GB)</span>
  </div>
  <!-- Token request -->
  <div id="token_request" class="well well-small">
    <table>
      <tr>
        <td> <input type="text" name="token" id="token" readonly="readonly" value="{{token}}"> </td>
        <td> <button onclick="requestToken()">Request new token</button> </td>
      </tr>
    </table>
  </div>
  <!-- Plug-in information -->
  <div id="plugins" class="well well-small">
    <strong>Plug-in Information</strong> <br />
    <table class="table table-hover table-striped table-nonfluid table-condensed">
      <thead>
        <tr class="warning"><td>Name</td><td>UID</td><td>Type</td><td>State</td></tr>
      </thead>
      <tbody>
        {% for plug in plugins|sort(attribute="name") %}
          {% set ptype = "Web" if plug.isWebPlugin() else "Backend" %}
          <tr><td>{{plug.name}}</td><td>{{plug.uid}}</td><td>{{ptype}}</td><td>{{plug.loadstate}}</td></tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  <!-- Database update -->
  <div class="well well-small tab">
    <strong>Update the database</strong> <br />
    <button onclick="updateDB()">Update</button>
  </div>
  <!-- Whitelist import -->
  <div class="well well-small tab">
    <strong>Manage whitelist</strong> <br />
    <input id="wl_Import" type='file' name='file' /> <br />
    <input type="checkbox" id="wl_ForceImport">Force<br />
    <button onclick="whitelistImport()">Import</button>
    <button onclick="whitelistExport()">Export</button>
    <button onclick="dropWhitelist()">Drop list</button>
    <button onclick="location.href='/admin/whitelist'">View</button>
  </div>
  <!-- Blacklist import -->
  <div class="well well-small tab">
    <strong>Manage blacklist</strong> <br />
    <input id="bl_Import" type='file' name='file' /> <br />
    <input type="checkbox" id="bl_ForceImport">Force<br />
    <button onclick="blacklistImport()">Import</button>
    <button onclick="blacklistExport()">Export</button>
    <button onclick="dropBlacklist()">Drop list</button>
    <button onclick="location.href='/admin/blacklist'">View</button>
  </div>
  <!-- Change password -->
  <div class="well well-small tab">
    <strong>Change your password</strong> <br />
    <table>
      <tr><td>Current</td><td><input type="password" name="current_pass" id="current_pass"></td></tr>
      <tr><td>New    </td><td><input type="password" name="new_pass"     id="new_pass">    </td></tr>
      <tr><td>Repeat </td><td><input type="password" name="repeat_pass"  id="repeat_pass"> </td></tr>
    </table>
    <button onclick="changePass()">Update</button>
  </div>
  <a href="#" class="back-to-top">Back to Top</a>
{% endblock %}
