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

{% block help_message %}
    <div class="alert alert-info help-message">
    左侧是资产树，右击可以新建、删除、更改树节点，授权资产也是以节点方式组织的，右侧是属于该节点下的资产
    </div>
{% endblock %}

{% block custom_head_css_js %}
    <link href="{% static 'css/plugins/ztree/awesomeStyle/awesome.css' %}" rel="stylesheet">
    <script type="text/javascript" src="{% static 'js/plugins/ztree/jquery.ztree.all.min.js' %}"></script>
    <script src="{% static 'js/jquery.form.min.js' %}"></script>
	<style type="text/css">
        div#rMenu {
            position:absolute;
            visibility:hidden;
            text-align: left;
            top: 100%;
            left: 0;
            z-index: 1000;
            float: left;
            padding: 5px 0;
            margin: 2px 0 0;
            list-style: none;
            background-clip: padding-box;
        }
        div#rMenu li{
        	margin: 1px 0;
        	cursor: pointer;
        	{#list-style: none outside none;#}
        }
        .dropdown a:hover {
            background-color: #f1f1f1
        }
	</style>

{% endblock %}

{% block content %}
<div class="wrapper wrapper-content">
   <div class="row">
       <div class="col-lg-3" id="split-left" style="padding-left: 3px">
           <div class="ibox float-e-margins">
               <div class="ibox-content mailbox-content" style="padding-top: 0;padding-left: 1px">
                   <div class="file-manager ">
                       <div id="assetTree" class="ztree">
                       </div>

                       <div class="clearfix"></div>
                   </div>
               </div>
           </div>
       </div>
       <div class="col-lg-9 animated fadeInRight" id="split-right">
           <div class="tree-toggle">
               <div class="btn btn-sm btn-primary tree-toggle-btn" onclick="toggle()">
                   <i class="fa fa-angle-left fa-x" id="toggle-icon"></i>
               </div>
           </div>
           <div class="mail-box-header">
               <div class="uc pull-left m-r-5"><a class="btn btn-sm btn-primary btn-create-asset"> {% trans "Create asset" %} </a></div>
               <div class="html5buttons">
                   <div class="dt-buttons btn-group">
                       <a class="btn btn-default btn_import" data-toggle="modal" data-target="#asset_import_modal" tabindex="0">
                           <span>{% trans "Import" %}</span>
                       </a>
                       <a class="btn btn-default btn_export" tabindex="0">
                           <span>{% trans "Export" %}</span>
                       </a>
                   </div>
               </div>
               <div class="btn-group" style="float: right">
                  <button data-toggle="dropdown" class="btn btn-default btn-sm dropdown-toggle">{% trans 'Label' %} <span class="caret"></span></button>
                  <ul class="dropdown-menu labels">
                      {% for label in labels %}
                          <li><a style="font-weight: bolder">{{ label.name }}:{{ label.value }}</a></li>
                      {% endfor %}
                  </ul>
               </div>
               <table class="table table-striped table-bordered table-hover " id="asset_list_table" style="width: 100%">
                   <thead>
                       <tr>
                           <th class="text-center"><input type="checkbox" class="ipt_check_all"></th>
                           <th class="text-center">{% trans 'Hostname' %}</th>
                           <th class="text-center">{% trans 'IP' %}</th>
                           <th class="text-center">{% trans 'Hardware' %}</th>
                           <th class="text-center">{% trans 'Active' %}</th>
                           <th class="text-center">{% trans 'Reachable' %}</th>
                           <th class="text-center">{% trans 'Action' %}</th>
                       </tr>
                   </thead>
                   <tbody>
                   </tbody>
               </table>
               <div id="actions" class="hide">
                   <div class="input-group">
                       <select class="form-control m-b" style="width: auto" id="slct_bulk_update">
                           <option value="delete">{% trans 'Delete selected' %}</option>
                           <option value="update">{% trans 'Update selected' %}</option>
                           <option value="remove">{% trans 'Remove from this node' %}</option>
                           <option value="deactive">{% trans 'Deactive selected' %}</option>
                           <option value="active">{% trans 'Active selected' %}</option>
                       </select>
                       <div class="input-group-btn pull-left" style="padding-left: 5px;">
                           <button id='btn_bulk_update' style="height: 32px;"  class="btn btn-sm btn-primary">
                            {% trans 'Submit' %}
                           </button>
                       </div>
                   </div>
               </div>
           </div>
       </div>
   </div>
