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

<!-- javascript -->
<script type="text/javascript" src="{{ url_for('static', filename='js/custom/admin.js') }}"></script>

{% endblock %}

{% block content %}
{% include 'subpages/breadcrumbs.html' %}
<div class="row admin_row">
  <div class="col-md-4">
    <div class="card">
      <div class="card-header">
        <strong>Database info <b>{{stats['stats']['dbName']}}</b></strong>
      </div>
      <div class="card-body">
        <div class="table-responsive">
          <table class="table table-hover table-striped admin_table">
            <thead class="thead-dark">
              <tr class="warning">
                <th>Collection</th>
                <th>#records</th>
                <th>Last update</th>
              </tr>
            </thead>
            <tbody>
              {% for key, values in stats['data'].items() %}
              <tr>
                <td>{{ key }}</td>
                <td>{{ values['size'] }}</td>
                <td>
                  {% if values['last_update'] is not none %}
                    {{ values['last_update'].strftime('%d-%m-%Y - %H:%M') }}
                  {% else %}
                    Not updated
                  {% endif %}
                </td>
              </tr>
              {% endfor %}
            </tbody>
          </table>
        </div>
        <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>
    </div>
  </div>
  <div class="col-md-4">
    <!-- Database update -->
    <div class="card">
      <div class="card-header">
        <strong>Update the database manually</strong>
      </div>
      <div class="card-body">
        <div class="form-group" id="updateSources">
          <p>Sources to update (defaults to configured):</p>
          <div class="row">
            {% for feed in feeds.items()|batch(2) %}
            <div class="col-md-4">
              {% for source, enabled in feed %}
              <div class="form-check">
                <input class="form-check-input" type="checkbox" id="source{{ source|upper }}" value="{{ source }}"{% if enabled %} checked{% endif %}>
                <label class="form-check-label" for="source{{ source|upper }}">{{ source|upper }}</label>
              </div>
              {% endfor %}
            </div>
            {% endfor %}
          </div>
        </div>
        <div class="form-group">
          <p>Additional options:</p>
          <div class="form-check">
            <input class="form-check-input" type="checkbox" id="updateCache">
            <label class="form-check-label" for="updateCache">Populate Redis cache (always a full update)</label>
          </div>
        </div>
        <button class="btn btn-primary btn-sm" onclick="updateDB()" class="margin_left">
          Start update
        </button>
        <button
          class="btn btn-danger btn-warning btn-sm float-right"
          onclick="clearLocks()"
          class="margin_left"
          title="Only use if you have verified no update is running. For stale locks only."
        >
          Forcibly clear locks
        </button>
      </div>
    </div>
  </div>
    <div class="col-md-4">
    <!-- Change password -->
    {% if show_pwd_form %}
    <div class="card">
      <div class="card-header">
        <strong>Change your password</strong>
      </div>
      <div class="card-body">
        <table class="admin_table">
          <tr>
            <td class="passwd_td">Current</td>
            <td><input type="password" class="form-control" name="current_pass" id="current_pass"></td>
          </tr>
          <tr>
            <td class="passwd_td">New </td>
            <td><input type="password" class="form-control" name="new_pass" id="new_pass"> </td>
          </tr>
          <tr>
            <td class="passwd_td">Repeat </td>
            <td><input type="password" class="form-control" name="repeat_pass" id="repeat_pass"> </td>
          </tr>
        </table>
        <button class="btn btn-success btn-sm" onclick="changePass()">Update</button>
      </div>
    </div>
    {% endif %}
     </div>
    </div>
  </div>
</div>

{% endblock %}
{% block body_scripts %}
<script>
  $(document).ready(function () {
    parseStatus('{{status}}')
  });
</script>
{% endblock %}
