{% extends "base.html" %}

{% set active_page = "Admin" %}


{% block styles %}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='bootstrap3-editable/css/bootstrap-editable.css') }}">
<script type="text/javascript" src="{{ url_for('static', filename='bootstrap3-editable/js/bootstrap-editable.min.js') }}"></script>

<style>
    .glyphicon-ok:before {
        content: "\2714";
    }
    .glyphicon-remove:before {
        content: "\2718";
    }
    .glyphicon {
        display: inline-block;
        font-size: inherit;
        text-rendering: auto;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
</style>

<script>
    $.fn.editable.defaults.mode = 'inline';
    $(document).ready(function() {
        $('#roles').editable({
            value: {{ role_indexes }},
            source: [
                {% for r in available_roles %}
                    {value: {{ available_roles.index(r) }}, text: '{{ r | replace_underscore }}'},
                {% endfor %}
            ]
        });
    });
</script>
{% endblock %}


{% block body %}

<div class="row justify-content-center">
    <div class="col-md-8 mt-4">
        <h3 class="mb-3">Manage user {{ user.email }}</h3>
    </div>
</div>

<div class="row justify-content-center">
    <div class="col-md-8">
        <table class="table table-bordered">
            <tr>
                <th width="120px" style="text-align: right;">Username</th>
                <td>{{ user.email }}</td>
            </tr>
            <tr>
                <th style="text-align: right;">ID</th>
                <td>{{ user.id }}</td>
            </tr>
            <tr>
                <th style="text-align: right;">API Key</th>
                <td>{{ user.api_key }}</td>
            </tr>

            <!-- user roles -->
            <tr>
                <th class="d-flex justify-content-between align-items-center align-middle border-0">
                    <a href="#" data-toggle="modal" data-target="#user_role_help_modal" style="width: 100%">
                        <span class="fas fa-info-circle" data-toggle="tooltip" title="show role definitions"></span>
                    </a>
                    Roles
                </th>
                <td>
                    <a href="#" id="roles" data-type="checklist" data-pk="{{ user.email }}" data-url="/admin/edit_user" data-title="Select User Groups"></a>
                </td>
            </tr>
        </table>

        <!-- role help -->
        <div id="user_role_help_modal" class="modal" role="dialog" data-backdrop="false">
            <div class="modal-dialog">
                <div class="modal-content">

                    <div class="modal-header d-flex justify-content-between align-items-center">
                        <h5 class="modal-title">User roles</h5>
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
                    </div>

                    <div class="modal-body p-0">
                        <table class="table table-bordered table-striped mb-0">
                            <tr>
                                <th></th>
                                {% for role in available_roles | sort_roles %}
                                    <th>
                                        {{ role | replace_underscore }}
                                    </th>
                                {% endfor %}
                            </tr>
                            {% for privilege in privileges | sort_privileges %}
                                <tr>
                                    <th style="text-align: right">{{ privilege | replace_underscore }}</th>
                                    {% for role in available_roles | sort_roles %}
                                        <td class="align-middle">
                                            {% if role in privileges[privilege] %}
                                                <i class="fas fa-check text-success"></i>
                                            {% else %}
                                                <i class="fas fa-times text-danger"></i>
                                            {% endif %}
                                        </td>
                                    {% endfor %}
                                </tr>
                            {% endfor %}
                        </table>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>


<div class="row justify-content-center">
    <div class="col-md-4 mt-4 mb-2">
        <button data-toggle="collapse" data-target="#delete-button-div" class="btn btn-primary" style="width: 100%">
            <i class="fas fa-trash-alt"></i> Delete user
        </button>
        <div class="collapse" id="delete-button-div">
            <button class="btn btn-danger" type="button" onclick='window.location.href = "/admin/delete_user/{{ user.email }}";' style="width: 100%">
                <i class="fas fa-check"></i> Are you sure?
            </button>
        </div>
    </div>

<!-- admin change user password -->
    <div class="col-md-4 mt-4 mb-2">

        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#admin_change_password_modal" style="width: 100%">
            <i class="fas fa-key"></i> Change password
        </button>

        <div id="admin_change_password_modal" class="modal fade" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content">

                    <div class="modal-header d-flex justify-content-between align-items-center">
                        <h5 class="modal-title">Change user password</h5>
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
                    </div>

                    <div class="modal-body">
                        <form method="post">

                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"><i class="fas fa-lock"></i></span>
                                </div>
                                <input id="admin_change_password" type="password" class="form-control" name="admin_change_password" placeholder="new password" required>
                            </div>

                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"><i class="fas fa-lock"></i></span>
                                </div>
                                <input id="admin_confirm_password" type="password" class="form-control" name="admin_confirm_password" placeholder="confirm password" required>
                            </div>

                            <button class="btn btn-primary" type="submit" id="admin_change_password_button" value="admin_change_password">Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

{% endblock %}