</div>

<div id="rMenu">
    <ul class="dropdown-menu">
        <li class="divider"></li>
        <li id="m_create" tabindex="-1" onclick="addTreeNode();"><a><i class="fa fa-plus-square-o"></i> {% trans 'Add node' %}</a></li>
        <li id="m_del" tabindex="-1" onclick="editTreeNode();"><a><i class="fa fa-pencil-square-o"></i> {% trans 'Rename node' %}</a></li>
        <li id="m_del" tabindex="-1" onclick="removeTreeNode();"><a><i class="fa fa-minus-square"></i> {% trans 'Delete node' %}</a></li>
        <li class="divider"></li>
        <li id="menu_asset_add" class="btn-add-asset" data-toggle="modal" data-target="#asset_list_modal" tabindex="0"><a><i class="fa fa-copy"></i> {% trans 'Add assets to node' %}</a></li>
        <li id="menu_asset_move" class="btn-move-asset" data-toggle="modal" data-target="#asset_list_modal" tabindex="0"><a><i class="fa fa-cut"></i> {% trans 'Move assets to node' %}</a></li>
        <li class="divider"></li>
        <li id="menu_refresh_hardware_info" class="btn-refresh-hardware" tabindex="-1"><a><i class="fa fa-refresh"></i> {% trans 'Refresh node hardware info' %}</a></li>
        <li id="menu_test_connective" class="btn-test-connective" tabindex="-1"><a><i class="fa fa-chain"></i> {% trans 'Test node connective' %}</a></li>
    </ul>
</div>

{% include 'assets/_asset_import_modal.html' %}
{% include 'assets/_asset_list_modal.html' %}
{% endblock %}

{% block custom_foot_js %}
<script>
var zTree, rMenu, asset_table, show = 0;
var update_node_action = "";
function initTable() {
    var options = {
        ele: $('#asset_list_table'),
        columnDefs: [
            {targets: 1, createdCell: function (td, cellData, rowData) {
                {% url 'assets:asset-detail' pk=DEFAULT_PK as the_url  %}
                var detail_btn = '<a href="{{ the_url }}">' + cellData + '</a>';
                $(td).html(detail_btn.replace('{{ DEFAULT_PK }}', rowData.id));
            }},
            {targets: 3, createdCell: function (td, cellData, rowData) {
                $(td).html(rowData.hardware_info)
            }},
            {targets: 4, createdCell: function (td, cellData) {
                if (!cellData) {
                    $(td).html('<i class="fa fa-times text-danger"></i>')
                } else {
                    $(td).html('<i class="fa fa-check text-navy"></i>')
                }
            }},
            {targets: 5, createdCell: function (td, cellData) {
                if (cellData === 'Unknown'){
                    $(td).html('<i class="fa fa-circle text-warning"></i>')
                } else if (!cellData) {
                    $(td).html('<i class="fa fa-circle text-danger"></i>')
                } else {
                    $(td).html('<i class="fa fa-circle text-navy"></i>')
                }
            }},
            {targets: 6, createdCell: function (td, cellData, rowData) {
                var update_btn = '<a href="{% url "assets:asset-update" pk=DEFAULT_PK %}" class="btn btn-xs btn-info">{% trans "Update" %}</a>'.replace("{{ DEFAULT_PK }}", cellData);
                var del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn_asset_delete" data-uid="{{ DEFAULT_PK }}">{% trans "Delete" %}</a>'.replace('{{ DEFAULT_PK }}', cellData);
                $(td).html(update_btn + del_btn)
            }}
        ],
        ajax_url: '{% url "api-assets:asset-list" %}',
        columns: [
            {data: "id"}, {data: "hostname" }, {data: "ip" },
            {data: "cpu_cores"}, {data: "is_active", orderable: false },
            {data: "is_connective", orderable: false}, {data: "id", orderable: false }
        ],
        op_html: $('#actions').html()
    };
    asset_table = jumpserver.initServerSideDataTable(options);
    return asset_table
}

