$(document).ready(function() {
    $('.main-spinner').show();
    $('#datatable_item_deposit').DataTable({
        "autoWidth": false,
        serverSide: true,
        "searching": true,
        "scrollY": "600px",
        "scrollCollapse": true,
        "scrollX": true,
        lengthMenu: [
            [20, 30, 50, 100, 200, 500, 1000],
            [20, 30, 50, 100, 200, 500, 1000]
        ],
        dom: 'Blfrtip',
        buttons: [
            'copyHtml5',
            'excelHtml5'
        ],
        ajax: '/realty/datatable-report-stockin',
        columns: [
            { data: 'DT_RowIndex', 'searchable': false },
            { data: 'project_name', 'name': 'nso007_project_stock.project_name',orderable:false},
            { data: 'category_name', 'name': 'nso007_category_stock.category_name',orderable:false},
            { data: 'item_street', 'searchable': false },
            { data: 'item_house', 'searchable': false },
            { data: 'item_zone', 'searchable': false },
            { data: 'item_size', 'searchable': false },
            { data: 'item_cost', 'searchable': false },
            { data: 'item_price', 'searchable': false }
        ],

        "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(7)
                .data()
                .reduce(function(a, b) {
                    return intVal(a) + intVal(b);
                }, 0);

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


            $(api.column(7).footer()).html("$" + parseFloat(cost_Total).toFixed(2));
            $(api.column(8).footer()).html("$" + parseFloat(price_Total).toFixed(2));
        },
    });

});