$(document).ready(function() {
	    $('.main-spinner').show();
			$('#datatable_report_commision').DataTable({
            "lengthMenu": [[20, 25, 50, 100, 500], [20, 25, 50, 100, 500]],
            "serverSide": true,
            ajax: "/realty/datatable_get_reports_commission",
                columns: [
                {data: 'DT_RowIndex',width: '4px'},
                {data: 'full_name'},
                {data: 'project_name'},
                {data: 'category_name'},
                {data: 'item_name'},
                {data: 'commission'},
                {data: 'amount'},
                {data: 'receive'},
                {data: 'balance'},
                {data: 'balance_per'},
              ],
                
                
                "initComplete": function(settings, json) {
                    $('.main-spinner').hide();
                },

                "footerCallback": function ( row, data, start, end, display ) {
                  var api = this.api(), data;
       
                  // converting to interger to find total
                  var intVal = function ( i ) {
                      return typeof i === 'string' ?
                          i.replace(/[\$,]/g, '')*1 :
                          typeof i === 'number' ?
                              i : 0;
                  };
       
                  var cost_Total = api
                      .column( 6 )
                      .data()
                      .reduce( function (a, b) {
                          return intVal(a) + intVal(b);
                      }, 0 );

                  var price_Total = api
                      .column( 7 )
                      .data()
                      .reduce( function (a, b) {
                          return intVal(a) + intVal(b);
                      }, 0 );

                  var balance_Total = api
                      .column( 8 )
                      .data()
                      .reduce( function (a, b) {
                          return intVal(a) + intVal(b);
                      }, 0 );
              
                  var numFormat = $.fn.dataTable.render.number( '\,', '.', 2, '$ ' ).display;
                  $( api.column( 6 ).footer() ).html(numFormat(cost_Total));
                  $( api.column( 7 ).footer() ).html(numFormat(price_Total));
                  $( api.column( 8 ).footer() ).html(numFormat(balance_Total));
                },	       
			});
			
			$.fn.dataTable.ext.errMode = 'throw';
});