{% extends 'site_templates/site_master.html' %}
{% load staticfiles %}

{% block header_block %}
  <title>Server Logs</title>


  <link rel="stylesheet" href="{% static 'plugins/highlightjs/highlight.min.css' %}" />
  

  <link rel="stylesheet" href="{% static 'plugins/jquery.json-viewer/jquery.json-viewer.css' %}">
  
{% endblock %}

{% block body_block %}
  <h2>Server Logs</h2>

  <form class="form-inline" action="{% url 'logger:all_logs' %}" method="GET">
    <h3>Filters</h3>
    <div class="form-group">
      <label for="level">Log Level:</label>
      <select id="level" name="level" class="form-control">
        <option value="">All</option>
        {% for level in log_levels %}
          <option value="{{ level.1 }}" {% if log_level == level.1 %}selected{% endif %}>{{ level.1 }}</option>
        {% endfor %}
      </select>
    </div>
    <div class="form-group">
      <label for="method">Method:</label>
      <select id="method" name="method" class="form-control">
        <option value="">All</option>
        <option value="GET" {% if method == 'GET' %}selected{% endif %}>GET</option>
        <option value="POST" {% if method == 'POST' %}selected{% endif %}>POST</option>
        <option value="PUT" {% if method == 'PUT' %}selected{% endif %}>PUT</option>
        <option value="DELETE" {% if method == 'DELETE' %}selected{% endif %}>DELETE</option>
      </select>
    </div>
    <div class="form-group">
      <label for="url">Url:</label>
      <input type="text" class="form-control" id="url" name="url" value="{{ url }}">
    </div>
    <button type="submit" class="btn btn-success">Submit</button>
  </form>

  <br /><br />

  <div class="panel-group" id="accordion">
    {% for log in logs %}
      <div class="panel 
          {% if log.log_level == 1 %}
            panel-danger
          {% elif log.log_level == 2 %}
            panel-success
          {% elif log.log_level == 3 %}
            panel-warning
          {% elif log.log_level == 4 %}
            panel-info
          {% else %}
            panel-default
          {% endif %}">
        <div class="panel-heading" data-toggle="collapse" data-parent="#accordion" href="#log{{ log.id }}" style="cursor: pointer;">
          {% if log.log_level == 1 %}
            <div class="row">
              <div class="col-md-2">
                <h4 class="panel-title"><b>Log Type: log</b></h4>
              </div>
              <div class="col-md-5">
                <h4 class="panel-title"><b>Exception Type: {{ log.exception_type }}</b></h4>
              </div>
              <div class="col-md-5">
                <h4 class="panel-title"><b>Share: </b>{{ request.get_host }}{% url 'logger:log' log.id %}</h4>
              </div>
            </div>
          {% elif log.log_level == 2 %}
            <div class="row">
              <div class="col-md-7">
                <h4 class="panel-title"><b>Log Type: DEBUG</b></h4>
              </div>
              <div class="col-md-5">
                <h4 class="panel-title"><b>Share: </b>{{ request.get_host }}{% url 'logger:log' log.id %}</h4>
              </div>
            </div>
          {% elif log.log_level == 3 %}
            <div class="row">
              <div class="col-md-7">
                <h4 class="panel-title"><b>Log Type: WARN</b></h4>
              </div>
              <div class="col-md-5">
                <h4 class="panel-title"><b>Share: </b>{{ request.get_host }}{% url 'logger:log' log.id %}</h4>
              </div>
            </div>
          {% elif log.log_level == 4 %}
            <div class="row">
              <div class="col-md-7">
                <h4 class="panel-title"><b>Log Type: INFO</b></h4>
              </div>
              <div class="col-md-5">
                <h4 class="panel-title"><b>Share: </b>{{ request.get_host }}{% url 'logger:log' log.id %}</h4>
              </div>
            </div>
          {% endif %}
          <div class="row">
            <div class="col-md-6">
              <b>Url:</b> {{ log.request_url }}
            </div>
            <div class="col-md-2">
              <b>Method:</b> {{ log.request_method }}
            </div>
            <div class="col-md-2">
              <b>{{ log.created_on }}</b>
            </div>
            <div class="col-md-2">
              <a href="{% url 'logger:log' log.id %}">Details</a>
            </div>
          </div>
        </div>
        <div id="log{{ log.id }}" class="panel-collapse collapse">
          <div class="panel-body">
            <div class="row">
              <div class="col-md-2">
                <b>Message:</b>
              </div>
              <div class="col-md-10">
                <pre>{{ log.message }}</pre>
              </div>
            </div>
            <div class="row">
              <div class="col-md-2">
                <b>User Id:</b>
              </div>
              <div class="col-md-10">
                <pre>{{ log.user_id }}</pre>
              </div>
            </div>
            <div class="row">
              <div class="col-md-2">
                <b>User Name:</b>
              </div>
              <div class="col-md-10">
                <pre>{{ log.user_name }}</pre>
              </div>
            </div>


          </div>
        </div>
      </div>
    {% endfor %}
  </div>

    <div class="col-md-10">
      <ul class="pagination pagination-md">
        {% if logs.has_previous %}
          <li><a href="?{{ prev_url }}">Prev</a></li>
        {% endif %}
        <li class="disabled"><a href="#">Page {{ logs.number }} of {{ logs.paginator.num_pages }}</a></li>
        {% if next_url %}
          <li><a href="?{{ next_url }}">Next</a></li>
        {% endif %}
      </ul>
    </div>
    <div class="col-md-2">
      <form action="{% url 'logger:all_logs' %}" method="POST">
        {% csrf_token %}
        <button type="submit" class="btn btn-danger pull-right">Delete All</button>
      </form>
    </div>

{% endblock %}

{% block js_block %}
  <script src="{% static 'plugins/highlightjs/highlight.min.js' %}"></script>
  <script src="{% static 'plugins/jquery.json-viewer/jquery.json-viewer.js' %}"></script>

  <script src="{% static 'logger/js/all_logs.js' %}"></script>
  <script>hljs.initHighlightingOnLoad();</script>
{% endblock %}