document.addEventListener('DOMContentLoaded', function(event) {
    $.extend( $.fn.dataTable.defaults, {
        dom: '<"datatable-header"fl><"datatable-scroll"t><"datatable-footer"ip>',
        language: {
            search: '<span>Buscar:</span> _INPUT_',
            searchPlaceholder: 'Buscar...',
            lengthMenu: '<span>Mostrar:</span> _MENU_',
            paginate: { 'first': 'First', 'last': 'Last', 'next': $('html').attr('dir') == 'rtl' ? '&larr;' : '&rarr;', 'previous': $('html').attr('dir') == 'rtl' ? '&rarr;' : '&larr;' }
        }
    });
    var table = $('.datatable').DataTable({
        "processing": true,
        "serverSide": true,
        "serverMethod": 'post',
        autoWidth: false,

        "ajax":{'url':url, 
                'beforeSend': function (request) {
                    $('table.datatable').block({ 
                      message: '<i class="icon-spinner2 spinner"></i>',
                      overlayCSS: {
                          backgroundColor: '#fff',
                          opacity: 0.8,
                          cursor: 'wait',
                          'box-shadow': '0 0 0 1px #ddd'
                      },
                      css: {
                          border: 0,
                          padding: 0,
                          backgroundColor: 'none'
                      }
                  });
                 },
                "data":function (d) {
                    d._token = $('meta[name="csrf-token"]').attr('content');
                    d.action = 'showtable';
                    d.desde_fecha =  $('#fecha_desde').val();
                    d.hasta_fecha =  $('#fecha_hasta').val();
                },
                "dataSrc": function ( json ) {
                    $('table.datatable').unblock();

                    return json.data;
                }  
                
        } ,
        "columns": columns,
        "columnDefs": [
    { "width": "60%", "targets": 0 }
  ],
         "order": [],
        'createdRow': function( row, data, dataIndex ) {
            $(row).attr( 'id','codenv'+data.id );
          },
           "language": {
            "info": "Pagina _PAGE_ de _PAGES_",
        },
    });
    table.columns.adjust().draw();
});
