{% 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>
	<style type="text/css">

	</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:cluster-detail' pk=cluster.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Detail' %} </a>
                            </li>
                            <li class="active"><a href="{% url 'assets:cluster-assets' pk=cluster.id %}" class="text-center">
                                <i class="fa fa-bar-chart-o"></i> {% trans 'Cluster 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 'Cluster assets' %} <b>{{ cluster.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 table-hover " id="cluster_assets_table" >
                                        <thead>
                                            <tr>
                                                <th>{% trans 'Hostname' %}</th>
                                                <th>{% trans 'IP' %}</th>
                                                <th>{% trans 'Port' %}</th>
	                                            <th>{% trans 'Type' %}</th>
                                                <th>{% trans 'Alive' %}</th>
	                                            <th>{% trans 'Action' %}</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        </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 'Test assets connective' %}:</td>
                                            <td>
                                                <span style="float: right">
                                                    <button type="button" class="btn btn-primary btn-xs" id="btn-test-assets" style="width: 54px">{% trans 'Run' %}</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 'Add assets to' %} {{ cluster.name }}
                                </div>
                                <div class="panel-body">
                                    <table class="table">
                                        <tbody>
                                        <form>
                                            <tr class="no-borders-tr">
                                                <td colspan="2">
                                                    <select data-placeholder="{% trans 'Select asset' %}" class="select2" style="width: 100%" multiple="" tabindex="4">
                                                        {% for asset in assets_remain %}
                                                            <option value="{{ asset.id }}" id="opt_{{ asset.id }}">{{ asset.hostname}}</option>
                                                        {% endfor %}
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr class="no-borders-tr">
                                                <td colspan="2">
                                                    <button type="button" class="btn btn-info btn-sm btn-add-assets">{% trans 'Confirm' %}</button>
                                                </td>
                                            </tr>
                                        </form>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block custom_foot_js %}
<script src="{% static 'js/jquery.form.min.js' %}"></script>
<script>

jumpserver.assets_selected = {};

Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
		if (index > -1) {
		this.splice(index, 1);
	}
};

function addAssets(assets) {
    var the_url = "{% url 'api-assets:cluster-add-assets' pk=cluster.id %}";
    var body = {
        assets: assets
    };
	var $data_table = $("#cluster_assets_table").DataTable();
    var success = function(data) {
        $('.select2-selection__rendered').empty();
		$data_table.ajax.reload();
        jumpserver.groups_selected = {};
    };
    APIUpdateAttr({
        url: the_url,
        body: JSON.stringify(body),
	    method: 'PUT',
        success: success
    });
}


function initTable() {
    var options = {
		ele: $('#cluster_assets_table'),
		buttons: [],
		order: [],
		columnDefs: [
			{targets: 0, createdCell: function (td, cellData, rowData) {
				var detail_btn = '<a href="{% url "assets:asset-detail" pk=DEFAULT_PK %}" data-aid="'+rowData.id+'">' + cellData + '</a>';
				$(td).html(detail_btn.replace('{{ DEFAULT_PK }}', rowData.id));
			}},
			{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, 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 }}', rowData.id);
				$(td).html(update_btn)
			}}
		],
		ajax_url: '{% url "api-assets:asset-list" %}?cluster_id={{ cluster.id }}',
		columns: [{data: "hostname" }, {data: "ip" }, {data: "port" },
			{data: "get_type_display" }, {data: "is_connective" }, {data: "id"}],
		op_html: $('#actions').html()
	};
	jumpserver.initServerSideDataTable(options);

}

$(document).ready(function () {
	$('.select2').select2()
	.on("select2:select", function (evt) {
		var data = evt.params.data;
		jumpserver.assets_selected[data.id] = data.text;
	})
	.on('select2:unselect', function(evt) {
		var data = evt.params.data;
		delete jumpserver.assets_selected[data.id];
	});
	initTable();
})
.on('click', '.btn-add-assets', function () {
	if (Object.keys(jumpserver.assets_selected).length === 0) {
		return false;
	}
	var assets_id = [];
    $.map(jumpserver.assets_selected, function(value, index) {
        assets_id.push(index);
    });

    addAssets(assets_id);
})
.on('click', '#btn-test-assets', function () {
    var the_url = "{% url 'api-assets:cluster-test-connective' pk=cluster.id %}";
    APIUpdateAttr({
        url: the_url,
	    method: 'GET',
        success_message: "{% trans 'Task has been send, seen left assets status' %}"
    });
})
</script>
{% endblock %}
