{% extends "base.html" %}

{% block content %}
    <h1>Users</h1>
    <table class="table table-striped" id="usersTable"></table>

    <script>
        $(authWidget(function() {
            reload(() => data_ajax('get_users'), fillUsersTable)
        }));
    
        $(function() {
            $('#usersTable').DataTable({
                "paging":false,
                "saveState":true,
                "columns": [
                    {
                        "title":"Email",
                        "render": function(data, type, row) {
                            return '<a href=/user/'+ data + '>' + data + '</a>'
                        }
                    },
                    {
                        "title":"UserID",
                        "render": function(data, type, row) {
                            return '<a href=/user/'+ row[1] + '>' + data + '</a>'
                        }
                    }
                ]
            });
        });
        
        function fillUsersTable(response) {
            table = $('#usersTable').DataTable()
            data = JSON.parse(response)
            if (data.status == 'ok') {
                table.clear()
                for (var user in data.users) {
                    table.row.add([
                        data.users[user]['email'],
                        user
                    ]);
                }
                table.draw()
            }
        }
        
    </script>
{% endblock %}
