{% extends "base/base_layout.html" %}
{% load static %}
{% block sidebar_option %}
sidebar-collapse
{% endblock %}
{% block extra_css %}
<link href="{% static "adminlte/plugins/sweetalert2/sweetalert2.min.css" %}" rel="stylesheet">
{% endblock %}
{% block content %}
<div class="content-wrapper">
   <div class="content-header">
   </div>
   <div class="container-fluid">
      <div class="row">
         <div class="col-lg-12">
            <div class="card">
               <div class="card-body">
                  <div class="h-100 d-flex align-items-center justify-content-center">
                     <div class="">
                        <div class="login-logo">
                           <a href="">User Management</a>  <a href="{% url 'create_user' %}" id="add_user" class="btn btn-success" role="button"><i class="fa fa-user"></i> Add User</a>
                        </div>
                        <div class="card">
                           <div class="card-body login-card-body">
                            {% if not users %}
                            <h3>No Users Available</h3>
                            {% else %}
                            <div class="table-responsive">
                                <table class="table table-responsive-md">
                                    <thead>
                                        <tr>
                                            <th><strong>#</strong></th>
                                            <th><strong>Username</strong></th>
                                            <th><strong>Email</strong></th>
                                            <th><strong>Role</strong></th>
                                            <th><strong>Manage</strong></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for user in users %}
                                                <tr>
                                                    <td>
                                                        <div class="profile-photo">
                                                            <img src="https://www.gravatar.com/avatar/{{ user.email | md5}}" width="30" class="img-fluid rounded-circle" alt="">
                                                        </div>
                                                     </td>
                                                    <td>{{user.username}}</td>
                                                    <td>{{user.email}}</td>
                                                    <td> {% if user.is_staff %}Admin{% else %} {{user.groups.all.0}}  {% endif %}</td>
                                                    <td> {% if not user.is_staff %}
                                                      <a class="btn btn-danger btn-sm" id="{{ user.username }}" onclick="delete_user(this)" href="#"><i class="fa fa-trash"></i> Delete </a>
                                                      {% endif %}</td>
                                                </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                            {% endif %}
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</div>
{% endblock %}
{% block extra_scripts %}
<script src="{% static "adminlte/plugins/sweetalert2/sweetalert2.min.js" %}"></script>
<script>
    function delete_user(item){
      Swal.fire({
      title: 'Are you sure?',
      text: "This will permanently remove the user from MobSF",
      type: 'warning',
      showCancelButton: true,
      confirmButtonText: 'Yes',
      cancelButtonText: 'No',
      confirmButtonColor: '#d33',
      cancelButtonColor: '#2da532',
    }).then((result) => {
        if (result.value) {
            $.ajax({
                    url: '{% url "delete_user" %}',
                        type : 'POST',
                    dataType: 'json',
                        data : {
                                csrfmiddlewaretoken: '{{ csrf_token }}',
                                username: item.id,
                                },
                            success : function(json) {
                                if (json.deleted==='yes'){
                                    Swal.fire(
                                        'Deleted!',
                                        'The user is deleted!',
                                        'success'
                                    ).then(function () {
                                        location.reload();
                                    })
                                }
                                else {
                                    Swal.fire(
                                    'Delete Failed',
                                    'Cannot delete the user',
                                    'error'
                                    )
                                }
                            },
                            error : function(xhr,errmsg,err) {
                                Swal.fire(
                                    'Delete Failed',
                                    errmsg,
                                    'error'
                                    )
                            }
                });
               
        }
        });
}
</script>
{% endblock %}