{% 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:domain-detail' pk=object.id %}"
                                   class="text-center"><i
                                        class="fa fa-laptop"></i> {% trans 'Detail' %}
                                </a>
                            </li>
                            <li class="active">
                                <a href="{% url 'assets:domain-detail' pk=object.id %}"
                                   class="text-center"><i
                                        class="fa fa-laptop"></i> {% trans 'Gateway' %}
                                </a>
                            </li>
                        </ul>
                    </div>
                    <div class="tab-content">
                        <div class="col-sm-12" style="padding-left: 0;">
                            <div class="" id="content_start">
                            </div>
                            <div class="ibox float-e-margins">
                                <div class="ibox-title">
                                    <span style="float: left"><b>{% trans 'Gateway list' %}</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">
                                    <div class="uc pull-left  m-r-5">
                                        <a href="{% url 'assets:domain-gateway-create' pk=object.id %}"
                                           class="btn btn-sm btn-primary"> {% trans "Create gateway" %} </a>
                                    </div>
                                    <table class="table table-striped table-bordered table-hover "
                                           id="domain_list_table">
                                        <thead>
                                        <tr>
                                            <th class="text-center">
                                                <input type="checkbox"
                                                       id="check_all"
                                                       class="ipt_check_all">
                                            </th>
                                            <th class="text-center">{% trans 'Name' %}</th>
                                            <th class="text-center">{% trans 'IP' %}</th>
                                            <th class="text-center">{% trans 'Port' %}</th>
                                            <th class="text-center">{% trans 'Protocol' %}</th>
                                            <th class="text-center">{% trans 'Username' %}</th>
                                            <th class="text-center">{% trans 'Comment' %}</th>
                                            <th class="text-center">{% trans 'Action' %}</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% include 'assets/_gateway_test_modal.html' %}
{% endblock %}
{% block content_bottom_left %}{% endblock %}
{% block custom_foot_js %}
<script>
function initTable() {
    var options = {
        ele: $('#domain_list_table'),
        columnDefs: [
            {targets: 7, createdCell: function (td, cellData, rowData) {
                var update_btn = '<a href="{% url "assets:domain-gateway-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-delete" data-uid="{{ DEFAULT_PK }}">{% trans "Delete" %}</a>'.replace('{{ DEFAULT_PK }}', cellData);
                var test_btn = '<a class="btn btn-xs btn-warning m-l-xs btn-test" data-uid="{{ DEFAULT_PK }}" data-port="PORT">{% trans "Test connection" %}</a>'.replace('{{ DEFAULT_PK }}', cellData).replace("PORT", rowData.port);
                if(rowData.protocol === 'rdp'){
                    test_btn = '<a class="btn btn-xs btn-warning m-l-xs btn-test" disabled data-uid="{{ DEFAULT_PK }}">{% trans "Test connection" %}</a>'.replace('{{ DEFAULT_PK }}', cellData);
                }
                $(td).html(update_btn + test_btn + del_btn)
             }}
        ],
        ajax_url: '{% url "api-assets:gateway-list" %}?domain={{ object.id }}',
        columns: [
            {data: "id"}, {data: "name" }, {data: 'ip'}, {data: 'port'},
            {data: "protocol", orderable: false}, {data: "username" }, {data: "comment" }, {data: "id", orderable: false}
        ],
        op_html: $('#actions').html()
    };
    jumpserver.initServerSideDataTable(options);
}
$(document).ready(function(){
    initTable();
})
.on('click', '.btn-delete', function () {
    var $this = $(this);
    var $data_table = $('#domain_list_table').DataTable();
    var name = $(this).closest("tr").find(":nth-child(2)").html();
    var uid = $this.data('uid');
    var the_url = '{% url "api-assets:gateway-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
    objectDelete($this, name, the_url);
    setTimeout( function () {
        $data_table.ajax.reload();
    }, 3000);
}).on('click', '.btn-test', function () {
    $("#ssh_test_port").val($(this).data('port'));
    $("#gateway_id").val($(this).data('uid'));
    $("#gateway_test").modal('show');

}).on('click', '#btn_gateway_test', function () {
    var data = $("#test_gateway_form").serializeObject();
    var uid = data.gateway_id;
    var the_url = '{% url "api-assets:test-gateway-connective" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
    requestApi({
        url: the_url,
        method: "POST",
        body: JSON.stringify({'port': parseInt(data.port)}),
        success_message: "{% trans 'Can be connected' %}",
        {#fail_message: "{% trans 'The connection fails' %}"#}
     })
});
</script>
{% endblock %}