function addTreeNode() {
	hideRMenu();
	var parentNode = zTree.getSelectedNodes()[0];
	if (!parentNode){
	    return
    }
    var url = "{% url 'api-assets:node-children' pk=DEFAULT_PK %}".replace("{{ DEFAULT_PK }}", parentNode.id );
    $.post(url,  {}, function (data, status){
        if (status === "success") {
            var newNode = {
                name: data["value"],
                id: data["id"],
                pId: parentNode.id
            };
            newNode.checked = zTree.getSelectedNodes()[0].checked;
            zTree.addNodes(parentNode, 0, newNode);
        } else {
            alert("{% trans 'Create node failed' %}")
        }
    });
}

function removeTreeNode() {
	hideRMenu();
	var current_node = zTree.getSelectedNodes()[0];
	if (!current_node){
	    return
    }
	if (current_node.children && current_node.children.length > 0) {
		toastr.error("{% trans 'Have child node, cancel' %}");
	 } else if (current_node.assets_amount !== 0) {
        toastr.error("{% trans 'Have assets, cancel' %}");
    } else {
        var url = "{% url 'api-assets:node-detail' pk=DEFAULT_PK %}".replace("{{ DEFAULT_PK }}", current_node.id );
		$.ajax({
            url: url,
            method: "DELETE",
            success: function () {
                zTree.removeNode(current_node);
            }
        });
	}
}

function editTreeNode() {
    hideRMenu();
	var current_node = zTree.getSelectedNodes()[0];
	if (!current_node){
	    return
    }
    if (current_node.value) {
        current_node.name = current_node.value;
    }
    zTree.editName(current_node);
}

function OnRightClick(event, treeId, treeNode) {
    if (!treeNode && event.target.tagName.toLowerCase() !== "button" && $(event.target).parents("a").length === 0) {
		zTree.cancelSelectedNode();
		showRMenu("root", event.clientX, event.clientY);
	} else if (treeNode && !treeNode.noR) {
		zTree.selectNode(treeNode);
		showRMenu("node", event.clientX, event.clientY);
	}
}

function showRMenu(type, x, y) {
	$("#rMenu ul").show();
    x -= 220;
    rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});

	$("body").bind("mousedown", onBodyMouseDown);
}

function beforeClick(treeId, treeNode, clickFlag) {
    return true;
}

function hideRMenu() {
	if (rMenu) rMenu.css({"visibility": "hidden"});
	$("body").unbind("mousedown", onBodyMouseDown);
}

function onBodyMouseDown(event){
	if (!(event.target.id === "rMenu" || $(event.target).parents("#rMenu").length>0)) {
		rMenu.css({"visibility" : "hidden"});
	}
}


function onRename(event, treeId, treeNode, isCancel){
    var url = "{% url 'api-assets:node-detail' pk=DEFAULT_PK %}".replace("{{ DEFAULT_PK }}", treeNode.id);
    var data = {"value": treeNode.name};
    if (isCancel){
        return
    }
    APIUpdateAttr({
        url: url,
        body: JSON.stringify(data),
        method: "PATCH"
    })
}

function onSelected(event, treeNode) {
    var url = asset_table.ajax.url();
    url = setUrlParam(url, "node_id", treeNode.id);
    setCookie('node_selected', treeNode.id);
    asset_table.ajax.url(url);
    asset_table.ajax.reload();
}

