{% extends 'base.html' %}
{% load static %}
{% load i18n %}

{% block custom_head_css_js %}
    <link href='{% static "css/plugins/select2/select2.min.css" %}' rel="stylesheet">
    <script src='{% static "js/plugins/select2/select2.full.min.js" %}'></script>
{% endblock %}

{% block content %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="panel-options">
                        <ul class="nav nav-tabs">
                            <li class="active">
                                <a href="{% url 'assets:system-user-detail' pk=system_user.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Detail' %} </a>
                            </li>
{#                            <li>#}
{#                                <a href="{% url 'assets:system-user-asset' pk=system_user.id %}" class="text-center">#}
{#                                <i class="fa fa-bar-chart-o"></i> {% trans 'Attached assets' %}#}
{#                                </a>#}
{#                            </li>#}
                            <li class="pull-right">
                                <a class="btn btn-outline btn-default" href="{% url 'assets:system-user-update' pk=system_user.id %}"><i class="fa fa-edit"></i>{% trans 'Update' %}</a>
                            </li>
                            <li class="pull-right">
                                <a class="btn btn-outline btn-danger btn-del">
                                    <i class="fa fa-trash-o"></i>{% trans 'Delete' %}
                                </a>
                            </li>
                        </ul>
                    </div>
                    <div class="tab-content">
                        <div class="col-sm-8" style="padding-left: 0;">
                            <div class="ibox float-e-margins">
                                <div class="ibox-title">
                                    <span class="label"><b>{{ system_user.name }}</b></span>
                                    <div class="ibox-tools">
                                        <a class="collapse-link">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                            <i class="fa fa-wrench"></i>
                                        </a>
                                        <ul class="dropdown-menu dropdown-user">
                                        </ul>
                                        <a class="close-link">
                                            <i class="fa fa-times"></i>
                                        </a>
                                    </div>
                                </div>
                                <div class="ibox-content">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                            <td>{% trans 'Name' %}:</td>
                                            <td><b>{{ system_user.name }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Username' %}:</td>
                                            <td><b>{{ system_user.username }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Protocol' %}:</td>
                                            <td><b>{{ system_user.protocol }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Sudo' %}:</td>
                                            <td><b>{{ system_user.sudo }}</b></td>
                                        </tr>
                                        {% if system_user.shell %}
                                        <tr>
                                            <td>{% trans 'Shell' %}:</td>
                                            <td><b>{{ system_user.shell }}</b></td>
                                        </tr>
                                        {% endif %}
                                        {% if system_user.home %}
                                        <tr>
                                            <td>{% trans 'Home' %}:</td>
                                            <td><b>{{ system_user.home }}</b></td>
                                        </tr>
                                        {% endif %}
                                        {% if system_user.uid %}
                                            <tr>
                                                <td>{% trans 'Uid' %}:</td>
                                                <td><b>{{ system_user.uid }}</b></td>
                                            </tr>
                                        {% endif %}
                                        <tr>
                                            <td>{% trans 'Date created' %}:</td>
                                            <td><b>{{ system_user.date_created }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Created by' %}:</td>
                                            <td><b>{{ asset_group.created_by  }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Comment' %}:</td>
                                            <td><b>{{ system_user.comment }}</b></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>

                        <div class="col-sm-4" style="padding-left: 0;padding-right: 0">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Quick update' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table">
                                        <tbody>
                                        <tr class="no-borders-tr">
                                            <td width="50%">{% trans 'Auto push' %}:</td>
                                            <td>
                                                <span class="pull-right">
                                                    <div class="switch">
                                                        <div class="onoffswitch">
                                                            <input type="checkbox" {% if system_user.auto_push %} checked {% endif %} class="onoffswitch-checkbox" id="btn-auto-push">
                                                            <label class="onoffswitch-label" for="btn-auto-push">
                                                                <span class="onoffswitch-inner"></span>
                                                                <span class="onoffswitch-switch"></span>
                                                            </label>
                                                        </div>
                                                    </div>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr class="no-borders-tr">
                                            <td width="50%">{% trans 'Push system user manually' %}:</td>
                                            <td>
                                                <span style="float: right">
                                                    <button type="button" class="btn btn-primary btn-xs btn-push" style="width: 54px">{% trans 'Push' %}</button>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width="50%">{% trans 'Test assets connective' %}:</td>
                                            <td>
                                                <span style="float: right">
                                                    <button type="button" class="btn btn-primary btn-xs btn-test-connective" style="width: 54px">{% trans 'Test' %}</button>
                                                </span>
                                            </td>
                                        </tr>

{#                                        <tr>#}
{#                                            <td width="50%">{% trans 'Change auth period' %}:</td>#}
{#                                            <td>#}
{#                                                <span style="float: right">#}
{#                                                    <button type="button" class="btn btn-primary btn-xs" style="width: 54px">{% trans 'Reset' %}</button>#}
{#                                                </span>#}
{#                                            </td>#}
{#                                        </tr>#}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <div class="panel panel-info">
                                <div class="panel-heading">
                                    <i class="fa fa-info-circle"></i> {% trans 'Nodes' %}
                                </div>
                                <div class="panel-body">
                                    <table class="table cluster_edit" id="add-asset2group">
                                        <tbody>
                                        <form>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <select data-placeholder="{% trans 'Add to node' %}" id="cluster_selected" class="select2" style="width: 100%" multiple="" tabindex="4">
                                                        {% for node in nodes_remain %}
                                                        <option value="{{ node.id }}" id="opt_{{ node.id }}" >{{ node.name }}</option>
                                                        {% endfor %}
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <button type="button" class="btn btn-info btn-sm" id="btn-add-to-cluster">{% trans 'Confirm' %}</button>
                                                </td>
                                            </tr>
                                        </form>

                                        {% for node in system_user.nodes.all %}
                                        <tr>
                                          <td ><b class="bdg_node" data-gid={{ node.id }}>{{ node.name }}</b></td>
                                          <td>
                                              <button class="btn btn-danger pull-right btn-xs btn-remove-from-cluster" type="button"><i class="fa fa-minus"></i></button>
                                          </td>
                                        </tr>
                                        {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block custom_foot_js %}
<script>
function updateSystemUserCluster(clusters) {
    var the_url = "{% url 'api-assets:system-user-detail' pk=system_user.id %}";
    var body = {
        cluster: Object.assign([], clusters)
    };
    var success = function(data) {
        // remove all the selected groups from select > option and rendered ul element;
        $('.select2-selection__rendered').empty();
        $('#cluster_selected').val('');
        $.map(jumpserver.cluster_selected, function(cluster_name, index) {
            $('#opt_' + index).remove();
            // change tr html of user groups.
            $('.cluster_edit tbody').append(
                '<tr>' +
                '<td><b class="bdg_node" data-gid="' + index + '">' + cluster_name + '</b></td>' +
                '<td><button class="btn btn-danger btn-xs pull-right btn-remove-from-cluster" type="button"><i class="fa fa-minus"></i></button></td>' +
                '</tr>'
            )
        });
        // clear jumpserver.groups_selected
        jumpserver.cluster_selected = {};
    };
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify(body),
        success: success
    });
}
jumpserver.cluster_selected = {};
$(document).ready(function () {
    $('.select2').select2()
    .on('select2:select', function(evt) {
            var data = evt.params.data;
            jumpserver.cluster_selected[data.id] = data.text;
    })
    .on('select2:unselect', function(evt) {
        var data = evt.params.data;
        delete jumpserver.cluster_selected[data.id];
    });
})
.on('click', '#btn-auto-push', function () {
    var checked = $(this).prop('checked');
    var the_url = "{% url 'api-assets:system-user-detail' pk=system_user.id %}";
    var body = {
        'auto_push': checked
    };
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify(body)
    });
})
.on('click', '#btn-add-to-cluster', function() {
    if (Object.keys(jumpserver.cluster_selected).length === 0) {
        return false;
    }
    var clusters = $('.bdg_node').map(function() {
        return $(this).data('gid');
    }).get();
    $.map(jumpserver.cluster_selected, function(value, index) {
        clusters.push(index);
    });
    updateSystemUserCluster(clusters);
})
.on('click', '.btn-remove-from-cluster', function() {
    var $this = $(this);
    var $tr = $this.closest('tr');
    var $badge = $tr.find('.bdg_node');
    var gid = $badge.data('gid');
    var cluster_name = $badge.html() || $badge.text();
    $('#groups_selected').append(
        '<option value="' + gid + '" id="opt_' + gid + '">' + cluster_name + '</option>'
    );
    $tr.remove();
    var clusters = $('.bdg_node').map(function () {
        return $(this).data('gid');
    }).get();
    updateSystemUserCluster(clusters);
}).on('click', '.btn-del', function () {
    var $this = $(this);
    var name = "{{ system_user.name}}";
    var uid = "{{ system_user.id }}";
    var the_url = '{% url "api-assets:system-user-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
    var redirect_url = "{% url 'assets:system-user-list' %}";
    objectDelete($this, name, the_url, redirect_url);
})
.on('click', '.btn-push', function () {
    var the_url = "{% url 'api-assets:system-user-push' pk=system_user.id %}";
    var error = function (data) {
        alert(data)
    };
    APIUpdateAttr({
        url: the_url,
        error: error,
        method: 'GET',
        success_message: "{% trans "Task has been send, Go to ops task list seen result" %}"
    });
})
.on('click', '.btn-test-connective', function () {
    var the_url = "{% url 'api-assets:system-user-connective' pk=system_user.id %}";
    var error = function (data) {
        alert(data)
    };
    APIUpdateAttr({
        url: the_url,
        error: error,
        method: 'GET',
        success_message: "{% trans "Task has been send, seen left assets status" %}"
    });
})
</script>
{% endblock %}
