{% extends 'dashboard/dashboard_base.html' %}
{% load paginate %}
{% block stage %}
<div class="content">
  <div class="list">
    <div class="list-header">
      <label>Users</label>
    </div>
    <div class="results-slct-items row mt">
      {% paginate 10 users_list %}
      {% show_pageitems %}
      <form name="filter_form" id="filter_form" method='post'>
        {% csrf_token %}
        <input type="text" name="search_text" id="search_text" value="{{ request.POST.search_text }}"/>
        <input type="hidden" name="filter_per_page" id="filter_per_page" value="{{ filter_per_page }}"/>
        <button type="submit">Submit</button>
      </form>
    </div>
    {% if users_list %}
        <div class="user_table">
      <div class="table-responsive">

    <table style="overflow:scroll;" class="sub_items table table-hover table-bordered">
                      <thead>
                        <tr>
                          <th>Id</th>
                          <th>Email</th>
                          <th>User Name</th>
                          <th>No Of Used Votes(Up/Down)</th>
                          <th>Actions</th>
                        </tr>
                      </thead>
                      <tbody class="overflow:scroll;">
                        {% for user in users_list %}
                        <tr class="sub_item_trs sub_item_{{ item.item_key }}">
                          <td>{{ forloop.counter }}</td>
                          <td>{{ user.user.email }}</td>
                          <td>{{ user.user.username }}</td>
                          <td>{{ user.used_votes }}({{user.get_no_of_up_votes}}/{{user.get_no_of_down_votes}})</td>
                          <td><a href="{% url "django_simple_forum:user_detail" user.user.id %}" class=""><i class="fa fa-eye view"></i></a><a href="{% url "django_simple_forum:edit_user" user.user.id %}" class=""><i class="fa fa-edit edit"></i></a><a href="#" data-href="{% url "django_simple_forum:user_status" user.user.id %}" class="user_status" title='{% if user.user.is_active %}InActive{%  else %}Active{% endif %}'>{% if user.user.is_active %}<i class="fa fa-times edit"></i>{% else %}<i class="fa fa-check edit"></i>{% endif %}</a><a href="#" data-href="{% url "django_simple_forum:delete_user" user.user.id %}" class="delete-user"><i class="fa fa-trash delete"></i></a></td>
                        </tr>
                        {% endfor %}
                      </tbody>
                    </table>
          {% show_pages %}
    </div>
    </div>
    <nav class="overview-paging">
    </nav>
        {% else %}
    No Users Available in database
    {% endif %}

  </div>
</div>
{% endblock %}
{% block extra_js %}
<script type="text/javascript">
  $('.delete-user').click(function(e){
      e.preventDefault();
      href = $(this).attr('data-href');
      if (!confirm('Do you want to delete User?'))
        return;
      $.post(href, $("form#jobform").serialize(), function(data) {
      if (data.error) {
        alert(data.response);
      } else {
        alert("User Deleted Successfully")
        window.location = '.';
      }
    }, 'json');
  });
  $('.user_status').click(function(e){
      e.preventDefault();
      href = $(this).attr('data-href');
      if (!confirm('Do you want to Change the User Status?'))
        return;
      $.post(href, $("form#jobform").serialize(), function(data) {
      if (data.error) {
        alert(data.response);
      } else {
        alert("User Status Changed Successfully")
        window.location = '.';
      }
    }, 'json');
  });

</script>
{% endblock %}