function selectQueryNode() {
    var query_node_id = $.getUrlParam("node");
    var cookie_node_id = getCookie('node_selected');
    var node;
    var node_id;

    if (query_node_id !== null) {
        node_id = query_node_id
    } else if (cookie_node_id !== null) {
        node_id = cookie_node_id;
    }

    node = zTree.getNodesByParam("id", node_id, null);
    if (node){
        zTree.selectNode(node[0]);
    }
}

function beforeDrag() {
    return true
}

function beforeDrop(treeId, treeNodes, targetNode, moveType) {
    var treeNodesNames = [];
    $.each(treeNodes, function (index, value) {
        treeNodesNames.push(value.value);
    });

    var msg = "你想移动节点: `" + treeNodesNames.join(",") + "` 到 `" + targetNode.value + "` 下吗?";
    if (confirm(msg)){
        return true
    } else {
        return false
    }
}

function onDrag(event, treeId, treeNodes) {
}

function onDrop(event, treeId, treeNodes, targetNode, moveType) {
    var treeNodesIds = [];
    $.each(treeNodes, function (index, value) {
        treeNodesIds.push(value.id);
    });

    var the_url = "{% url 'api-assets:node-add-children' pk=DEFAULT_PK %}".replace("{{ DEFAULT_PK }}", targetNode.id);
    var body = {nodes: treeNodesIds};
    APIUpdateAttr({
        url: the_url,
        method: "PUT",
        body: JSON.stringify(body)
    })
}

