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

{% 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 '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 'Login mode' %}:</td>
                                            <td><b>{{ system_user.get_login_mode_display }}</b></td>
                                        </tr>
                                        <tr>
                                            <td>{% trans 'Protocol' %}:</td>
                                            <td><b id="id_protocol_type">{{ system_user.protocol }}</b></td>
                                        </tr>
                                        <tr class="only-ssh">
                                            <td>{% trans 'Sudo' %}:</td>
                                            <td><b>{{ system_user.sudo }}</b></td>
                                        </tr>
                                        {% if system_user.shell %}
                                        <tr class="only-ssh">
                                            <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="only-ssh-rdp">
                                            <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>
                                        {% if system_user.auto_push %}
                                        <tr class="only-ssh-rdp">
                                            <td width="50%">{% trans 'Push system user now' %}:</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>
                                        {% endif %}
                                        <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>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                    {% if system_user.protocol != 'rdp' %}
                    <div class="col-sm-4" style="padding-left: 0; padding-right: 0">
                        <div class="panel panel-info">
                            <div class="panel-heading">
                                <i class="fa fa-info-circle"></i> {% trans 'Command filter' %}
                            </div>
                            <div class="panel-body">
                                <table class="table">
                                <tbody>
                                <form>
                                    <tr>
                                        <td colspan="2" class="no-borders">
                                            <select data-placeholder="{% trans 'Binding command filters' %}"  id="command_filters_selected"  class="select2" style="width: 100%" multiple="" tabindex="4">
                                            {% for cf in cmd_filters_remain %}
                                                <option value="{{ cf.id }}" id="opt_{{ cf.id }}" >{{ cf }}</option>
                                            {% endfor %}
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2" class="no-borders">
                                            <button type="button" class="btn btn-info btn-sm" id="btn-binding-command-filters">{% trans 'Confirm' %}</button>
                                        </td>
                                    </tr>
                                </form>
                                {% for cf in object.cmd_filters.all %}
                                <tr>
                                  <td><b class="bdg-command-filters" data-gid={{ cf.id }}>{{ cf }}</b></td>
                                  <td>
                                      <button class="btn btn-danger pull-right btn-xs btn-unbound-command-filter" data-gid={{ cf.id }} type="button"><i class="fa fa-minus"></i></button>
                                  </td>
                                </tr>
                                {% endfor %}
                                </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block custom_foot_js %}
<script>
function updateCommandFilters(command_filters) {
    var the_url = "{% url 'api-assets:system-user-detail' pk=system_user.id %}";
    var body = {
        cmd_filters: Object.assign([], command_filters)
    };
    var success = function(data) {
        location.reload();
    };
    requestApi({
        url: the_url,
        body: JSON.stringify(body),
        success: success
    });
}
$(document).ready(function () {
    var protocol = $('#id_protocol_type').text();
    if(protocol !== 'ssh'){
        $('.only-ssh').addClass("hidden")
    }
    if(["ssh", "rdp"].indexOf(protocol) === -1){
        $('.only-ssh-rdp').addClass('hidden');
    }
    $(".panel-body .table tr:visible:first").addClass('no-borders-tr');
    $('.select2').select2()
})
.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
    };
    requestApi({
        url: the_url,
        body: JSON.stringify(body)
    });
}).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 success = function (data) {
        var task_id = data.task;
        showCeleryTaskLog(task_id);
    };
    requestApi({
        url: the_url,
        method: 'GET',
        success: success,
        flash_message: false
    });
})
.on('click', '.btn-test-connective', function () {
    var the_url = "{% url 'api-assets:system-user-connective' pk=system_user.id %}";
    var success = function (data) {
        var task_id = data.task;
        showCeleryTaskLog(task_id);
    };
    requestApi({
        url: the_url,
        method: 'GET',
        success: success,
        flash_message: false
    });
}).on('click', '#btn-binding-command-filters', function () {
    var new_selected_cmd_filters = $.map($('#command_filters_selected').select2('data'), function (i) {
        return i.id;
    });
    var origin_cmd_filters = $.map($(".bdg-command-filters"), function (s) {
        return $(s).data('gid')
    });
    var command_filters = new_selected_cmd_filters.concat(origin_cmd_filters);
    updateCommandFilters(command_filters)
}).on('click', '.btn-unbound-command-filter', function () {
    var unbound_command_filter = $(this).data('gid');
    var origin_command_filters = $.map($(".bdg-command-filters"), function (s) {
        return $(s).data('gid')
    });
    var command_filters = $.grep(origin_command_filters, function (n, i) {
        return n !== unbound_command_filter
    });
    updateCommandFilters(command_filters)
})
</script>
{% endblock %}
