{% extends '_base_list.html' %}
{% load i18n static %}
{% block table_search %}
{% endblock %}

{% block table_container %}
<div class="uc pull-left m-r-5">
    <a href="{% url "assets:platform-create" %}" class="btn btn-sm btn-primary"> {% trans "Create platform" %} </a>
</div>
<table class="table table-striped table-bordered table-hover " id="platform_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 'Base platform' %}</th>
        <th class="text-center">{% trans 'Comment' %}</th>
        <th class="text-center">{% trans 'Action' %}</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>
{% endblock %}
{% block content_bottom_left %}{% endblock %}
{% block custom_foot_js %}
<script>
var platformTable = 0;
function initTable() {
    var options = {
        ele: $('#platform_list_table'),
        columnDefs: [
            {targets: 1, render: function (cellData, tp, rowData, meta) {
                cellData = htmlEscape(cellData);
                var detailBtn = '<a href="{% url "assets:platform-detail" pk=999 %}">' + cellData + '</a>';
                return detailBtn.replace('999', rowData.id);
             }},
            {targets: 4, createdCell: function (td, cellData, rowData) {
                var updateBtn = '<a href="{% url "assets:platform-update" pk=9999 %}" class="btn btn-xs m-l-xs btn-info" disabled>{% trans "Update" %}</a>'.replace('9999', cellData);
                var delBtn = '<a class="btn btn-xs btn-danger m-l-xs btn-object-delete" data-uid="{{ DEFAULT_PK }}" disabled>{% trans "Delete" %}</a>'.replace('{{ DEFAULT_PK }}', cellData);
                if (!rowData.internal) {
                    updateBtn = updateBtn.replace('disabled', '');
                    delBtn = delBtn.replace('disabled', '');
                }

                $(td).html(updateBtn + delBtn)
             }}
        ],
        ajax_url: '{% url "api-assets:platform-list" %}',
        columns: [
            {data: "id"}, {data: "name"}, {data: "base" },
            {data: "comment"}, {data: "id", orderable: false, width: "120px"}
        ]
    };
    platformTable = jumpserver.initServerSideDataTable(options);
    return platformTable
}

$(document).ready(function(){
    initTable();
})
.on('click', '.btn-object-delete', function () {
	var $this = $(this);
	var name = $(this).closest("tr").find(":nth-child(2)").children('a').html();
	var uid = $this.data('uid');
	var theUrl = '{% url "api-assets:platform-detail" pk=0 %}'.replace('0', uid);
	objectDelete($this, name, theUrl);
	setTimeout( function () {
        platformTable.ajax.reload();
    }, 3000);

})
</script>
{% endblock %}
