{% extends base_template|default:"userextensions/userextensions_base.htm" %}
{% load static %}

{% block local_head %}
{% include 'handyhelpers/component/clipboard.htm' %}
{% endblock local_head %}

{% block content %}
<div class="container-fluid mb-5 animated fadeIn">
    <h1 class="text-primary"><b>Manage Service Accounts: </b></h1>
</div>

<div class="container-fluid pl-5 pr-5">
    <div class="row mb-5 animated fadeIn" style="animation-delay: .25s;">
        <div class="col-sm-12 col-md-3 col-lg-2 mb-3 pl-0">
            <i class="fas fa-users-cog fa-6x text-primary"></i><br/>
        </div>
        <div class="col-sm-12 col-md-9 col-lg-10">
            <div class="row">
                <h3 class="text-primary mb-3">Service Accounts</h3>
            </div>
            <div class="row">
                <table style="width: 100%">
                    <thead>
                    <tr>
                        <th class="bg-transparent text-secondary">User Account</th>
                        <th class="bg-transparent text-secondary">Group</th>
                        <th class="bg-transparent text-secondary">Owner Enabled</th>
                        <th class="bg-transparent text-secondary">Admin Enabled</th>
                        <th class="bg-transparent text-secondary">Actions</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for row in service_accounts %}
                    <tr class="border-top border-secondary">
                        <td><span class="ml-1">{{ row.user }}</span></td>
                        <td><span class="ml-1">{{ row.group }}</span></td>
                        <td>
                            {% if row.enabled %}
                            <span class="text-primary ml-1"><i class="fas fa-check"></i></span>
                            {% else %}
                            <span class="text-danger ml-1"><i class="fas fa-times"></i></span>
                            {% endif %}
                        </td>
                        <td>
                            {% if row.admin_enabled %}
                            <span class="text-primary ml-1"><i class="fas fa-check"></i></span>
                            {% else %}
                            <span class="text-danger ml-1"><i class="fas fa-times"></i></span>
                            {% endif %}
                        </td>
                        <td>
                            <!-- view api token -->
