var tableBiblioteca = $('#tb-biblioteca').DataTable({
    bFilter: true,
    "bLengthChange": false,
    bInfo: true,
    "searching": true,
    responsive: true,
    "autoWidth": true,
    processing: true,
    serverSide: true,
    ajax: {
        url: url_biblioteca,
        method: 'POST',
        "data": function(d){
            d.code = code_user;
        }
    },
    "language": {
        "lengthMenu": "Mostrar _MENU_ Entradas",
            "zeroRecords": "No se encontraron registros",
            "info": "Mostrando la página _PAGE_ de _PAGES_",
            "infoEmpty": "No hay registros disponibles",
            "infoFiltered": "(filtrado de _MAX_ registros totales)",
            "search": "Buscar: ",
            "paginate": {
            "first": "Primero",
                "last": "Último",
                "next": "Siguiente",
                "previous": "Anterior"
        }
    },
    columns: [
        {
            data:   "id",
            render: function ( data, type, row ) {
                if ( type === 'display' ) {
                    return '<input type="checkbox" name="chkbx-tes" class="editor-active" data-id="'+data+'">';
                }
                return data;
            },
            className: "dt-body-center"
        },
        {data: 'CodigoUsuario', name: 'CodigoUsuario'},
        {data: 'CodigoDeuda', name: 'CodigoDeuda'},
        {data: 'Nombre', name: 'Nombre'},
        {data: 'FechaDevolucion', name: 'FechaDevolucion'},
        {data: 'MontoPagar', name: 'MontoPagar'}

    ],
    "select": {
        style: 'os',
        selector: 'td:not(:last-child)'
    },
    "columnDefs": [
        {
            'targets': 0,
            'searchable': false,
            'orderable': false,
            'width': '1%',
            'className': 'dt-body-center'
        },
        {
            "targets": [ 3 ],
            "visible": true,
            "searchable": true
        }
    ],
    "order": [],
    "initComplete": function () {
    $('#text-filter').on('keyup',function(){
        tableBiblioteca.column(3).search($(this).val()).draw();
    });
},
rowCallback: function ( row, data, index ) {

    var rowId = data["id"];
    if($.inArray(rowId, rows_selected_biblioteca) !== -1){
        $(row).find('input[type="checkbox"]').prop('checked', true);
        $(row).addClass('selected');
    }
}
});

$('#tb-biblioteca tbody').on('click', 'input[type="checkbox"]', function(e){
    var $row = $(this).closest('tr');
    var data = tableBiblioteca.row($row).data();
    var rowId = data["id"];
    var index = $.inArray(rowId, rows_selected_biblioteca);
    if(this.checked && index === -1){
        rows_selected_biblioteca.push(rowId);

    } else if (!this.checked && index !== -1){
        rows_selected_biblioteca.splice(index, 1);
    }
    if(this.checked){
        $row.addClass('selected');
    } else {
        $row.removeClass('selected');
    }
    updateDataTableSelectAllCtrl(tableBiblioteca);
    e.stopPropagation();
});

$('#tb-biblioteca').on('click', 'tbody td, thead th:first-child', function(e){
    $(this).parent().find('input[type="checkbox"]').trigger('click');
});

$('thead input[name="select_all"]', tableBiblioteca.table().container()).on('click', function(e){
    if(this.checked){
        $('#tb-biblioteca tbody input[type="checkbox"]:not(:checked)').trigger('click');
    } else {
        $('#tb-biblioteca tbody input[type="checkbox"]:checked').trigger('click');
    }

    e.stopPropagation();
});

tableBiblioteca.on('draw', function(){
    updateDataTableSelectAllCtrl(tableBiblioteca);
});