function initTree() {
    var setting = {
        view: {
            dblClickExpand: false,
            showLine: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        edit: {
            enable: true,
			showRemoveBtn: false,
			showRenameBtn: false,
            drag: {
                isCopy: true,
                isMove: true
            }
        },
        callback: {
            onRightClick: OnRightClick,
            beforeClick: beforeClick,
            onRename: onRename,
            onSelected: onSelected,
            beforeDrag: beforeDrag,
            onDrag: onDrag,
            beforeDrop: beforeDrop,
            onDrop: onDrop
        }
    };

    var zNodes = [];
    $.get("{% url 'api-assets:node-list' %}", function(data, status){
        $.each(data, function (index, value) {
            value["pId"] = value["parent"];
            {#if (value["key"] === "0") {#}
            value["open"] = true;
            {# }#}
            value["name"] = value["value"] + ' (' + value['assets_amount'] + ')';
            value['value'] = value['value'];
        });
        zNodes = data;
        $.fn.zTree.init($("#assetTree"), setting, zNodes);
        zTree = $.fn.zTree.getZTreeObj("assetTree");
		rMenu = $("#rMenu");
		selectQueryNode();
    });
}

function toggle() {
    if (show === 0) {
        $("#split-left").hide(500, function () {
            $("#split-right").attr("class", "col-lg-12");
            $("#toggle-icon").attr("class", "fa fa-angle-right fa-x");
            show = 1;
        });
    } else {
        $("#split-right").attr("class", "col-lg-9");
        $("#toggle-icon").attr("class", "fa fa-angle-left fa-x");
        $("#split-left").show(500);
        show = 0;
    }
}

$(document).ready(function(){
    initTable();
    initTree();
})
.on('click', '.labels li', function () {
    var val = $(this).text();
    $("#asset_list_table_filter input").val(val);
    asset_table.search(val).draw();
 })
.on('click', '.btn_export', function () {
    var $data_table = $('#asset_list_table').DataTable();
    var rows = $data_table.rows('.selected').data();
    var nodes = zTree.getSelectedNodes();
    var current_node;
    if (nodes && nodes.length === 1) {
        current_node = nodes[0];
    }
    var assets = [];
    $.each(rows, function (index, obj) {
        assets.push(obj.id)
    });
    $.ajax({
        url: "{% url "assets:asset-export" %}",
        method: 'POST',
        data: JSON.stringify({assets_id: assets, node_id: current_node.id}),
        dataType: "json",
        success: function (data, textStatus) {
            window.open(data.redirect)
        },
        error: function () {
            toastr.error('Export failed');
        }
    })
})
.on('click', '#btn_asset_import', function () {
    var $form = $('#fm_asset_import');
    var action = $form.attr("action");
    var nodes = zTree.getSelectedNodes();
    var current_node;
    if (nodes && nodes.length ===1 ){
        current_node = nodes[0];
        action = setUrlParam(action, 'node_id', current_node.id);
        {#action += "?node_id=" + current_node.id;#}
        $form.attr("action", action)
    }
    $form.find('.help-block').remove();
    function success (data) {
        if (data.valid === false) {
            $('<span />', {class: 'help-block text-danger'}).html(data.msg).insertAfter($('#id_assets'));
        } else {
            $('#id_created').html(data.created_info);
            $('#id_created_detail').html(data.created.join(', '));
            $('#id_updated').html(data.updated_info);
            $('#id_updated_detail').html(data.updated.join(', '));
            $('#id_failed').html(data.failed_info);
            $('#id_failed_detail').html(data.failed.join(', '));
            var $data_table = $('#asset_list_table').DataTable();
            $data_table.ajax.reload();
        }
    }
    $form.ajaxSubmit({success: success});
})
.on('click', '.btn-create-asset', function () {
    var url = "{% url 'assets:asset-create' %}";
    var nodes = zTree.getSelectedNodes();
    var current_node;
    if (nodes && nodes.length ===1 ){
        current_node = nodes[0];
        url += "?node_id=" + current_node.id;
    }
    window.open(url, '_self');
})
.on('click', '.btn-refresh-hardware', function () {
    var url = "{% url 'api-assets:node-refresh-hardware-info' pk=DEFAULT_PK %}";
    var nodes = zTree.getSelectedNodes();
    var current_node;
    if (nodes && nodes.length ===1 ){
        current_node = nodes[0];
    } else {
        return null;
    }

    var the_url = url.replace("{{ DEFAULT_PK }}", current_node.id);
    function success(data) {
        rMenu.css({"visibility" : "hidden"});
        var task_id = data.task;
        var url = '{% url "ops:celery-task-log" pk=DEFAULT_PK %}'.replace("{{ DEFAULT_PK }}", task_id);
        window.open(url, '', 'width=800,height=600')
    }
    APIUpdateAttr({
        url: the_url,
        method: "GET",
        success: success,
        flash_message: false
    });

})
.on('click', '.btn-test-connective', function () {
    var url = "{% url 'api-assets:node-test-connective' pk=DEFAULT_PK %}";
    var nodes = zTree.getSelectedNodes();
    var current_node;
    if (nodes && nodes.length ===1 ){
        current_node = nodes[0];
    } else {
        return null;
    }

    var the_url = url.replace("{{ DEFAULT_PK }}", current_node.id);
    function success(data) {
        rMenu.css({"visibility" : "hidden"});
        var task_id = data.task;
        var url = '{% url "ops:celery-task-log" pk=DEFAULT_PK %}'.replace("{{ DEFAULT_PK }}", task_id);
        window.open(url, '', 'width=800,height=600')
    }
    APIUpdateAttr({
        url: the_url,
        method: "GET",
        success: success,
        flash_message: false
    });
})
.on('click', '.btn_asset_delete', function () {
    var $this = $(this);
    var $data_table = $("#asset_list_table").DataTable();
    var name = $(this).closest("tr").find(":nth-child(2)").children('a').html();
    var uid = $this.data('uid');
    var the_url = '{% url "api-assets:asset-detail" pk=DEFAULT_PK %}'.replace("{{ DEFAULT_PK }}", uid);
    objectDelete($this, name, the_url);
    setTimeout( function () {
        $data_table.ajax.reload();
    }, 3000);
})
.on('click', '#btn_bulk_update', function () {
    var action = $('#slct_bulk_update').val();
    var $data_table = $('#asset_list_table').DataTable();
    var id_list = [];
    $data_table.rows({selected: true}).every(function(){
        id_list.push(this.data().id);
    });
    if (id_list.length === 0) {
        return false;
    }
    var the_url = "{% url 'api-assets:asset-list' %}";

    function doDeactive() {
        var data = [];
        $.each(id_list, function(index, object_id) {
            var obj = {"pk": object_id, "is_active": false};
            data.push(obj);
        });
        function success() {
            asset_table.ajax.reload()
        }
        APIUpdateAttr({
            url: the_url,
            method: 'PATCH',
            body: JSON.stringify(data),
            success: success
        });
    }
    function doActive() {
        var data = [];
        $.each(id_list, function(index, object_id) {
            var obj = {"pk": object_id, "is_active": true};
            data.push(obj);
        });
        function success() {
            asset_table.ajax.reload()
        }
        APIUpdateAttr({
            url: the_url,
            method: 'PATCH',
            body: JSON.stringify(data),
            success: success
        });
    }
    function doDelete() {
        swal({
            title: "{% trans 'Are you sure?' %}",
            text: "{% trans 'This will delete the selected assets !!!' %}",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "{% trans 'Confirm' %}",
            closeOnConfirm: false
        }, function() {
            var success = function() {
                var msg = "{% trans 'Asset Deleted.' %}";
                swal("{% trans 'Asset Delete' %}", msg, "success");
                $('#asset_list_table').DataTable().ajax.reload();
            };
            var fail = function() {
                var msg = "{% trans 'Asset Deleting failed.' %}";
                swal("{% trans 'Asset Delete' %}", msg, "error");
            };
            var url_delete = the_url + '?id__in=' + JSON.stringify(id_list);
            APIUpdateAttr({
                url: url_delete,
                method: 'DELETE',
                success: success,
                error: fail
            });
            $data_table.ajax.reload();
            jumpserver.checked = false;
        });
    }
    function doUpdate() {
        var id_list_string = id_list.join(',');
        var url = "{% url 'assets:asset-bulk-update' %}?assets_id=" + id_list_string;
        location.href = url
    }

    function doRemove() {
        var current_node;
        var nodes = zTree.getSelectedNodes();
        if (nodes && nodes.length === 1) {
            current_node = nodes[0]
        } else {
            return
        }

        var data = {
            'assets': id_list
        };

        var success = function () {
            asset_table.ajax.reload()
        };

        APIUpdateAttr({
            'url': '/api/assets/v1/nodes/' + current_node.id + '/assets/remove/',
            'method': 'PUT',
            'body': JSON.stringify(data),
            'success': success
        })
    }
    switch(action) {
        case 'deactive':
            doDeactive();
            break;
        case 'delete':
            doDelete();
            break;
        case 'update':
            doUpdate();
            break;
        case 'active':
            doActive();
            break;
        case 'remove':
            doRemove();
            break;
        default:
            break;
    }
    $(".ipt_check_all").prop("checked", false)
})
.on('click', '#btn_asset_modal_confirm', function () {
    var assets_selected = asset_table2.selected;
    var current_node;
    var nodes = zTree.getSelectedNodes();
    if (nodes && nodes.length === 1) {
        current_node = nodes[0]
    } else {
        return
    }

    var data = {
        'assets': assets_selected
    };
    var success = function () {
        asset_table2.selected = [];
        asset_table2.ajax.reload()
    };

    var url = '';
    if (update_node_action === "move") {
        url = "{% url 'api-assets:node-replace-assets' pk=DEFAULT_PK %}".replace("{{ DEFAULT_PK }}", current_node.id);
    } else {
        url = "{% url 'api-assets:node-add-assets' pk=DEFAULT_PK %}".replace("{{ DEFAULT_PK }}", current_node.id);
    }

    APIUpdateAttr({
        'url': url,
        'method': 'PUT',
        'body': JSON.stringify(data),
        'success': success
    })
}).on('hidden.bs.modal', '#asset_list_modal', function () {
    window.location.reload();
}).on('click', '#menu_asset_add', function () {
    update_node_action = "add"
}).on('click', '#menu_asset_move', function () {
    update_node_action = "move"
})
</script>

{% endblock %}