<!--                            <a href="#" title="view api token" role="button" data-toggle="tooltip" data-placement="left"-->
<!--                               onClick="showLocalInfo('API Token: <small><i>{{ row.user }}</i></small>', '{{ row.user.auth_token }}');">-->
<!--                                <i class="fas fa-key ml-1 mr-1"></i>-->
<!--                            </a>-->

                            <!-- show api token -->
                            <a href="#" title="view api token" role="button" data-toggle="tooltip" data-placement="left"
                               onClick="showInfo('{% url "userextensions:show_srv_acct_token" %}', '{{ row.user.auth_token }}', 'API Token: <small><i>{{ row.user.username }}</i></small>', 'lg');">
                                <i class="fas fa-key ml-1 mr-1"></i>
                            </a>

                            <!-- refresh api token -->
                            <a href="#" title="refresh api token" role="button" data-toggle="tooltip" data-placement="left"
                               onClick="confirmAction('{% url "userextensions:refresh_srv_acct_token" %}?srv_acct_id={{ row.id }}', 'Update Token', 'This will delete the API token and create a new one. Do you wish to continue?', 'Continue', 'POST');">
                                <i class="fas fa-sync-alt ml-1 mr-1"></i>
                            </a>

                            <!-- api token history -->
                            <a href="#" title="view api token history" role="button" data-toggle="tooltip" data-placement="left"
                               onClick="showInfo('{% url "userextensions:get_srv_acct_token_history" %}', '{{ row.id }}', 'Token Refresh History: <small><i>{{ row.user.username }}</i></small>', 'lg');">
                                <i class="fas fa-history ml-1 mr-1"></i>
                            </a>

                            <!-- enable/disable -->
                            {% if row.enabled %}
                            <a href="#" title="disable service account" role="button" data-toggle="tooltip" data-placement="left"
                               onClick="confirmAction('{% url "userextensions:disable_srv_account" %}?srv_acct_id={{ row.id }}', 'Disable Service Account', 'This will set the <i>{{ row.user }}</i> service account to disabled. Do you wish to continue?', 'Continue', 'POST');">
                                <i class="fas fa-toggle-off ml-1 mr-1"></i>
                            </a>
                            {% else %}
                            <a href="#" title="enable service account" role="button" data-toggle="tooltip" data-placement="left"
                                onClick="confirmAction('{% url "userextensions:enable_srv_account" %}?srv_acct_id={{ row.id }}', 'Enable Service Account', 'This will set the <i>{{ row.user }}</i> service account to enabled. Do you wish to continue?', 'Continue', 'POST');">
                                <i class="fas fa-toggle-on ml-1 mr-1"></i>
                            </a>
                            {% endif %}

                            <!-- view users in group -->
                            <a href="#" title="view users in group" role="button" data-toggle="tooltip" data-placement="left"
                               onClick="showInfo('{% url "userextensions:get_users_per_group" %}', '{{ row.group.id }}', 'Users: <small><i>{{ row.group.name }}</i></small>', 'lg');">
                            <i class="fas fa-user-friends ml-1 mr-1"></i>
                            </a>

                            <!-- delete -->
                            <a href="#" title="delete service account" role="button" data-toggle="tooltip" data-placement="left"
                               onClick="confirmAction('{% url "userextensions:delete_srv_account" %}?srv_acct_id={{ row.id }}', 'Delete Service Account', 'This will permanently delete the <i>{{ row.user }}</i> service account. Do you wish to continue?', 'Continue', 'POST');">
                               <i class="fas fa-trash ml-1 mr-1"></i>
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    &nbsp;<br/>

    {% if groups %}
    <div class="row mb-5 animated fadeIn" style="animation-delay: .5s;">
        <div class="col-sm-12 col-md-3 col-lg-2 mb-3 pl-0">
            <i class="fas fa-users fa-6x text-primary"></i>
        </div>
        <div class="col-sm-12 col-md-9 col-lg-10">
            <div class="row">
                <h3 class="text-primary mb-3">Groups Without Service Accounts</h3>
            </div>
            <div class="row">
                <table style="width: 100%">
                    <thead>
                    <tr>
                        <th class="bg-transparent text-secondary">Group</th>
                        <th class="bg-transparent text-secondary">User Count</th>
                        <th class="bg-transparent text-secondary">Actions</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for row in groups %}
                    <tr class="border-top border-secondary">
                        <td><span class="ml-1">{{ row.name }}</span></td>
                        <td><span class="ml-1">
                            <a href="#" title="view users" role="button" data-toggle="tooltip" data-placement="left"
                               onClick="showInfo('{% url "userextensions:get_users_per_group" %}', '{{ row.id }}', 'Users: <small><i>{{ row.name }}</i></small>', 'lg');">{{ row.user_set.count }}</a>
                        </span></td>
                        <td>
                            <a href="#" title="view users" role="button" data-toggle="tooltip" data-placement="left"
                               onClick="showInfo('{% url "userextensions:get_users_per_group" %}', '{{ row.id }}', 'Users: <small><i>{{ row.name }}</i></small>', 'lg');">
                                <i class="fas fa-user-friends ml-1 mr-1"></i>
                            </a>

                            <a href="#" title="create service account" role="button" data-toggle="tooltip" data-placement="left"
                               onClick="confirmAction('{% url 'userextensions:create_srv_account' %}?group_id={{ row.id }}', 'Create Service Account', 'This will create a new service account linked to <b><i>{{ row.name }}</i></b>. Do you wish to continue?', 'Continue', 'POST');">
                                <i class="fas fa-plus ml-1 mr-1"></i>
                            </a>
                        </td>
                    </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% endif %}
</div>

{% include 'handyhelpers/component/modals.htm' %}
{% endblock content %}
