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

{% block custom_head_css_js %}
    <style>
    .table.node_edit {
        margin-bottom: 0;
    }
    </style>
{% 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>
                                <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 class="active">
                                <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>
                        </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 style="float: left">{% trans 'Assets of ' %} <b>{{ system_user.name }} </b><span class="badge">{{ paginator.count }}</span></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">
                                    {% include 'assets/_asset_user_list.html' %}
                                </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 '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>
                                        {% if system_user.auto_push %}
                                        <tr>
                                            <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 %}
                                        </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 node_edit" id="add-asset2group">
                                        <tbody>
                                        <form>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <select data-placeholder="{% trans 'Add to node' %}" id="node_selected" class="nodes-select2" style="width: 100%" multiple="" tabindex="4">
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="2" class="no-borders">
                                                    <button type="button" class="btn btn-info btn-sm" id="btn-add-to-node">{% trans 'Confirm' %}</button>
                                                </td>
                                            </tr>
                                        </form>
                                        <table class="table" id="node_list_table">
                                        </table>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block custom_foot_js %}
<script>
var assetsRelationUrl = "{% url 'api-assets:system-users-assets-relation-list' %}";
var nodesRelationUrl = "{% url 'api-assets:system-users-nodes-relation-list' %}";

function getRelationUrl(type) {
    var theUrl = "";
    switch (type) {
        case "asset":
            theUrl = assetsRelationUrl;
            break;
        case "node":
            theUrl = nodesRelationUrl;
            break;
    }
    return theUrl;
}

function addObjects(objectsId, type) {
    if (!objectsId || objectsId.length === 0) {
        return
    }
    var theUrl = getRelationUrl(type);
    var body = [];
    objectsId.forEach(function (v) {
        var data = {systemuser: "{{ object.id }}"};
        data[type] = v;
        body.push(data)
    });
    requestApi({
        url: theUrl,
        body: JSON.stringify(body),
        method: "POST",
        success: reloadPage
    });
}

function removeObject(objectId, type, success) {
    if (!objectId)  {
        return
    }
    var theUrl = getRelationUrl(type);
    theUrl = setUrlParam(theUrl, 'systemuser', "{{ object.id }}");
    theUrl = setUrlParam(theUrl, type, objectId);
    if (!success) {
        success = reloadPage
    }
    requestApi({
        url: theUrl,
        method: "DELETE",
        success: success
    });
}

function initNodeTable() {
    var theUrl = setUrlParam(nodesRelationUrl, "systemuser", "{{ object.id }}");
    var options = {
        ele: $('#node_list_table'),
        toggle: true,
        columnDefs: [
             {targets: 1, createdCell: function (td, cellData) {
                  var removeBtn = '<button class="btn btn-danger pull-right btn-xs btn-remove-from-node" data-uid="UID" type="button"><i class="fa fa-minus"></i></button>'
                      .replace('UID', cellData);
		  		  $(td).html(removeBtn);
             }}
        ],
        ajax_url: theUrl,
        dom: "trp",
        hideDefaultDefs: true,
        columns: [
            {data: "node_display", orderable: false},
            {data: "node", orderable: false},
        ],
    };
    table = jumpserver.initServerSideDataTable(options);
    return table
}


$(document).ready(function () {
	$('.select2').select2();
    nodesSelect2Init(".nodes-select2");
	assetUserListUrl = setUrlParam(assetUserListUrl, "system_user_id", "{{ system_user.id }}");
    needPush = true;
    initAssetUserTable();
    initNodeTable();
})
.on('click', '.btn-remove-from-node', function() {
    var $this = $(this);
    var nodeId = $(this).data('uid');
    var success = function() {
        var $tr = $this.closest('tr');
        $tr.remove();
    };
    removeObject(nodeId, "node", success)
})
.on('click', '#btn-add-to-node', function() {
    var nodes = $("#node_selected").val();
    addObjects(nodes, "node");
})
.on('click', '.btn-push', function () {
    var theUrl = "{% url 'api-assets:system-user-push' pk=system_user.id %}";
    var error = function (data) {
        alert(data)
    };
    var success = function (data) {
        var taskId = data.task;
        showCeleryTaskLog(taskId);
    };
    requestApi({
        url: theUrl,
        error: error,
        method: 'GET',
        success: success
    });
})
.on('click', '.btn-push-auth', function () {
    var $this = $(this);
    var asset_id = $this.data('asset');
    var theUrl = "{% url 'api-assets:system-user-push-to-asset' pk=object.id aid=DEFAULT_PK %}";
    theUrl = theUrl.replace("{{ DEFAULT_PK }}", asset_id);
    var success = function (data) {
        var taskId = data.task;
        showCeleryTaskLog(taskId);
    };
    var error = function (data) {
        alert(data)
    };
    requestApi({
        url: theUrl,
        method: 'GET',
        success: success,
        error: error
    })
})
.on('click', '.btn-test-connective', function () {
    var theUrl = "{% url 'api-assets:system-user-connective' pk=system_user.id %}";
    var error = function (data) {
        alert(data)
    };
    var success = function (data) {
        var taskId = data.task;
        showCeleryTaskLog(taskId);
    };
    requestApi({
        url: theUrl,
        error: error,
        method: 'GET',
        success: success
    });
})
</script>
{% endblock %}
