{% 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>
{% endblock %}
{% block content_bottom_left %}{% endblock %}
{% block custom_foot_js %}
<script>
function initTable() {
    var options = {
        ele: $('#domain_list_table'),
        columnDefs: [
            {targets: 1, createdCell: function (td, cellData, rowData) {
                var detail_btn = '<a href="{% url "assets:domain-detail" pk=DEFAULT_PK %}">' + cellData + '</a>';
                $(td).html(detail_btn.replace('{{ DEFAULT_PK }}', rowData.id));
             }},

            {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 }}">{% 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"}, {data: "username" }, {data: "comment" }, {data: "id"}
        ],
        op_html: $('#actions').html()
    };
    jumpserver.initDataTable(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)").children('a').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 () {
    var $this = $(this);
    var uid = $this.data('uid');
    var the_url = '{% url "api-assets:test-gateway-connective" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
    APIUpdateAttr({
        url: the_url,
        method: "GET",
        success_message: "可连接",
        fail_message: "连接失败"
    })

})
</script>
{% endblock %}
