/* Create an array with the values of all the input boxes in a column, parsed as numbers */
        $.fn.dataTable.ext.order['dom-text-numeric'] = function  ( settings, col )
        {
            return this.api().column( col, {order:'index'} ).nodes().map( function ( td, i ) {
                return $('input', td).val() * 1;
            } );
        }
$(document).ready(function () {

    $(function () {
    //     $('#product_list').DataTable({
    //          'columnDefs': [{
    //      'targets': 0,
    //      'searchable': false,
    //      'orderable': false,
    //      'className': 'dt-body-center',
    //      'render': function (data, type, full, meta){
    //         //  return '<input type="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
    // return '<div class="checkbox"> <label><input type="checkbox" name="check_id[]" value=""></label>  </div>';
    //      }
    //   }]
      
      
    //     });
    //     $('.custom-searchable').each( function () {
       
    //     $(this).html( '<input class="custom-search-input" type="text" placeholder="Search '+'" />' );
    // } );
        
        var product_list_table = $('#product_list').DataTable( {
            dom: 'Bfrtip',
            // 'searching': true,
            // 'info': true,
            // 'autoWidth': false,
            // deferRender:    true,
            // scroller:       true,
            // scrollX:        true,
            // scrollCollapse: true,
            // fixedColumns: true,
            // responsive: true,
            buttons: [
            'selectAll',
            'selectNone',
            {
                extend: 'excelHtml5',
                exportOptions: {
                    columns: [ 2, 3, 7, 5 ]
                },
                title: 'Products',
				init: function(api, node, config) {
					if(!(hasPermissionExel)){
						$(node).addClass('hidden')
					}else{
						$(node).removeClass('hidden')
					}
					
					
				}
            },
            {
                text: 'Print Barcode',
                action: function ( e, dt, node, config ) { 
                    var data2 = product_list_table.rows( { selected: true } );
                    var doc = new jsPDF();
                    var left = 0;
                    var top=0;
                    var curr_qty = 1;
                    var margin_left = 10 ;
                    jQuery.each( data2.data(), function( i, val ) { 
                        
                        var divContents_name = data2.data()[i][3].substring(0,30);
                        var sku = data2.data()[i][2]; 
                        var divContents = data2.nodes().$('#print-barcode-id-'+data2.data()[i][2].replace('+','plus')+'-child')[0]['currentSrc'];
                        var qty = data2.nodes().$('.qty-box-'+data2.data()[i][2].replace('+','plus')).serializeArray()[0]['value'];

                        for (var l = 1; l <= qty; l++) {
                                    doc.addImage(divContents, "png",margin_left
+(left*75),15+top , 40, 10)
                                    doc.setFontSize(12);
                                    doc.text(divContents_name,35+(left*70),30+top,null, null, "center");
                                    left++;
                                    margin_left = 5;
                                    if (curr_qty!== 0 & curr_qty % 3 == 0) {
                                         top=top+35;
                                         left=0;
                                         margin_left = 12;

                                    }
                                    if(curr_qty!== 0 & curr_qty % 24 == 0){
                                        doc.addPage("a4", "p");
                                        left=0;
                                        top=0;
                                        margin_left = 12; 

                                    }
                                    curr_qty++;
                        }
                        if(curr_qty % 24 == 0){
                            doc.addPage("a4", "p");
                                        left=0;
                                        top=0;

                        }
                    });
                    
                doc.save('tanna_techbiz.pdf');  
                return false;
                },
				init: function(api, node, config) {
					if(!(hasPermissionPrintBarcodeAll)){
						$(node).addClass('hidden')
					}else{
						$(node).removeClass('hidden')
					}
					
					
				}
            }
            
        ],
        
        paging:         true,
        fixedColumns:   {
            leftColumns: 2
        },
        columnDefs: [ 
        {
            orderable: false,
            className: 'select-checkbox',
            targets:   0,
        } ,
        {"orderDataType": "dom-text-numeric", targets:4},
        {"targets": [ 7 ], "visible": false,},
        // { "width": 100, targets: [2,5 ]}
        ],
        
        select: {
            style:    'os',
            selector: 'td:first-child'
        },
        order: [[ 1, 'asc' ]],
        // initComplete: function () {
        //     // Apply the search
        //     this.api().columns().every( function () {
        //         var that = this;
 
        //         $('.custom-search-input',this.footer()).on( 'keyup change clear', function () {
        //             if ( that.search() !== this.value ) {
        //                 that
        //                     .search( this.value )
        //                     .draw();
        //             }
        //         } );
        //     } );
        // }
    } );
    
    
        $('#inventory_list').DataTable({
            'paging': true,
            'lengthChange': true,
            'searching': true,
            'order': [],
            'info': true,
            'autoWidth': false,
            "columnDefs": [
        { "orderDataType": "date-time", "targets": [4] }
    ]
        });
        
        // var product_list_detailed  = $('#product_list_detailed').DataTable({
        //     'paging': true,
        //     'lengthChange': true,
        //     'searching': true,
        //     'ordering': true,
        //     'info': true,
        //     'autoWidth': false,
        //     });
        
        //   $('#received_tot').html(product_list_detailed.column(2).data().sum());
        //   $('#dispatch_tot').html(product_list_detailed.column(3).data().sum());
          
        $('#custom_product_list').DataTable({
            'paging': true,
            'lengthChange': true,
            'searching': true,
            'ordering': true,
            'info': true,
            'autoWidth': false
        });
        
         $('#barcode_print_log').DataTable({
           'paging': true,
            'lengthChange': true,
            'searching': true,
            'order': [],
            'info': true,
            'autoWidth': false,
            "columnDefs": [
        { "orderDataType": "date-time", "targets": [4] }
    ]
        });
    });

    setTimeout(function () {
        $(document).find('.alert').hide(7000);
    }, 30);

    //called when key is pressed in textbox
    $(".number-only-custom").keypress(function (e) {
        //if the letter is not digit then display error and don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
    });

    $('.date-only-custom').keypress(function (e) {
        return false;
    });

    $(document).ready(function() {
    $('#product_list_detailed').DataTable( {
        "footerCallback": function ( row, data, start, end, display ) {
            var api = this.api(), data;
 
            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace(/[\-,]/g, '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            };
 
            // Total over all pages
            total = api
                .column(2)
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
 
            // Total over this page
            pageTotal = api
                .column(2, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
 
            // Update footer
            $( api.column(2).footer() ).html(
                pageTotal +' ( '+ total +' total)'
            );
            
            // Total over all pages
            totalDis = api
                .column(3)
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
 
            // Total over this page
            pageTotalDis = api
                .column(3, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
 
            // Update footer
            $( api.column(3).footer() ).html(
                pageTotalDis +' ( '+ totalDis +' total)'
            );
            
            // Total over all pages
            totalDis1 = api
                .column(4)
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b)
                }, 0 );
 
            // Total over this page
            pageTotalDis1 = api
                .column(4, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
 
            // Update footer
            $( api.column(4).footer() ).html(
                pageTotalDis1 +' ( '+ totalDis1 +' total)'
            );
        }
    } );
} );

});



