{% 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>
                                <a href="{% url 'assets:admin-user-detail' pk=admin_user.pk %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Detail' %} </a>
                            </li>
                            <li class="active">
                                <a href="{% url 'assets:admin-user-assets' pk=admin_user.pk %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Assets list' %} </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 'Asset list of ' %} <b>{{ admin_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 table-striped table-bordered table-hover" id="asset_list_table">
                                        <thead>
                                            <tr>
                                                <th>{% trans 'Hostname' %}</th>
                                                <th>{% trans 'IP' %}</th>
                                                <th>{% trans 'Port' %}</th>
                                                <th>{% trans 'Reachable' %}</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 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>
                </div>
            </div>
        </div>
    </div>
   {% include 'assets/_asset_user_auth_modal.html' %}
{% endblock %}
{% block custom_foot_js %}
<script>

function initTable() {
    var reachable = {{ admin_user.REACHABLE }};
    var unreachable = {{ admin_user.UNREACHABLE }};
    var options = {
		ele: $('#asset_list_table'),
		buttons: [],
		order: [],
		columnDefs: [
			{targets: 0, createdCell: function (td, cellData, rowData) {
			    cellData = htmlEscape(cellData);
				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: 3, createdCell: function (td, cellData) {
				if (cellData === unreachable) {
					$(td).html('<i class="fa fa-times text-danger"></i>')
				} else if (cellData === reachable) {
					$(td).html('<i class="fa fa-check text-navy"></i>')
				} else {
                    $(td).html('')
                }
			}},
            {targets: 4, createdCell: function (td, cellData, rowData) {
                var test_btn = ' <a class="btn btn-xs btn-info btn-test-asset" data-uid="{{ DEFAULT_PK }}" >{% trans "Test" %}</a>'.replace("{{ DEFAULT_PK }}", cellData);
                var update_auth_btn = ' <a class="btn btn-xs btn-primary btn-update-asset-user-auth" data-aid="{{ DEFAULT_PK }}" data-hostname="hostname777">{% trans "Update auth" %}</a>'.replace("{{ DEFAULT_PK }}", cellData).replace("hostname777", rowData.hostname);
                $(td).html(test_btn + update_auth_btn);
			}}
        ],

		ajax_url: '{% url "api-assets:admin-user-assets" pk=admin_user.id %}',
		columns: [
		    {data: "hostname" }, {data: "ip" },
            {data: "port" },  {data: "connectivity" }, {data: "id"}],
		op_html: $('#actions').html()
	};
	jumpserver.initServerSideDataTable(options);
}

function initAssetUserAuthModalForm(hostname, username){
    $('#id_hostname_p').html(hostname);
    $('#id_username_p').html(username);
    $('#id_password').parent().removeClass('has-error');
    $('#id_password').val('');
}

var assetId ;

$(document).ready(function () {
	initTable();
})
.on('click', '.btn-test-asset', function () {
    var asset_id = $(this).data('uid');
    var the_url = "{% url 'api-assets:asset-alive-test' pk=DEFAULT_PK %}".replace('{{ DEFAULT_PK }}', asset_id);
    var success = function (data) {
        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,left=400,top=400')
    };
    APIUpdateAttr({
        url: the_url,
        method: 'GET',
        success: success,
        flash_message: false
    });
})
.on('click', '.btn-test-connective', function () {
    var the_url = "{% url 'api-assets:admin-user-connective' pk=admin_user.id %}";
    var success = function (data) {
        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,left=400,top=400')
    };
    APIUpdateAttr({
        url: the_url,
        method: 'GET',
        success: success,
        flash_message: false
    });
})
.on('click', '.btn-update-asset-user-auth', function() {
    assetId = $(this).data('aid');
    var hostname = $(this).data('hostname');
    var username = '{{ admin_user.username }}';
    initAssetUserAuthModalForm(hostname, username);
    $("#asset_user_auth_modal").modal();
})
.on('click', '#btn_asset_user_auth_modal_confirm', function(){
    var password = $('#id_password').val();
    if (password){
        var data = {
            'name': "{{ admin_user.username }}",
            'asset': assetId,
            'username': "{{ admin_user.username }}",
            'password': password
        };
        formSubmit({
            data: data,
            url: "{% url 'api-assets:asset-user-list' %}",
            method: 'POST',
            success: function () {
                toastr.success("{% trans 'Update successfully!' %}");
            },
            error: function () {
                toastr.error("{% trans 'Update failed!' %}");
            }
        });
        $("#asset_user_auth_modal").modal('hide');
    }
    else{
        $('#id_password').parent().addClass('has-error');
    }
})
</script>
{% endblock %}
